Write a simplified version of Texas Hold'em. Given two five-card hands, compare them and determine the winner.
The design needed to use object-oriented principles, pay attention to code style, and be extensible.
Game Rules
Goal:
Compare two five-card hands, hand_1 and hand_2, and determine the winner.
Card values:
9 is the highest value and 1 is the lowest. There are no letter cards such as ace, king, queen, or jack.
Comparison within the same hand category:
If both hands have the same category, compare their cards one by one from right to left, meaning from the last card dealt back to the first. Do not sort either hand. The first larger card encountered wins. If every card is identical, the result is a tie.
Hand rankings, strongest to weakest:
Five of a kind: five equal cards, such as 99999.
Four of a kind: four equal cards and one different card, such as 99998.
Full house: three equal cards and two other equal cards, such as 33322.
Two pair: two equal cards, another two equal cards, and one different card, such as 33224.
Three of a kind: three equal cards and two distinct other cards, such as 99987.
One pair: two equal cards and three mutually distinct other cards, such as 55432.
High card: all five cards are distinct, such as 97531.
The code needed to follow good object-oriented programming practices so that new hand categories could be added easily later.
Task 1: Evaluate the Winner
Implement a function that compares two complete five-card hands:
evaluate(hand1, hand2)
Return which hand wins.
Task 2: Predict the Remaining Cards
Given any partial hand with fewer than five cards, such as two cards, predict how to fill the remaining three positions for the strongest and weakest possible outcomes.
best_hand(partial_hand)
Return the strongest five-card combination that can be formed. For input 99, the output is 99999, producing five of a kind.
worst_hand(partial_hand)
Return the weakest five-card combination that can be formed. For input 99, the output is 99321, using the smallest possible distinct values to produce one pair.
If the input hand already contains five cards, both functions should return that hand unchanged.
A code-pad AI chat was available. It could answer questions about specific logic, syntax, library functions, or utility functions. Using it to write helper functions could save a lot of time.
Discussion
Loading comments…