← Questions

Shuffle, deal and sort a four-player card hand

0
OpenAIScale AICodingLast asked Aug 2026

You are building the core of a four-player card game. Model a standard 52-card deck and implement the deal.

Types to define

  • Suit: CLUBS, DIAMONDS, HEARTS, SPADES, in this order.
  • Rank: A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, in this order (ace low).
  • Card: one suit and one rank. Each of the 52 combinations appears exactly once in a deck.
  • Deck: holds the 52 cards and can shuffle them.
  • Player: has a name and a hand of cards.

Implement Game.deal_and_sort_hands() (dealAndSortHands in Java or C++). It must:

  1. Shuffle the deck uniformly at random. Every ordering must be equally likely.
  2. Deal every card so each of the four players receives the same number of cards (13 each).
  3. Sort each player's hand by suit (in the Suit order above), then by rank (in the Rank order above).
  4. Return each player's sorted hand (for example, a map from player name to list of cards).

Requirements

  • Ranks must sort by their order in the game, not as strings: 9 < 10 < J < Q < K.
  • Calling the method again must produce a fresh, complete deal. Cards must not carry over or pile up from the previous one.
  • Make the randomness injectable (for example, a seed) so tests are deterministic.
  • If the number of players does not divide the deck evenly, raise an error and leave the state unchanged.

Example

With players North, East, South, West and some seed, one possible result is:

North: 7C 2D 5D 6D 9D QD 2H 7H KH 2S 3S 4S JS
East:  6C QC 3D KD 4H 9H 10H AS 5S 6S 9S QS KS
...

Each hand has 13 cards and all 52 cards appear exactly once across the four hands. Clubs come before diamonds, then hearts, then spades, and inside a suit 10 sorts after 9.

Example (edge case)

With a single player, the hand is the whole deck in order: AC 2C ... KC AD ... KS. With three players, the call fails, because 52 is not divisible by 3.

Be ready for follow-ups: playing tricks where players must follow the led suit, ace-high or trump ordering, and other player counts.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding