KatInterview

Simulate a Four-Seat Trick-Taking Card Game with Scoring

Data Structure DesignAuto-graded · 12 test cases
MediumAuto-gradedData Structure Designsimulationstate-managementsorting

Last 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 form SUIT_RANK, such as "H_10" or "S_A".
  • seat_names: four player names. The player at index i sits in seat i.
  • 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 of 0..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_order is not a permutation of 0..51.
  • Cards are not unique.
  • A card is not in SUIT_RANK form 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_values keys 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.