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!
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.
## 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()
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.
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.
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.
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.
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()