Game State Management
The game engine is built around three core classes that manage game state at different levels of complexity.GameInstance Class
Location:src/app/hex/[gameId]/GameInstance.ts
The GameInstance class represents an active game session with full player information.
Grid Representation
The game board is stored as a 2D array where:0= empty cell1= red player (connects left to right)2= blue player (connects top to bottom)
Key Methods
- Move Validation
- Turn Logic
- State Updates
AI Engine Architecture
BeeHex implements a sophisticated minimax-based AI with parallel exploration using Web Workers.Class Hierarchy
SimpleGameInstance
Location:src/app/hex/[gameId]/Algorithm.ts:7
Lightweight representation of a game state without evaluation.
Move Generation
Immutable Move Application
The engine uses immutable state updates - each move creates a new game instance.
ScoredGameInstance
Location:src/app/hex/[gameId]/Algorithm.ts:95
Extends SimpleGameInstance with position evaluation and tree structure.
Score Propagation
Scores propagate up the tree using minimax logic:Score Class
Location:src/app/hex/[gameId]/Algorithm.ts:783
Represents position evaluation with special handling for forced wins.
Score Semantics
- Heuristic Scores
- Win Countdown
- Comparison Logic
When
isWinCountdown = false, the score represents a heuristic evaluation:- Positive values: Red (player 1) is winning
- Negative values: Blue (player 2) is winning
- Magnitude: Distance from victory (lower = better for that player)
Position Evaluation Heuristic
Location:src/app/hex/[gameId]/Algorithm.ts:526
The basicHeuristic function evaluates positions using a sophisticated pathfinding algorithm.
Algorithm Overview
1
Calculate path costs
For each player, compute minimum cost to connect their edges using Dijkstra-style search.
2
Detect forced wins
If cost = 0, the player has already won. Return win countdown score.
3
Compute differential
Return
player2_cost - player1_cost as heuristic evaluation.Path Cost Calculation
Bridge Detection
A “bridge” is a connection pattern that allows jumping over opponent pieces without additional cost.
Explorer and Web Workers
Location:src/app/hex/[gameId]/Algorithm.ts:281
The Explorer class orchestrates parallel game tree search using Web Workers.
Explorer Architecture
Exploration Strategy
1
Initialize root position
Create
ScoredGameInstance from current board state and evaluate with heuristic.2
Populate main instances
Generate all first-level moves as
MainScoredGameInstance objects (special class that reports back to UI).3
Explore second level
For each main instance, explore opponent replies to depth 2.
4
Send to worker
Identify best incomplete branch and send to Web Worker for deep exploration (2 more levels).
5
Process worker results
Receive explored game tree, reconstruct instance graph, propagate scores upward.
6
Update UI
Return top 10 moves with their scores and optimal continuations.
7
Repeat
Continue sending next best incomplete branch until tree is fully explored.
Worker Communication
Worker Code:src/app/hex/[gameId]/AlgorithmWorker.ts
Performance Monitoring
Recommended Move Format
Next Steps
WebSocket Protocol
Learn about the multiplayer communication protocol and packet specifications