Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Poker Hand Evaluation - From Best to Worst Note: Cards can appear in any order. Royal Flush: Contains a 10, jack, queen, king and ace,
Poker Hand Evaluation - From Best to Worst
Note: Cards can appear in any order.
- Royal Flush: Contains a 10, jack, queen, king and ace, all of the same suit. For example: king clubs, jack clubs, ace clubs, 10 clubs, queen clubs.
- Straight Flush: Contains five consecutive cards that share the same suit. A straight flush does not contain an ace. For example: 3 clubs, 7 clubs, 6 clubs, 4 clubs, 5 clubs.
- Four of a Kind: Contains four cards that share the same rank. For example: 4 clubs, 10 diamonds, 4 hearts, 4 spades, 4 diamonds.
- Full House: Contains three cards that share the same rank and two cards that share a different rank. For example: jack hearts, king hearts, king clubs, jack clubs, jack diamonds.
- Straight: Contains five consecutive cards that do not all share the same suit. For example: 3 clubs, 7 diamonds, 6 hearts, 5 hearts, 4 clubs.
- Three of a Kind: Contains three cards that share the same rank and two cards that have unique ranks. For example: 6 clubs, 7 clubs, 8 clubs, 7 diamonds, 7 hearts.
- Two Pair: Contains two cards that share the same rank, two cards that share a different rank, and one card that has a unique rank. For example: six diamonds, six hearts, queen spades, ace clubs, queen diamonds.
- Pair: Contains two cards that share the same rank and three cards that have unique ranks. For example: king hearts, queen hearts, 9 diamonds, 9 hearts, ace spades.
- Nothing: All other hands of five cards. For example: 9 hearts, 6 diamonds, 3 diamonds, 10 spades, queen clubs.
Assignment
- Examine the code below, make sure that you fully understand it, and then modify the missing parts so that it evaluates poker hands correctly. When it is run using the hands provided, it should produce this output:
-
CSCI xxx: Poker Hand Evaluation Program --------------------------------------- [[10, 'spades'], [11, 'spades'], [12, 'spades'], [13, 'spades'], [14, 'spades']] --> Royal Flush [[6, 'clubs'], [7, 'clubs'], [8, 'clubs'], [9, 'clubs'], [10, 'clubs']] --> Straight Flush [[2, 'clubs'], [2, 'diamonds'], [2, 'hearts'], [2, 'spades'], [7, 'clubs']] --> Four of a Kind [[7, 'clubs'], [7, 'spades'], [8, 'clubs'], [8, 'diamonds'], [8, 'hearts']] --> Full House [[6, 'clubs'], [7, 'clubs'], [8, 'spades'], [9, 'clubs'], [10, 'clubs']] --> Straight [[7, 'clubs'], [7, 'hearts'], [7, 'spades'], [8, 'clubs'], [13, 'diamonds']] --> Three of a Kind [[6, 'diamonds'], [6, 'hearts'], [9, 'clubs'], [9, 'diamonds'], [10, 'spades']] --> Two Pair [[6, 'diamonds'], [9, 'diamonds'], [10, 'spades'], [12, 'clubs'], [12, 'hearts']] --> One Pair [[2, 'spades'], [7, 'clubs'], [8, 'diamonds'], [11, 'hearts'], [13, 'diamonds']] --> Nothing
-
- The evaluate function is correct - do not modify it.
- If it is helpful to do so, you are welcome to introduce other functions into your solution.
Grading - 100 points
Note: Your program will be tested on a different set of poker hands than the ones that appear in the main() function of poker.py. Thus, it is important that you test your program on different inputs and identify the different types of poker hands in a general fashion.
- 10 points - Every royal flush is identified correctly. (All or nothing.)
- 5 points - Every straight flush is identified correctly. (All or nothing.)
- 10 points - Every four of a kind is identified correctly. (All or nothing.)
- 10 points - Every full house is identified correctly. (All or nothing.)
- 10 points - Every straight is identified correctly. (All or nothing.)
- 10 points - Every three of a kind is identified correctly. (All or nothing.)
- 10 points - Every two pair is identified correctly. (All or nothing.)
- 10 points - Every pair is identified correctly. (All or nothing.)
- 10 points - Every nothing is identified correctly. (All or nothing.)
- 15 points - The Python solution is properly commented, easy to understand, high quality and does not contain unnecessary code. (3 points for each type of improvement up to 15 points.)
Code:
def get_all_ranks(hand): result = [] for card in hand: result.append(card[0]) return result def royal_flush(hand): return False def straight_flush(hand): return False def straight(hand): return False def four_of_a_kind(ranks): return False def full_house(ranks): return False def three_of_a_kind(ranks): return False def two_pair(ranks): return False def one_pair(ranks): return False # -----------------------------------------+ # Do not modify the evaluate function. | # -----------------------------------------+ def evaluate(poker_hand): poker_hand.sort() poker_hand_ranks = get_all_ranks(poker_hand) print(poker_hand, "--> ", end="") if royal_flush(poker_hand): print("Royal Flush") elif straight_flush(poker_hand): print("Straight Flush") elif four_of_a_kind(poker_hand_ranks): print("Four of a Kind") elif full_house(poker_hand_ranks): print("Full House") elif straight(poker_hand): print("Straight") elif three_of_a_kind(poker_hand_ranks): print("Three of a Kind") elif two_pair(poker_hand_ranks): print("Two Pair") elif one_pair(poker_hand_ranks): print("One Pair") else: print("Nothing") # -----------------------------------------+ def main(): print("CSCI xxx: Poker Hand Evaluation Program") print("---------------------------------------") evaluate([[10, "spades"], [14, "spades"], [12, "spades"], [13, "spades"], [11, "spades"]]) # royal flush evaluate([[10, "clubs"], [9, "clubs"], [6, "clubs"], [7, "clubs"], [8, "clubs"]]) # straight flush evaluate([[2, "diamonds"], [7, "clubs"], [2, "hearts"], [2, "clubs"], [2, "spades"]]) # 4 of a kind evaluate([[8, "diamonds"], [7, "clubs"], [8, "hearts"], [8, "clubs"], [7, "spades"]]) # full house evaluate([[13, "diamonds"], [7, "clubs"], [7, "hearts"], [8, "clubs"], [7, "spades"]]) # 3 of a kind evaluate([[10, "clubs"], [9, "clubs"], [6, "clubs"], [7, "clubs"], [8, "spades"]]) # straight evaluate([[10, "spades"], [9, "clubs"], [6, "diamonds"], [9, "diamonds"], [6, "hearts"]]) # 2 pair evaluate([[10, "spades"], [12, "clubs"], [6, "diamonds"], [9, "diamonds"], [12, "hearts"]]) # 1 pair evaluate([[2, "spades"], [7, "clubs"], [8, "diamonds"], [13, "diamonds"], [11, "hearts"]]) # nothing # -----------------------------------------+ main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started