Simulate a Four-Seat Trick-Taking Card Game with Scoring
Data Structure DesignAuto-graded · 12 test casesLast updated
Simulate a full 13-trick, four-player card game with deterministic dealing, follow-suit rules, and point scoring, and return the complete game record.
Simulate a Four-Seat Trick-Taking Card Game with Scoring
Write a function that plays one complete game of a four-player trick-taking card game and returns a full record of what happened. Everything is deterministic: the shuffle is given as a fixed permutation, and every choice a player makes is fixed by the rules below.
Inputs
deck: 52 unique card strings in the formSUIT_RANK, such as"H_10"or"S_A".seat_names: four player names. The player at indexisits in seati.first_leader: the seat index (0 to 3) that leads the first trick.rank_order: the 13 ranks from lowest to highest.suit_order: the 4 suits in sorting order. This order is used only for sorting hands, and no suit is trump.point_values: a dictionary mapping a rank to its point value. Any rank not in the dictionary is worth 0.deal_order: a permutation of0..51.
Rules
Dealing. Build the shuffled deck by taking deck[deal_order[0]], deck[deal_order[1]], and so on. Deal the cards one at a time in that order to seats 0, 1, 2, 3, 0, 1, 2, 3, and so on. Each player ends up with 13 cards.
Hand order. Sort each hand ascending by suit position in suit_order first, then by rank position in rank_order.
Tricks. Play 13 tricks. In each trick the players act in seat order starting from the current leader and wrapping around (leader, leader+1, ... modulo 4). The first leader is first_leader.
- Leader: plays the first card of their current sorted hand, that is, the smallest card by suit order and then by rank. This card's suit is the lead suit.
- Followers: if the player holds any card of the lead suit, they play the lowest-ranked such card. Otherwise they play the first card of their current sorted hand.
- A played card is removed from the hand.
Trick winner. The winner is the player who played the highest-ranked card among the cards of the lead suit. Off-suit cards never win. Ranks within one suit are unique, so there are no ties. The winner leads the next trick.
Trick score and capture. A trick's score is the sum of point_values over the four cards played, with 0 for ranks not listed. The winner captures all four cards, appended to their capture list in the order they were played, and adds the trick score to their total.
Overall winner. The winner is the player with the highest total. If several players tie, choose the one with the lowest seat index.
Return value
Return a dictionary with exactly these keys:
"hands": a list of four lists, the cards each player still holds after all tricks. This is normally four empty lists."tricks": a list of 13 dictionaries in play order. Each has:"starter": the leader's seat index."lead_suit": the lead suit."plays": a list of four{"player": seat, "card": card}dictionaries in play order."winner": the winning seat index."score": the trick score.
"captured": a list of four lists, the cards each seat captured, in the order described above."scores": a dictionary mapping each player's name to their total, in seat order."winner": the name of the overall winner.
Input validation
Raise ValueError if any of these hold:
- There are not exactly four players or 52 cards.
deal_orderis not a permutation of0..51.- Cards are not unique.
- A card is not in
SUIT_RANKform with a known suit and rank. - There are not exactly 13 ranks and 4 suits.
Constraints
- Exactly 4 players and 52 unique cards.
0 <= first_leader <= 3.- Rank and suit names contain no underscores.
point_valueskeys are rank names, and values are integers.- Runtime should be well within a second.
Examples
Example 1
Input: simulate_trick_game(['C_2', 'C_3', 'C_4', 'C_5', 'C_6', 'C_7', 'C_8', 'C_9', 'C_10', 'C_J', 'C_Q', 'C_K', 'C_A', 'D_2', 'D_3', 'D_4', 'D_5', 'D_6', 'D_7', 'D_8', 'D_9', 'D_10', 'D_J', 'D_Q', 'D_K', 'D_A', 'H_2', 'H_3', 'H_4', 'H_5', 'H_6', 'H_7', 'H_8', 'H_9', 'H_10', 'H_J', 'H_Q', 'H_K', 'H_A', 'S_2', 'S_3', 'S_4', 'S_5', 'S_6', 'S_7', 'S_8', 'S_9', 'S_10', 'S_J', 'S_Q', 'S_K', 'S_A'], ['Alice', 'Bob', 'Carol', 'Dave'], 0, ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'], ['C', 'D', 'H', 'S'], {'5': 5, '10': 10, 'K': 10}, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51])
Output: {'hands': [[], [], [], []], 'tricks': [{'starter': 0, 'lead_suit': 'C', 'plays': [{'player': 0, 'card': 'C_2'}, {'player': 1, 'card': 'C_3'}, {'player': 2, 'card': 'C_4'}, {'player': 3, 'card': 'C_5'}], 'winner': 3, 'score': 5}, {'starter': 3, 'lead_suit': 'C', 'plays': [{'player': 3, 'card': 'C_9'}, {'player': 0, 'card': 'C_6'}, {'player': 1, 'card': 'C_7'}, {'player': 2, 'card': 'C_8'}], 'winner': 3, 'score': 0}, {'starter': 3, 'lead_suit': 'C', 'plays': [{'player': 3, 'card': 'C_K'}, {'player': 0, 'card': 'C_10'}, {'player': 1, 'card': 'C_J'}, {'player': 2, 'card': 'C_Q'}], 'winner': 3, 'score': 20}, {'starter': 3, 'lead_suit': 'D', 'plays': [{'player': 3, 'card': 'D_4'}, {'player': 0, 'card': 'D_5'}, {'player': 1, 'card': 'D_2'}, {'player': 2, 'card': 'D_3'}], 'winner': 0, 'score': 5}, {'starter': 0, 'lead_suit': 'C', 'plays': [{'player': 0, 'card': 'C_A'}, {'player': 1, 'card': 'D_6'}, {'player': 2, 'card': 'D_7'}, {'player': 3, 'card': 'D_8'}], 'winner': 0, 'score': 0}, {'starter': 0, 'lead_suit': 'D', 'plays': [{'player': 0, 'card': 'D_9'}, {'player': 1, 'card': 'D_10'}, {'player': 2, 'card': 'D_J'}, {'player': 3, 'card': 'D_Q'}], 'winner': 3, 'score': 10}, {'starter': 3, 'lead_suit': 'H', 'plays': [{'player': 3, 'card': 'H_3'}, {'player': 0, 'card': 'H_4'}, {'player': 1, 'card': 'H_5'}, {'player': 2, 'card': 'H_2'}], 'winner': 1, 'score': 5}, {'starter': 1, 'lead_suit': 'D', 'plays': [{'player': 1, 'card': 'D_A'}, {'player': 2, 'card': 'H_6'}, {'player': 3, 'card': 'H_7'}, {'player': 0, 'card': 'D_K'}], 'winner': 1, 'score': 10}, {'starter': 1, 'lead_suit': 'H', 'plays': [{'player': 1, 'card': 'H_9'}, {'player': 2, 'card': 'H_10'}, {'player': 3, 'card': 'H_J'}, {'player': 0, 'card': 'H_8'}], 'winner': 3, 'score': 10}, {'starter': 3, 'lead_suit': 'S', 'plays': [{'player': 3, 'card': 'S_2'}, {'player': 0, 'card': 'S_3'}, {'player': 1, 'card': 'S_4'}, {'player': 2, 'card': 'S_5'}], 'winner': 2, 'score': 5}, {'starter': 2, 'lead_suit': 'H', 'plays': [{'player': 2, 'card': 'H_A'}, {'player': 3, 'card': 'S_6'}, {'player': 0, 'card': 'H_Q'}, {'player': 1, 'card': 'H_K'}], 'winner': 2, 'score': 10}, {'starter': 2, 'lead_suit': 'S', 'plays': [{'player': 2, 'card': 'S_9'}, {'player': 3, 'card': 'S_10'}, {'player': 0, 'card': 'S_7'}, {'player': 1, 'card': 'S_8'}], 'winner': 3, 'score': 10}, {'starter': 3, 'lead_suit': 'S', 'plays': [{'player': 3, 'card': 'S_A'}, {'player': 0, 'card': 'S_J'}, {'player': 1, 'card': 'S_Q'}, {'player': 2, 'card': 'S_K'}], 'winner': 3, 'score': 10}], 'captured': [['D_4', 'D_5', 'D_2', 'D_3', 'C_A', 'D_6', 'D_7', 'D_8'], ['H_3', 'H_4', 'H_5', 'H_2', 'D_A', 'H_6', 'H_7', 'D_K'], ['S_2', 'S_3', 'S_4', 'S_5', 'H_A', 'S_6', 'H_Q', 'H_K'], ['C_2', 'C_3', 'C_4', 'C_5', 'C_9', 'C_6', 'C_7', 'C_8', 'C_K', 'C_10', 'C_J', 'C_Q', 'D_9', 'D_10', 'D_J', 'D_Q', 'H_9', 'H_10', 'H_J', 'H_8', 'S_9', 'S_10', 'S_7', 'S_8', 'S_A', 'S_J', 'S_Q', 'S_K']], 'scores': {'Alice': 5, 'Bob': 15, 'Carol': 15, 'Dave': 65}, 'winner': 'Dave'}
Hints
Hint 1
Parse each card once into a (suit position, rank position) pair so sorting hands and comparing ranks are simple lookups.
Hint 2
Deal by index: the card at position i of the permuted deck goes to seat i % 4. After sorting, the leader's card is the first element, and a follower's legal choice is the first card in their hand with the lead suit.
Hint 3
Track the current leader in a variable. After each trick pick the highest-ranked card of the lead suit, record the winner, and make them the next leader. Break overall ties by the smallest seat index.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a direct simulation. The rules fix every decision, so no search is needed.
- Validate and parse. Check the sizes and the permutation. Map each rank and suit to its position, and parse every card once into a
(suit_position, rank_position)pair. - Deal. Reorder the deck by
deal_order, then give the card at positionito seati % 4. Sort each hand by the parsed pair. - Play 13 tricks. Keep a
leadervariable. Visit seatsleader, leader+1, ...modulo 4.- The leader plays
hand[0], which sets the lead suit. - Each follower plays the first card of the lead suit in their sorted hand, which is the lowest rank of that suit. If they have none, they play
hand[0]. - Remove each played card from its hand.
- The leader plays
- Resolve the trick. Among the cards in the lead suit, the one with the highest rank position wins. Rank positions are unique within a suit, so there is no tie. Add the four cards to the winner's captured list in play order, add the points, record the trick, and set
leaderto the winner. - Pick the overall winner. Take the maximum score and choose the lowest seat index that attains it.
Why it is correct
Each step follows the stated rules. Sorting by (suit, rank) makes the first card of a hand the smallest card overall, and the first card of a given suit the lowest rank in that suit. Only lead-suit cards compete for the trick, so there is no trump logic. The leader update and the removal of played cards give the right state for the next trick. Because each hand has exactly 13 cards and each trick removes one card per player, all hands are empty after 13 tricks.
Complexity
Dealing and sorting cost O(52 log 52). Each of the 13 tricks scans at most 13 cards per player, so the total is a small constant amount of work: effectively O(1) for a fixed deck size. Space is O(52) for hands, captured cards, and the trick log.
Reference solution
from typing import List, Dict
def simulate_trick_game(deck: List[str], seat_names: List[str], first_leader: int,
rank_order: List[str], suit_order: List[str],
point_values: Dict[str, int], deal_order: List[int]) -> Dict[str, object]:
if len(seat_names) != 4 or len(deck) != 52:
raise ValueError("Expected four players and 52 cards")
if sorted(deal_order) != list(range(52)):
raise ValueError("deal_order must be a permutation of 0..51")
if len(set(deck)) != 52:
raise ValueError("Cards must be unique")
rank_index = {rank: i for i, rank in enumerate(rank_order)}
suit_index = {suit: i for i, suit in enumerate(suit_order)}
parsed = {}
for card in deck:
parts = card.split("_")
if len(parts) != 2 or parts[0] not in suit_index or parts[1] not in rank_index:
raise ValueError("Cards must use SUIT_RANK format")
parsed[card] = (suit_index[parts[0]], rank_index[parts[1]])
if len(rank_index) != 13 or len(suit_index) != 4:
raise ValueError("Expected 13 ranks and 4 suits")
hands = [[] for _ in range(4)]
shuffled = [deck[i] for i in deal_order]
for i, card in enumerate(shuffled):
hands[i % 4].append(card)
for hand in hands:
hand.sort(key=lambda card: parsed[card])
scores = [0] * 4
captured = [[] for _ in range(4)]
tricks = []
leader = first_leader
for _ in range(13):
played = []
lead_suit = None
for offset in range(4):
player = (leader + offset) % 4
hand = hands[player]
if lead_suit is None:
chosen = hand[0]
lead_suit = chosen.split("_")[0]
else:
legal = [card for card in hand if card.split("_")[0] == lead_suit]
chosen = legal[0] if legal else hand[0]
hand.remove(chosen)
played.append({"player": player, "card": chosen})
winner_play = max(
(p for p in played if p["card"].split("_")[0] == lead_suit),
key=lambda p: parsed[p["card"]][1],
)
winner = winner_play["player"]
trick_cards = [p["card"] for p in played]
trick_score = sum(point_values.get(card.split("_")[1], 0) for card in trick_cards)
captured[winner].extend(trick_cards)
scores[winner] += trick_score
tricks.append({
"starter": leader,
"lead_suit": lead_suit,
"plays": played,
"winner": winner,
"score": trick_score,
})
leader = winner
best = max(scores)
winning_player = min(i for i, s in enumerate(scores) if s == best)
return {
"hands": hands,
"tricks": tricks,
"captured": captured,
"scores": {seat_names[i]: scores[i] for i in range(4)},
"winner": seat_names[winning_player],
}