School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,023 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,185 people online right now. Registration is fast and FREE... Join Now!




Python newbie trying a card game

 

Python newbie trying a card game

python9

26 Oct, 2009 - 02:06 AM
Post #1

New D.I.C Head
*

Joined: 26 Oct, 2009
Posts: 7


My Contributions
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.

CODE
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()


User is offlineProfile CardPM
+Quote Post


baavgai

RE: Python Newbie Trying A Card Game

26 Oct, 2009 - 03:31 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,348



Thanked: 411 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(python9 @ 26 Oct, 2009 - 04:06 AM) *

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:
python

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.

User is offlineProfile CardPM
+Quote Post

python9

RE: Python Newbie Trying A Card Game

26 Oct, 2009 - 12:38 PM
Post #3

New D.I.C Head
*

Joined: 26 Oct, 2009
Posts: 7


My Contributions
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 smile.gif
User is offlineProfile CardPM
+Quote Post

python9

RE: Python Newbie Trying A Card Game

27 Oct, 2009 - 03:44 AM
Post #4

New D.I.C Head
*

Joined: 26 Oct, 2009
Posts: 7


My Contributions
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.

CODE
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?

User is offlineProfile CardPM
+Quote Post

baavgai

RE: Python Newbie Trying A Card Game

27 Oct, 2009 - 04:44 AM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,348



Thanked: 411 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Ok, no OOP. sad.gif

I'd write the snippet you offered like this:
python

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. wink2.gif

python

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:
python

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.

User is offlineProfile CardPM
+Quote Post

python9

RE: Python Newbie Trying A Card Game

27 Oct, 2009 - 08:34 AM
Post #6

New D.I.C Head
*

Joined: 26 Oct, 2009
Posts: 7


My Contributions
Thank you so much smile.gif. This forum seems great.
This is quite an ask, but could you give a quick walkthrough of your OOP solution?
User is offlineProfile CardPM
+Quote Post

baavgai

RE: Python Newbie Trying A Card Game

27 Oct, 2009 - 09:51 AM
Post #7

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,348



Thanked: 411 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

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

python

# 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.

User is offlineProfile CardPM
+Quote Post

python9

RE: Python Newbie Trying A Card Game

27 Oct, 2009 - 12:18 PM
Post #8

New D.I.C Head
*

Joined: 26 Oct, 2009
Posts: 7


My Contributions
Wow, that really did help. Thank you very much smile.gif
User is offlineProfile CardPM
+Quote Post

python9

RE: Python Newbie Trying A Card Game

29 Oct, 2009 - 05:53 AM
Post #9

New D.I.C Head
*

Joined: 26 Oct, 2009
Posts: 7


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

This is what I have so far:
CODE
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.



User is offlineProfile CardPM
+Quote Post

baavgai

RE: Python Newbie Trying A Card Game

29 Oct, 2009 - 07:13 AM
Post #10

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,348



Thanked: 411 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Looks good.

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

python

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])

# your code here
def shuffle(self):
None

def deal(self, count=1):
None

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


User is offlineProfile CardPM
+Quote Post

python9

RE: Python Newbie Trying A Card Game

29 Oct, 2009 - 12:28 PM
Post #11

New D.I.C Head
*

Joined: 26 Oct, 2009
Posts: 7


My Contributions
Lovely jubbly mate. Thank you very much smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 08:12AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month