Class Project 2: 5-Card Stud
Work in groups of about 4.
This project is designed to let you practice working with classes. Remember, classes are your way of
creating types. You have a lot of power with classes – they allow you to organize methods (functions)
and properties that belong together conceptually.
The overall goal with this project is to create the necessary classes for the poker game, 5-card stud. The
idea behind 5-card stud is that each player gets 2 cards from a deck of cards. The first card for each
player is hidden from every player except that player (so in our game, you will be able to see your first
card, but not the first card of each computer player). Then everyone sees everybody’s second card.
After the second card, everyone places a bet, and another round of cards is dealt. Yet more betting,
then another card is dealt to everyone. This pattern of dealing, then betting happens until everyone has
5 cards and has done a final round of betting. Then the winning is determined. The winning hands are
determined as follows:
##Hands (in order):
## 1. royal flush (e.g., AH, KH, QH, JH, 10H)
## 2. straight flush (9C, 8C, 7C, 6C, 5C)
## 3. four of a kind (KH, KD, KS, KC)
## 4. full house (10H, 10D, 10C, 3S, 3D)
## 5. flush (e.g., 10H, 8H, 6H, 3H, 2H)
## 6. straight (e.g., KH, QD, JC, 10H, 9S)
## 7. 3 of a kind (e.g.,3D, 3H, 3Q, 10S, 4H)
## 8. two pair (8H, 8D, 4C, 4D, AS)
## 9. one pair (e.g., KD, KS, 9S, 7C, 3H)
## 10. high card (e.g., AS, QS, 9C, 4H, 2D)
Once the winner has been determined, the pot goes to the winner’s money, and every other player
loses the amount they’ve bet along with their ante.
If a player runs out of money, they lose and are ejected from the game. The last player in the game
For more details on how the game is played, go to:
(To be honest, there’s an awful lot on the betting part of this game and how it’s done. In my version of
the game, there’s the ante, which is 10 dollars, that every player throws in before any cards are dealt.
Then the user actually bets after each round, but the computer just randomly picks an amount between
0 and 20 dollars to bet. There’s no intelligence there at all. Equally, if you bet 0, you don’t fold from the
game. You just stay in the game and put no money in the pot. I’m pretty sure this isn’t how real poker
is played, largely because I keep winning. Think of my version as a confidence booster.)
Your job:
Part A:
First, create a class for a basic card:
The card class should have 2 properties: the suit, and the face of a card. The suit will be one of the
following letters, ‘S’,’H’,’D’, or ‘C’. The face will be a number, 2 through 14. The constructor (the
__init__ method) should initialize both the suit and the face.
The class should also have a __str__ method that has no input parameters, and returns a string (which is
what will be printed). This method creates the string that is printed out when you print a card. Note for
this, if the face is 14, you should print ‘A’, if it’s 13, you should print ‘K’, if it’s 12, you should print ‘Q’,
and if it’s 11, you should print ‘J’ along with the suit. (e.g., what should be printed out is JC, AH, 8D,
Once you have the __str__ method written, test it out. Create a couple different variables of type Card.
Then print them out.
The final method for the card class is a __lt__method. This method determines whether the current
card is less than another card or not, and if it is, it returns True, if it isn’t, it returns False (the Boolean
values). The thing you need to know about cards (that I had to look up) is that if the faces are equal,
then suits are rated as less than each other as follows: ‘C’ is less than ‘D’, which is less than ‘H’, which is
less than ‘S’. If you just happened to know that off the top of your head, there’s a gambler’s anonymous
number I can get for you.
Once you have that written, test it out. Make a couple of variables of type Card, then use < to see
whether the method is correctly determining which card is less than the other card.
Now do something cool. Make a list of Cards. Then use .sort (the sort method belonging to all lists) to
sort them. See if this works.
class Card(object):
def __init__
def __str__
def __lt__
Once you’ve got all that working, move to part B:
Part B:
Create a class for a DeckofCards. This class will have 2 properties: a list of all the card objects in a deck,
and then a list of the same card objects shuffled (Note: there’s more than one way to do this. The
property that is critical is the shuffled deck property, which I called shuffled.
To create a deck property that is a list of Card objects, you can do two things:
self.deck = [Card(2,’S’),Card(3,’S’),Card(4,’S’),…
(This takes a while, but works just fine)
Or you can append each card to the list, e.g.,
Etc. for every card.
This Class needs a shuffledeck method that randomly rearranges all the cards objects in the deck, and
sets the shuffle property to the newly rearranged list.
You may want to write a __str__ method for this function to print out the shuffled deck to make sure
your shuffle method works – that’s up to you.
This class also needs a getcard method, that gets the next card in the shuffled deck. If the getcard
method is called, and you are at the end of the shuffled deck, the method should call the shuffledeck
method to shuffle the cards, and then return the first card from the newly shuffled deck.
Once you have written your methods, create a variable of type DeckofCards. Then test your shuffledeck
method to make sure it is working properly. Do the same for your getcard method.
class DeckofCards(object):
def __init__
def shuffledeck
def __str__
def getcard
Part C:
Now write a class for a player of the game. Start with the constructor (the __init__ method). The Player
class has:
a name property, which is a string, and the player’s name.
a money property, which is an int and I initialized with 100 (for 100 dollars), but you can give
your player(s) more or less money.
A hand property, which is a list of Card objects. This should initially be an empty list
A currscore property, which is an int, and should be initialized to -1. This property will be set to
the hand’s score after the entire hand has been dealt. So for instance, if the hand has two 3s
and 2 7s, the score of the hand will be that of 2 pairs, which is 8. If the hand has 3 9s and 2 4s,
the score of the hand will be that of a full house, or a 4.
A currbet property, which is an int and will be used to represent the total amount the player has
bet in a round of the game so far. It should be initialized to 0.
A comp property, which is a Boolean value, and set to True if the Player is a computer, and set
to False, if the player is a human.
And a fold property, which is also a Boolean value, initially set to False. This property will
changes to True if the player folds during a hand.
class Player(object):
def __init__
Now write a method (as part of the Player class) that prints out a player’s hand. It should print out the
player’s name, and then the cards that have been dealt so far. This function should take as an input
parameter a Boolean value. If the Boolean value is set to False, the first card is printed as ‘XX’, for
p3: XX 5S JS 5H 3H
whereas if the Boolean value is set to True, the first card is printed as normal, e.g.,
p2: 10S 7D 5S 5H 5C
def printHand
And finally, as part of the Player class, you should hae a function getScore. For me, the easiest way to
write this function was to sort the Player’s hand list first, and then print it out. If you got this to work for
your card class, this just involves saying, self.hand.sort() If you want it to sort from largest to smallest,
then you’d say, self.hand.sort(reverse = True)
Print out the hand using the function you just wrote to make sure it sorted first.
Now determine what score to give the Player’s hand, and set currscore to that score. The scores are as
##Hands (in order, scores at the left):
## 1: royal flush (e.g., AH, KH, QH, JH, 10H)
## 2: straight flush (9C, 8C, 7C, 6C, 5C)
## 3: four of a kind (KH, KD, KS, KC)
## 4: full house (10H, 10D, 10C, 3S, 3D)
## 5: flush (e.g., 10H, 8H, 6H, 3H, 2H)
## 6: straight (e.g., KH, QD, JC, 10H, 9S)
## 7: 3 of a kind (e.g.,3D, 3H, 3Q, 10S, 4H)
## 8: two pair (8H, 8D, 4C, 4D, AS)
## 9: one pair (e.g., KD, KS, 9S, 7C, 3H)
## 10: high card (e.g., AS, QS, 9C, 4H, 2D)
Note that this function doesn’t break ties, it just gives each hand a score based on which of the above
categories it fits into. It sets the Player’s currscore property to 1-10, depending on what pattern it fits
from above. It then prints out the Player’s currscore.
def getscore(self):