
Your assignment is to put it all together and write code that will generate hands (5 cards) and evaluate them as Poker hands. Note that a hand can be created in two different ways:
A user of your code should be able to import Hand, create a new deck (since Hand imports Card and Deck), shuffle it, deal a hand or create a hand from a list of card specs, evaluate the hand. See the examples below.
This assignment can be pretty straightforward if you handle it correctly. The first thing to do is to preprocess the hand to extract the relevant information. Here's how to do that.
Recall the ordering of ranks and suits defined in the Card class by the two lists:
CARDRANKS = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'] CARDSUITS = ['S', 'D', 'H', 'C']Now, define two new lists, full of zeros:
myRanks = [0] * 13 # a list of 13 zeros mySuits = [0] * 4 # a list of 4 zerosYou interpret each position in myRanks as recording the number of cards in the hand being evaluated of the corresponding rank in CARDRANKS. Do a similar thing with mySuits. For example, suppose you have the following cards in your hand:
Ace of Spades 3 of Diamonds King of Spades 3 of Hearts Ace of HeartsYou record this information in myRanks and mySuits by incrementing the appropriate positions in these two arrays. For the hand above, this yields:
# two Aces, two 3's, one King myRanks: [ 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2 ] # two Spades, one Diamond, two Hearts, no Clubs mySuits: [ 2, 1, 2, 0 ]
Once you've pre-processed the hand, it's very easy to evaluate it by using the information in myRanks and mySuits. For example, you have:
def evaluateHand( hand ):
myHands, mySuits = processHand( hand )
print( hand )
if hasRoyalFlush( myHands, mySuits ):
print( "Royal Flush" )
elif hasStraightFlush( myHands, mySuits ):
print( "Straight Flush" )
elif hasFourOfAKind( myHands, mySuits ):
print( "Four of a kind" )
elif hasFullHouse( myHands, mySuits ):
print( "Full House" )
elif hasFlush( myHands, mySuits ):
print( "Flush" )
elif hasStraight( myHands, mySuits ):
print( "Straight" )
elif hasThreeOfAKind( myHands, mySuits ):
print( "Three of a kind" )
elif hasTwoPair( myHands, mySuits ):
print( "Two pair" )
elif hasPair( myHands, mySuits ):
print( "Pair" )
else:
print( "Nothing" )
>>> from Hand import *
>>> c1 = Card("2H") # checking that Card works correctly
>>> print(c1)
2 of Hearts
>>> c2 = Card("TS")
>>> print(c2)
10 of Spades
>>> c1.getRank()
'2'
>>> c2.getSuit()
'S'
>>> c1 < c2
True
>>> c2 <= c2
True
>>> c1 == c2
False
>>> d = Deck() # we'll need a deck
>>> print(d)
2 of Spades # 52 Cards in order
3 of Spades
..
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
>>> d.shuffle()
>>> print(d)
4 of Spades # 52 cards, now shuffled
Queen of Spades
8 of Clubs
4 of Hearts
...
6 of Hearts
4 of Diamonds
>>> h1 = Hand(d, True) # deal a hand from the deck
>>> print( h1 )
4 of Spades # notice: first 5 cards from d
Queen of Spades
8 of Clubs
4 of Hearts
3 of Hearts
>>> h2 = Hand(d, True) # next 5 cards from d
>>> print( h2 )
7 of Hearts
King of Clubs
6 of Clubs
2 of Hearts
Jack of Hearts
# generate some hands, specifying cards
>>> cardSpec = ("AS", "AD", "AH", "AC", "2D")
>>> h3 = Hand(cardSpec, False)
>>> evaluateHand( h3 ) # list hand and evaluation
Ace of Spades
Ace of Diamonds
Ace of Hearts
Ace of Clubs
2 of Diamonds
Four of a kind
>>> cardSpec = ("AS", "2S", "3C", "4H", "5D")
>>> h4 = Hand(cardSpec, False)
>>> evaluateHand( h4 )
Ace of Spades
2 of Spades
3 of Clubs
4 of Hearts
5 of Diamonds
Straight
>>> cardSpec = ("2S", "9S", "TC", "AH", "4D")
>>> h5 = Hand(cardSpec, False)
>>> evaluateHand( h5 )
2 of Spades
9 of Spades
10 of Clubs
Ace of Hearts
4 of Diamonds
Nothing
>>> h6 = Hand(d, True) # back to dealing from the deck d
>>> evaluateHand( h6 )
9 of Spades
King of Hearts
3 of Spades
King of Diamonds
7 of Spades
Pair
>>>
What you see above is testing the Card, Deck, and Hand classes in the
interactive loop. You should also be able to run your overall program
from a file named Poker.py. It asks the user to input an
integer n meaning to deal n hands. Then in a loop,
deal and evaluate those n hands. Your code will need to
generate a new deck if the card count ever drops below 5. Here's how
that might look.
> python Poker.py How many hands should I deal? -10 Positive number required. Try again! How many hands should I deal? 12 Hand drawn (1): 5 of Hearts 7 of Clubs 9 of Clubs King of Spades 3 of Hearts Nothing Hand drawn (2): 6 of Spades Queen of Diamonds 8 of Spades Jack of Clubs 5 of Spades Nothing Hand drawn (3): 4 of Diamonds Ace of Diamonds Ace of Hearts 8 of Clubs 10 of Hearts Pair Hand drawn (4): 2 of Hearts 5 of Diamonds 2 of Spades 7 of Spades 4 of Spades Pair Hand drawn (5): 3 of Spades Queen of Clubs Ace of Clubs Queen of Spades 9 of Spades Pair Hand drawn (6): 2 of Clubs 5 of Clubs Ace of Spades 6 of Hearts 8 of Diamonds Nothing Hand drawn (7): 10 of Clubs 2 of Diamonds 6 of Clubs 7 of Diamonds 4 of Hearts Nothing Hand drawn (8): 9 of Diamonds 7 of Hearts King of Clubs Jack of Spades 6 of Diamonds Nothing Hand drawn (9): 10 of Diamonds Jack of Diamonds 10 of Spades King of Diamonds 8 of Hearts Pair Hand drawn (10): 4 of Clubs King of Hearts 9 of Hearts 3 of Diamonds Queen of Hearts Nothing Dealing a new deck. Hand drawn (11): 7 of Diamonds Jack of Clubs 10 of Spades Ace of Spades 8 of Hearts Nothing Hand drawn (12): 2 of Spades Jack of Hearts 5 of Spades 2 of Diamonds 10 of Diamonds Pair >
Your file(s) must compile and run before submission. It must also contain a header with the following format:
# Assignment: Project2 # File: Poker.py # Student: # UT EID: # Course Name: CS303E # # Date: # Description of Program:
Use functions: If you see that you're doing some computation multiple times write a function to handle that. Always test the function before you move on.
Use print for debugging: If you don't quite see what's going wrong at any point, add print statements. Often seeing what values are being generated as you go is enough to understand your error. Just remember to comment out or remove the extra print statement before you submit.