Python newbie trying a card game

Page 1 of 1

10 Replies - 7712 Views - Last Post: 29 October 2009 - 01:28 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=134442&amp;s=034bf96a3c3dada68627b77486fc2c78&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 python9

Reputation: 0
• Posts: 11
• Joined: 26-October 09

Python newbie trying a card game

Posted 26 October 2009 - 03:06 AM

I'm hoping to implement some Bayesian Probability into my Python Card game, but so far I'm having problems just print 10 cards and making sure I don't get two of the same card. Any ideas? This ismy code so far. My first language was VB6, so my style is pretty rough.

```suit = ['Spades', 'Hearts', 'Clubs', 'Diamonds']
face = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']

## This prints 10 random cards from a "deck". Lol.

from random import *
current = 1
def card_game(current=1):
while current in range(1, 11):
suit_random = suit[randrange(0, 3)]
face_random = face[randrange(0, 12)]

used_suit = []
used_face = []

if suit_random not in used_suit and face_random not in used_face:
print '%s of %s' % (face_random, suit_random)
else:
card_game()

used_suit.append(suit_random)
used_face.append(face_random)

current = current + 1

card_game()
```

Is This A Good Question/Topic? 0

Replies To: Python newbie trying a card game

#2 baavgai

• Dreaming Coder

Reputation: 4949
• Posts: 11,356
• Joined: 16-October 07

Re: Python newbie trying a card game

Posted 26 October 2009 - 04:31 AM

python9, on 26 Oct, 2009 - 04:06 AM, said:

from a "deck".

This is your clue. Make a "deck" of cards. Do this by filling list with all possible cards.

Then, randomly pick from that list; deleting the one you chose. The trick to this is to make a card object.

Here's some code to get you started:
```class Card():
suitNames = ['Spades', 'Hearts', 'Clubs', 'Diamonds']
faceNames = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']

def __init__(self, suitNum=0, faceNum=0):
self.suitNum = suitNum
self.faceNum = faceNum

def suitName(self):
return Card.suitNames[self.suitNum]

def faceName(self):
return Card.faceNames[self.faceNum]

def __str__(self):
return "%s of %s" % (self.faceName(), self.suitName())

def showCards():
suitNum = 0
for faceNum in range(len(Card.faceNames)):
card = Card(suitNum, faceNum)
print card

if __name__=='__main__':
showCards()

```

Hope this helps.

#3 python9

Reputation: 0
• Posts: 11
• Joined: 26-October 09

Re: Python newbie trying a card game

Posted 26 October 2009 - 01:38 PM

Wow, thanks. Im still trying to get my head around OOP-style layouts, and how to use classes efficiently. I'll take your code and learn from it

#4 python9

Reputation: 0
• Posts: 11
• Joined: 26-October 09

Re: Python newbie trying a card game

Posted 27 October 2009 - 04:44 AM

Okay. While I'm learning more about OOP-style programming, I'm still thinking about this card game project. The problem is - I'm still having a few problems.

Here is my new, refined code so far. As you can see, Im --attempting-- to build a list of all possible cards. This will save me writing out 52 entries in a list, and should act as a confidence boost as well. I may in the end scrap this after succeeding, if it hinders performance too much.

```nameSuit = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
nameFace = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']

deck = []
current = 0
current_face = 0

def Make_Deck():
while n in range(0, 3):
while n in range(0, 12):
deck.extend(nameFace[current_face] + nameSuit[current])
current_face =+ 1
current =+ 1
count =+ 1
Make_Deck()
```

As you can see, my way is really quite scrappy, and doesn't work as yet. Basically, I'm hoping to concatenate "Hearts" along with each individual entry in the "nameFace" list, all into individual entries. Then, it will concatenate "Diamonds" will all "nameFace" entries, and so on.

So, is there an easier way to do this?

#5 baavgai

• Dreaming Coder

Reputation: 4949
• Posts: 11,356
• Joined: 16-October 07

Re: Python newbie trying a card game

Posted 27 October 2009 - 05:44 AM

Ok, no OOP.

I'd write the snippet you offered like this:
```def getCardName(faceNum, suitNum):
nameSuit = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
nameFace = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
return "%s of %s" % (nameFace[faceNum], nameSuit[suitNum])

def Make_Deck():
deck = []
for suitNum in range(4):
for faceNum in range(13):
deck.append(getCardName(faceNum, suitNum))
return deck

deck = Make_Deck()
for card in deck:
print card

```

Unfortunately, you loose the value of the cards. What if I want to know just the face num?

So, here's just a little OOP.

```def getCardName(faceNum, suitNum):
nameSuit = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
nameFace = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
return "%s of %s" % (nameFace[faceNum], nameSuit[suitNum])

class Card():
def __init__(self, faceNum, suitNum):
self.faceNum = faceNum
self.suitNum = suitNum

def Make_Deck():
deck = []
for suitNum in range(4):
for faceNum in range(13):
deck.append(Card(faceNum, suitNum))
return deck

deck = Make_Deck()
for card in deck:
print getCardName(card.faceNum, card.suitNum)

```

