Welcome to Dream.In.Code
Getting Help is Easy!

Join 109,556 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,351 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Python poker game

 
Reply to this topicStart new topic

Python poker game

soltan
post 16 Jul, 2008 - 08:45 AM
Post #1


New D.I.C Head

*
Joined: 13 Jul, 2008
Posts: 2

Hello everyone , i'm glad that i 've found this website as it truly helpfull..

to my problem, i'm a beginner in Python programming but i want to do a simple poker game using this amazing language so far , the problem is that i can't see how can i make it run (e.g. risk calculation) and to connect all different functions together . i want it to be simple so I included my code here , any comment or advice will be greatly appreciated.

thank you in advance,
soltan

CODE

from random import *
from math import *

#GLOBAL VARIABLES

cards = range(0,52)

def randRange(lower,upper):
    """return a value in the range in_lower to in_upper
    inclusive
    """
    temp_range = upper - lower
    return int(round((temp_range+0.5)*random() + (lower - 0.5)))

def dealCard():
    """simulate a deal from a hand of cards
    """
    return randRange(0,51)

def randArray(in_list):
    return in_list[randRange(0,len(in_list)-1)]
    
def popRandArray(in_list):
    """ return a random value from an array with no replacement
    """
    return in_list.pop(randRange(0,len(in_list)-1))
    
def dealCardTuple():
    """return a tuple with a random card assigned
    """
    return int(randRange(0,12)),int(randRange(0,3))

def cardTupleAsString(in_tuple):
    """turn a deal from a hand of cards as a tuple into a string
    """
    temp_value,temp_suite = in_tuple
    value = ["ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"]
    suite = ["hearts","diamonds","spades","clubs"]
    return value[temp_value]+ " of " + suite[temp_suite]

def cardAsString(in_card):
    """turn a deal from a hand of cards as a tuple into a string
    """
    value = ["ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"]
    suit = ["hearts","diamonds","spades","clubs"]
    return value[in_card%13]+ " of " + suit[in_card/13]
    
def realDealCard():
    """performs a non replacement randomised lookup
    """
    global cards
    #check to see if the array still has a card in it
    if len(cards)==0:
        print "new deck"
        cards = range(0,52)
    return popRandArray(cards)
    
def testCards():
    """tests the distribution of card when dealing a mulitple
    number of hands"""
    hands=int(raw_input('how many hands do you want to play?:'))
    histo = 52 * [0]
    for i in range(1,hands*52+1):
        n = realDealCard()
        histo[n] = histo[n] + 1
    for i,v in enumerate(histo):
        print cardAsString(i) + " " + str(v)
#print dealCard()
def cardScore(in_card):
    """converts a card into a numerical score"""
    score = in_card%13+1
    if score > 10:
        score = 11
    return score
User is offlineProfile CardPM

Go to the top of the page


BetaWar
post 16 Jul, 2008 - 10:55 AM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,266



Thanked 38 times

Dream Kudos: 775
My Contributions


I would suggest looking into pygame or wxPython to make it easier to add a GUI, if you are wanting to do something other than a standard text-based game. Second, I would look into using classes, that way you can place all the relevant functions into a single class and have an __init__ function (def) which will be auto-run when you call to the class. That way you can have it loop through calling all the functions and things that you need.

If you want to stay with the code as it currently is look inot creating an init function anyway and then having it call to the other functions in order that they are needed.

for instance you may want to have a function that goes like so for your init:

CODE

def init():
    for i in range(1, 5):
        print dealCard()
    while True:
        pass
        #Keeps the Console open even though the function has finished
init()


Then all yuou have to do is get the init function to do what you want and it will keep the window open until you close it (or return false).

Assuming that this is all the code you have it is doing exactly what it should do (I believe) and that is nothing, since there are not calls to functions outside of functions (that aren't commented out). Additionally, even if you have it run as it currently is there is nothing there to keep the console window open, that means it will close as soon as it has finished doing what it is supposed to do.

Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

soltan
post 17 Jul, 2008 - 11:16 AM
Post #3


New D.I.C Head

*
Joined: 13 Jul, 2008
Posts: 2

that was quite helpfull betawar icon_up.gif

Thank you very much for your time ,

soltan
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/7/08 11:11PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month