Bored, I used bolt.new to create a code for a card game to play, written by AI. Reading AI's code often leads to rapid improvement.
Write a program to deal cards and determine the winner based on hand strength.
Game Rules:#
A standard deck of cards, with the jokers removed, each player is dealt 3 cards, and the hands are compared to see who wins.
There are the following types of hands:
Three of a Kind: Three cards of the same rank, such as three 6s.
Straight Flush: Also known as a flush straight, which is three cards of the same suit in sequence, such as hearts 5, 6, 7.
Straight: Also known as a tractor, cards of different suits but in sequence, such as hearts 5, diamonds 6, spades 7, forming a straight.
Pair: Two cards of the same rank.
Single Card: The highest single card is an Ace.
The ranking of these hands from highest to lowest is: Three of a Kind > Straight Flush > Straight > Pair > Single Card.
Points to be implemented in the program:#
- 
First, generate a complete deck of cards. 
- 
Deal cards randomly to 5 players. 
- 
Reveal the hands, compare them, and output who the winner is. 
import random
# Define the suits and ranks of the cards
suits = ['♥', '♦', '♣', '♠']
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
# Generate a complete deck of cards
deck = [(suit, rank) for suit in suits for rank in ranks]
# Shuffle the deck
random.shuffle(deck)
# Deal cards to 5 players, 3 cards each
players = []
for i in range(5):
    hand = deck[:3]
    players.append(hand)
    deck = deck[3:]
# Define the ranking order of hands
def rank_value(rank):
    if rank == 'A':
        return 14
    elif rank == 'K':
        return 13
    elif rank == 'Q':
        return 12
    elif rank == 'J':
        return 11
    else:
        return int(rank)
def is_straight(hand):
    values = sorted([rank_value(rank) for suit, rank in hand])
    return values[0] + 1 == values[1] and values[1] + 1 == values[2]
def is_flush(hand):
    suits = [suit for suit, rank in hand]
    return len(set(suits)) == 1
def is_three_of_a_kind(hand):
    values = [rank_value(rank) for suit, rank in hand]
    return values[0] == values[1] == values[2]
def is_pair(hand):
    values = [rank_value(rank) for suit, rank in hand]
    return values[0] == values[1] or values[1] == values[2] or values[0] == values[2]
def hand_rank(hand):
    if is_three_of_a_kind(hand):
        return (5, max([rank_value(rank) for suit, rank in hand]))
    elif is_straight(hand) and is_flush(hand):
        return (4, max([rank_value(rank) for suit, rank in hand]))
    elif is_straight(hand):
        return (3, max([rank_value(rank) for suit, rank in hand]))
    elif is_pair(hand):
        return (2, max([rank_value(rank) for suit, rank in hand]))
    else:
        return (1, max([rank_value(rank) for suit, rank in hand]))
# Compare hand strengths
def compare_hands(hand1, hand2):
    rank1 = hand_rank(hand1)
    rank2 = hand_rank(hand2)
    if rank1 > rank2:
        return 1
    elif rank1 < rank2:
        return -1
    else:
        return 0
# Output each player's hand and hand rank
for i, hand in enumerate(players):
    print(f"Player {i + 1}: {hand}, Hand Rank: {hand_rank(hand)}")
# Compare all players' hands to find the winner
winner = players[0]
for hand in players[1:]:
    if compare_hands(hand, winner) > 0:
        winner = hand
# Output the winner
winner_index = players.index(winner)
print(f"Winner is Player {winner_index + 1} with hand {winner}")