Overview
TheGame class is the central domain model that encapsulates the complete state and business logic of a Blackjack game. It manages the game flow, player and dealer hands, deck state, and determines game outcomes according to standard Blackjack rules.
Class Structure
Fields
Unique identifier for the game session. Generated automatically using UUID when a new game is created.
Identifier of the player participating in this game. Links the game to a specific player record.
The deck of cards used in this game. Tracks remaining cards after each draw operation. Immutable - each draw returns a new Deck instance.
The player’s current hand containing all dealt cards. Updated with each hit action.
The dealer’s current hand containing all dealt cards. Updated automatically when player stands.
Current state of the game. Possible values:
IN_PROGRESS- Game is active and awaiting player actionPLAYER_BUST- Player’s hand exceeded 21 (intermediate state)DEALER_BUST- Dealer’s hand exceeded 21 (intermediate state)PLAYER_WINS- Player won the gameDEALER_WINS- Dealer won the gamePUSH- Tie game (both hands have equal value)
Business Logic
Game Initialization
When a new game is created, the following occurs:- A shuffled 52-card deck is prepared
- Four cards are drawn in alternating fashion:
- Card 1 → Player
- Card 2 → Dealer
- Card 3 → Player
- Card 4 → Dealer
- Natural Blackjacks (21 on initial deal) are automatically resolved
Natural Blackjack Resolution
The game automatically checks for natural Blackjacks (21 on initial two cards):- Both have Blackjack → Game ends in
PUSH(tie) - Player has Blackjack → Player wins immediately
- Dealer has Blackjack → Dealer wins immediately
- Neither has Blackjack → Game continues normally
Player Actions
Hit
Draws one additional card for the player:- Throws
InvalidMoveExceptionif game is notIN_PROGRESS - Automatically checks for bust (score > 21)
- If player busts, game transitions to
DEALER_WINS
Stand
Player holds their current hand and triggers dealer play:- Throws
InvalidMoveExceptionif game is notIN_PROGRESS - Dealer automatically draws cards following standard rules
- Final outcome is determined by comparing scores
Dealer Logic
The dealer follows standard casino rules:- Must hit on any total of 16 or less
- Must stand on any total of 17 or more
Outcome Resolution
Final game outcomes are determined by:- Player Bust → Dealer wins
- Dealer Bust → Player wins
- Player score > Dealer score → Player wins
- Dealer score > Player score → Dealer wins
- Equal scores → Push (tie)
Immutability
TheGame class follows an immutable design pattern:
- All fields are
final - The class is declared
final(cannot be subclassed) - All state-changing operations return a new
Gameinstance - This ensures thread safety and prevents unintended side effects
Factory Methods
Create New Game
Rehydrate from Storage
Accessor Methods
Validation Rules
- All fields are non-null (enforced via
Objects.requireNonNull()) - Player actions (
hit,stand) can only be performed whenstatusisIN_PROGRESS - Attempting actions on completed games throws
InvalidMoveException