Project
The Royal Gambit
Chess × Blackjack × Game AI
A browser-based chess variant where players can risk active material in blackjack to recover captured pieces. I built the custom rule engine, Monte Carlo probability model, Stockfish-backed bot and expected-value decision system, alongside online multiplayer and deployment.
Chess with a recovery mechanic
Standard chess rules apply throughout. The variant adds a single option: when a player falls far enough behind in material, they can risk active pieces in a blackjack challenge to recover captured ones. That one rule adds a second layer of decision-making around:
Standard legality plus a piece-state model
chess.js handles standard chess legality. On top of it I maintain a separate piece-state model for the Royal Gambit-specific rules.
- Legal chess moves
- Check and checkmate
- Castling
- Promotion
- Stalemate
- Threefold repetition
- Original square
- Captured state
- Promotion state
- Recovery eligibility
- Temporary protection
Staking active pieces to recover material
When a player is sufficiently behind, they select captured pieces to recover and choose active pieces to stake. Before a challenge is allowed, the system checks:
- Material deficit
- Recovery value
- Stake value
- Available captured pieces
- Original-square availability
- King safety
- Blackjack attempt limits
A piece cannot be staked if removing it would expose its own king to check.
Normal blackjack is limited to five attempts per player, making blackjack itself a limited strategic resource.
Estimating blackjack odds by simulation
A separate blackjack simulation handles card generation, ace values, player hit/stand behaviour, dealer behaviour and win/loss/tie resolution. Monte Carlo simulation then plays many rounds to estimate the outcome probabilities for a given state:
The bot uses these probabilities when deciding whether a blackjack gamble is worthwhile. Results are cached, so identical states are not repeatedly simulated.
Comparing every option by expected value
The bot compares the expected value of continuing with a normal chess move, taking a blackjack recovery gamble, or using the King's Gamble, and picks the best.
best action = max(chess move EV, blackjack EV, king gamble EV)The blackjack decision depends on:
In-browser evaluation via a Web Worker
Stockfish runs inside the browser through a Web Worker.
More than raw engine strength
Difficulty levels change more than Stockfish's strength. Each level also varies its evaluation and gambling behaviour:
The displayed Elo-style numbers are difficulty levels / approximate strength labels, not officially calibrated Elo ratings.
A recovery mode from a lone king
When a player is reduced to only their king, they can enter a special blackjack-based recovery mode. Blackjack wins generate recovery points that can be spent on recovering material. A blackjack loss does not directly end the chess game — checkmate remains the actual loss condition. The bot evaluates this option separately from a normal move.
A small combinatorial problem inside the game
Each gamble poses two questions — what should I recover? and what should I risk? — which the bot solves as a small combinatorial search:
- Identify legal recovery targets
- Check original-square availability
- Rank recovery options
- Find legal stake pieces
- Reject king-unsafe stakes
- Generate stake combinations
- Compare combination cost
- Select a valid low-cost stake
Two networking paths
Local / LAN
- Room creation
- White / Black role assignment
- Spectators
- Presence
- Game-state synchronisation
Deployed (Vercel)
Friend rooms run on serverless routes with optional Vercel KV / Upstash Redis. The API includes input validation and payload-size limits.
Interactive board and interface
Three themes, with preference persisted locally: