Programming Assignment 2 (Poker Hands)
Write a program that reads and classifies poker hands. Each card in the hand will have both a suit (clubs, diamonds, hearts or spades) and a rank (two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, or ace). No jokers are allowed. We will assume aces are high. After reading a hand of five cards, the program should classify the hand into one of the following categories (listed in order from best to worst):
- Straight flush (both straight and flush)
- Four of a kind (four cards of the same rank)
- Full house (a three-of-a-kind and a pair)
- Flush (five cards of the same suit)
- Straight (five cards with consecutive ranks)
- Three of a kind (three cards of the same rank)
- Two pairs
- Pair (two cards of the same rank)
- High card (any other hand)
Ranks: 2 3 4 5 6 7 8 9 t j q k a Suits: c d h sIf the input hand contains invalid characters, you should flag this as an error. The input to the program should be in a file whose name is provided on command line, and should have one hand per line. The following is the content of one such input file (input.txt):
2s:5s:4s:3s:6s 8c:as:7c:ad:3h as:4c:5h:ks:9s 8c:1s:7c:ax:3h 3c:4c:3d:6c:3s 3c:7d:5s:6h:4hA sample run of the program on a Terminal is shown below:
$ python3 Poker.py input.txt 2s:5s:4s:3s:6s Straight flush 8c:as:7c:ad:3h Pair 8c:1s:7c:ax:3h INVALID Hand! as:4c:5h:ks:9s High card 3c:4c:3d:6c:3s Three of a kind 3c:7d:5s:6h:4h Straight $
Some Notes:
To open and read data from a file, you can use the following code:
fname = sys.argv[1] with open(fname) as f: data = f.read().splitlines() f.close()
Some useful functions to write:
# takes as input, hand, and returns True if hand is "valid", # and False otherwise def valid_input(hand): pass # takes as input, hand, and returns a "dictionary" whose keys are # the ranks and the values are the number of cards in the hand for # the given rank def process_ranks(hand): pass # takes as input, hand, and returns a "dictionary" whose keys are # the suits and the values are the number of cards in the hand for # the given suit def process_suits(hand): pass # takes the ranks_dict for a particular hand as input and returns True # if the hand contains a "straight" def check_straight(ranks_dict): pass # takes the suits_dict for a particular hand as input and returns True # if the hand contains a "flush" def check_flush(suits_dict): pass # takes the ranks_dict for a particular hand as input and returns True # if the hand contains a "fours" def check_fours(ranks_dict): pass # takes the ranks_dict for a particular hand as input and returns True # if the hand contains a "threes" def check_threes(ranks_dict): pass # takes the ranks_dict for a particular hand as input and returns the # number of "pairs". def check_pairs(ranks_dict): pass # takes boolean inputs straight, flush, four, three, pair for a hand # and returns the highest hand classification. def hand_type(straight,flush,four,three,pair): pass
What to Submit? Poker.py