Need help with Python programming problem.

Sean Ross sross at connectmail.carleton.ca
Sat May 10 11:31:25 EDT 2003


Hi.
This may be overkill, and it will likely be a little less straight-forward
but here's a suggested framework:

Make a card class. Each card knows its rank and suit.

Make an enumeration of suits, i.e., and face values
    SUITS = (HEARTS, SPADES, DIAMONDS, CLUBS) = range(4)
    FACES = (JACKS, QUEENS, KINGS) = range(11,14)
this'll let you do things like check for face cards, and/or for flushes.

Make a player class. Each player has a 'hand' and a 'score' and perhaps a
score_hand() method. One benefit of this approach is that you can subclass
this class for each type of card game, and you'll usually only have to
override the score_hand() method. For example,
you can make a

class PokerPlayer(Player):
    def score_hand(self):
        # score hand using poker rules

You should also make the card instances comparable, so that you can do
things like
         # say we're inside a Player instance method
         self.hand.sort()  # hand is a list of cards
This'll make it easier to spot runs.

Make a deck class as a subclass of list. i.e., class Deck(list): ... .
Populate the deck with the appropriate 52 card instances. The deck should
have shuffle()
and deal() methods. deal() should return a list of three card instances.
Then you can do things like:
    # shuffle cards
    deck.shuffle()
    # deal cards to players 'user' and 'computer'
    user.hand = deck.deal(3)
    computer.hand = deck.deal(3)

One problem with your game as it stands is that if you only have two players
and each player only gets 3 cards for each hand, you will run short of cards
before exhausting the deck. In other words, 6 does not divide evenly into
52, so you can't   'e) Repeat until the deck is empty'
Just so you know...

Anyway, this is the outline of one approach. There are several others.
Good luck with what you're doing.
Sean


"gk" <topsoil at mail.com> wrote in message
news:Xns937753D7E64B7topsoilmailcom at 199.184.165.241...
> Been trying to get the following problem to work. So far its been
> hectic since i am very new to programming:
>
>
> Make a program that simulates a card game.
>
>
> You have a Deck of cards that contains the following:
>
>
>
> -4 suits (spades, hearts, diamonds, clubs)
> -Each suit has 14 ranks and points assigned to them:
>
> 2= 2 points
> 3= 3 points
> 4= 4 points
> 5= 5 points
> 6= 6 points
> 7= 7 points
> 8= 8 points
> 9= 9 points
> 10= 10 points
> Jack= 11 points
> Queen= 12 points
> King= 13 points
> Ace= 14 points
>
>
> First the computer picks 3 cards and then the user gets 3 cards. The
> following rules are used to calculate the total points accumulated
> for the computer and the user:
>
> -Each Card carries points equivalent to its rank except the Ace card,
> which is worth 14 points.
>
> -If all 3 cards are of the same suit, then add 30 bonus points to the
> total points of the 3 cards.
>
> - If all 3 cards are of the same rank, add 50 points bonus to the
> total points of the 3 cards.
>
> - if all 3 cards are face cards (meaning they have faces like a
> king,queen,jack) and are of the same rank, then add 100 points bonus
> to the total points of the 3 cards.
>
> - if all 3 cards of the same suit are a sequence, add 30 points bonus
> to the total points of the 3 cards.
>
>
> At the end of the game the winner is announced between the computer
> and the user.
>
>
>
> The following is what i think should be done (got help on it from
> another forum), but i don't know much syntax and therefore am stuck:
>
> 1) Create somewhere to store the deck of 52 cards (e.g. a list named
> Deck)
> (Hint - search for list.append() and list.pop() help)
> 2) Create somewhere to store user points and computer points
> 3) Fill "Deck" with 52 cards, one of each suite
> 4) Shuffle the deck
> 5) a) Read 3 cards
>    b) Calculate points
>    c) Calculate bonus points
>    d) Add to user or computer store
>    e) Repeat until the deck is empty
> 6) Print scores
>
>
>
> import random
>
> ##Store the deck as a list
> Deck = []
> UserPoints = 0
> CompPoints = 0
>
> ##Populate the deck with cards
> ##Hint - for x in range(14)
> ##Hint2 - for s in ['hearts', 'spades']
>
> ##Shuffle the deck
> random.shuffle(deck)
>
> ##Main loop
> while Deck:
>   ##Read 3 cards
>
>   ##calculate points
>
>   ## calculate bonus points
>
>   ## Add points and bonus points to total
>
>   ## etc
>
> ##Print winner
>
>
>
>
> Thanks.






More information about the Python-list mailing list