That's just a short step from this:
```class Card():
def __init__(self, faceNum, suitNum):
self.faceNum = faceNum
self.suitNum = suitNum

def getCardName(self):
nameSuit = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
nameFace = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
return "%s of %s" % (nameFace[self.faceNum], nameSuit[self.suitNum])

def Make_Deck():
deck = []
for suitNum in range(4):
for faceNum in range(13):
deck.append(Card(faceNum, suitNum))
return deck

deck = Make_Deck()
for card in deck:
print card.getCardName()

```

Coding, no matter your language, is the art of organization. You'll find objects make the job of organizing simpler.

#6 python9

Reputation: 0
• Posts: 11
• Joined: 26-October 09

Re: Python newbie trying a card game

Posted 27 October 2009 - 09:34 AM

Thank you so much . This forum seems great.
This is quite an ask, but could you give a quick walkthrough of your OOP solution?

#7 baavgai

• Dreaming Coder

Reputation: 4949
• Posts: 11,356
• Joined: 16-October 07

Re: Python newbie trying a card game

Posted 27 October 2009 - 10:51 AM

There isn't that much too it, but I'll see what I can do.

```# class declaration
class Card():
# most classes have __init__
# this the "constructor"
# here we take two values, faceNum and suitNum
# all object methods also take the value of self ( or "this" )
# which implicitly references the current object when called
def __init__(self, faceNum, suitNum):
# here, the values passed are assigned to the
# object itself
self.faceNum = faceNum
self.suitNum = suitNum

# a method that when called will take no parameters
# note, still needs self
def getCardName(self):
# declare the arrays to do lookup
nameSuit = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
nameFace = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
# the first part here is the format mask, "%s of %s"
# which says I expect you to give me a set with two strings
# the second part is the set, using the values assigned to
# the current object to do the lookup
# and return the result
return "%s of %s" % (nameFace[self.faceNum], nameSuit[self.suitNum])

# a function
def Make_Deck():
# create an empty list
deck = []
# here range is expanded to [0,1,2,3]
# so suitNum will loop four times with those values
for suitNum in range(4):
# another loop, same idea
for faceNum in range(13):
# we create a new card object with the current values of the loop
newCard = Card(faceNum, suitNum)
# we add that object to our list
deck.append(newCard)
# when we're done with our loops, we return the results
return deck

# call our function, returns a list into variable deck
deck = Make_Deck()

# loop through all the items in deck
for card in deck:
# we know card is of type Card
# we call it's getCardName method for a friendly
# print out
print card.getCardName()
# we could also ask it about it's other properties, if we wanted
print card.faceNum

```

Not sure what more I can do with such a short example.

Hope this helps.

#8 python9

Reputation: 0
• Posts: 11
• Joined: 26-October 09

Re: Python newbie trying a card game

Posted 27 October 2009 - 01:18 PM

Wow, that really did help. Thank you very much

#9 python9

Reputation: 0
• Posts: 11
• Joined: 26-October 09

Re: Python newbie trying a card game

Posted 29 October 2009 - 06:53 AM

Sorry if this is cutting open a recently healed thread. But I'm actually getting somewhere with the OOP malark .

This is what I have so far:
```deck = []

class Card():
suitNames = ['Spades', 'Hearts', 'Clubs', 'Diamonds']
faceNames = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']

def __init__(self, suitNum=0, faceNum=0):
self.suitNum = suitNum
self.faceNum = faceNum

def suitName(self):
return Card.suitNames[self.suitNum]

def faceName(self):
return Card.faceNames[self.faceNum]

def __str__(self):
return "%s of %s" % (self.faceName(), self.suitName())

def showCards():
suitNum = 0
for suitNum in range(len(Card.suitNames)):
for faceNum in range(len(Card.faceNames)):
card = Card(suitNum, faceNum)
deck.append(card)

print 'Items in list are:\n\n'

for item in deck:
print "%d\t %s" % (deck.index(item)+1, item)

if __name__=='__main__':
showCards()
```

I basically took your template, and fiddled with it. I added the cards to a list, and printed the items in the list numbered. What do you think? Is there any way I can improve on what i've done so far?

I'm hoping to randomise the cards, deal the first 5, and then remove those 5 cards from the list, so as not to deal two of the same. Later, if i continue the project further, I can have those cards go to the end of the list, and pick the next 5.

#10 baavgai

• Dreaming Coder

Reputation: 4949
• Posts: 11,356
• Joined: 16-October 07

Re: Python newbie trying a card game

Posted 29 October 2009 - 08:13 AM

Looks good.

Now, make a Deck class too and go from there.

```class Deck():
def __init__(self):
self.cards = []
for suitNum in range(len(Card.suitNames)):
for faceNum in range(len(Card.faceNames)):
self.cards.append(Card(suitNum, faceNum))

def show(self):
print 'Items in list are:\n\n'
for i in range(len(self.cards)):
print "%d\t %s" % (i+1, self.cards[i])

def shuffle(self):
None

def deal(self, count=1):
None

if __name__=='__main__':
deck = Deck()
deck.show()

```

#11 python9

Reputation: 0
• Posts: 11
• Joined: 26-October 09

Re: Python newbie trying a card game

Posted 29 October 2009 - 01:28 PM

Lovely jubbly mate. Thank you very much