Sure! Here's a Python program that simulates the card game you described and determines the winning hand:
import random
def create_deck():
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits = ['♠', '♥', '♦', '♣']
deck = [(rank, suit) for suit in suits for rank in ranks]
random.shuffle(deck)
return deck
def get_rank(card):
return card[0]
def get_suit(card):
return card[1]
def get_sorted_hand(hand):
return sorted(hand, key=lambda card: (get_rank(card), get_suit(card)))
def evaluate_hand(hand):
sorted_hand = get_sorted_hand(hand)
# Check for high card
high_card = sorted_hand[-1]
# Check for pairs
pairs = []
for i in range(len(sorted_hand)-1):
if get_rank(sorted_hand[i]) == get_rank(sorted_hand[i+1]):
pairs.append((sorted_hand[i], sorted_hand[i+1]))
# Check for three of a kind
three_of_a_kind = None
for i in range(len(sorted_hand)-2):
if get_rank(sorted_hand[i]) == get_rank(sorted_hand[i+1]) == get_rank(sorted_hand[i+2]):
three_of_a_kind = (sorted_hand[i], sorted_hand[i+1], sorted_hand[i+2])
break
# Check for four of a kind
four_of_a_kind = None
for i in range(len(sorted_hand)-3):
if get_rank(sorted_hand[i]) == get_rank(sorted_hand[i+1]) == get_rank(sorted_hand[i+2]) == get_rank(sorted_hand[i+3]):
four_of_a_kind = (sorted_hand[i], sorted_hand[i+1], sorted_hand[i+2], sorted_hand[i+3])
break
return high_card, pairs, three_of_a_kind, four_of_a_kind
def compare_hands(hands):
max_high_card = None
max_pairs = []
max_three_of_a_kind = None
max_four_of_a_kind = None
for hand in hands:
high_card, pairs, three_of_a_kind, four_of_a_kind = evaluate_hand(hand)
if high_card > max_high_card:
max_high_card = high_card
max_pairs = pairs
max_three_of_a_kind = three_of_a_kind
max_four_of_a_kind = four_of_a_kind
elif high_card == max_high_card:
if len(pairs) > len(max_pairs):
max_high_card = high_card
max_pairs = pairs
max_three_of_a_kind = three_of_a_kind
max_four_of_a_kind = four_of_a_kind
elif len(pairs) == len(max_pairs):
if three_of_a_kind and not max_three_of_a_kind:
max_high_card = high_card
max_pairs = pairs
max_three_of_a_kind = three_of_a_kind
max_four_of_a_kind = four_of_a_kind
elif not three_of_a_kind and max_three_of_a_kind:
continue
elif three_of_a_kind and max_three_of_a_kind:
if get_rank(three_of_a_kind[0]) > get_rank(max_three_of_a_kind[0]):
max_high_card = high_card
max_pairs = pairs
max_three_of_a_kind = three_of_a_kind
max_four_of_a_kind = four_of_a_kind
elif get_rank(three_of_a_kind[0]) == get_rank(max_three_of_a_kind[0]):
if four_of_a_kind and not max_four_of_a_kind:
max_high_card = high_card
max_pairs = pairs
max_three_of_a_kind = three_of_a_kind
max_four_of_a_kind = four_of_a_kind
elif not four_of_a_kind and max_four_of_a_kind:
continue
elif four_of_a_kind and max_four_of_a_kind:
if get_rank(four_of_a_kind[0]) > get_rank(max_four_of_a_kind[0]):
max_high_card = high_card
max_pairs = pairs
max_three_of_a_kind = three_of_a_kind
max_four_of_a_kind = four_of_a_kind
elif get_rank(four_of_a_kind[0]) == get_rank(max_four_of_a_kind[0]):
if get_suit(four_of_a_kind[0]) > get_suit(max_four_of_a_kind[0]):
max_high_card = high_card
max_pairs = pairs
max_three_of_a_kind = three_of_a_kind
max_four_of_a_kind = four_of_a_kind
return max_high_card, max_pairs, max_three_of_a_kind, max_four_of_a_kind
def main():
num_hands = 10
deck = create_deck()
hands = [deck[i:i+5] for i in range(0, 5*num_hands, 5)]
max_high_card, max_pairs, max_three_of_a_kind, max_four_of_a_kind = compare_hands(hands)
print("Winning hand:")
print("High card:", max_high_card)
print("Pairs:", max_pairs)
print("Three of a kind:", max_three_of_a_kind)
print("Four of a kind:", max_four_of_a_kind)
if __name__ == '__main__':
main()
The program first creates a shuffled deck of cards using the create_deck() function. Then, it deals 10 hands by dividing the deck into 5-card chunks.
Next, the compare_hands() function is called to determine the winning hand among the 10 hands. It compares each hand based on the criteria described in the game rules (high card, pairs, three of a kind, and four of a kind) and updates the maximum hand as necessary.
Finally, the program prints the winning hand along with its high card, pairs, three of a kind, and four of a kind if applicable.