Overview
The AI engine provides real-time position analysis during game review, helping players understand optimal strategies and evaluate their move choices.Minimax Search
Multi-threaded game tree exploration with alpha-beta pruning concepts
Position Scoring
Custom heuristic evaluating connection strength and bridge formations
Move Ranking
Top 4 moves displayed with evaluation scores and optimal continuations
Win Detection
Forced win sequences identified with exact move count
Core Architecture
Explorer Class
TheExplorer class coordinates the analysis process:
src/app/hex/[gameId]/Algorithm.ts
Game Instance Types
- SimpleGameInstance
- ScoredGameInstance
- MainScoredGameInstance
Base class representing a board position:
src/app/hex/[gameId]/Algorithm.ts
Position Evaluation
Score Type
The scoring system distinguishes between heuristic evaluations and forced wins:src/app/hex/[gameId]/Algorithm.ts
When
isWinCountdown is true, the score represents the number of moves until forced victory (positive for player 1, negative for player 2).Basic Heuristic
The default evaluation function analyzes connection strength:src/app/hex/[gameId]/Algorithm.ts
Connection Analysis
TheattributeScore function performs pathfinding to evaluate connection strength:
src/app/hex/[gameId]/Algorithm.ts
Bridge Detection
Bridge Detection
The algorithm recognizes “bridges” - connection patterns that cannot be broken by the opponent:Bridges provide guaranteed connections without requiring additional moves.
Minimax Search
Tree Exploration
The engine uses minimax with iterative deepening:src/app/hex/[gameId]/Algorithm.ts
Score Propagation
When a leaf node is evaluated, scores propagate up the tree:src/app/hex/[gameId]/Algorithm.ts
Web Worker Integration
The engine uses Web Workers for parallel computation without blocking the UI:src/app/hex/[gameId]/Algorithm.ts
Worker Communication
1
Send Exploration Request
Main thread identifies next position to explore:
2
Worker Explores
Worker expands the game tree and evaluates leaf nodes.
3
Return Results
Worker sends back explored instances:
4
Update UI
Main thread integrates results and updates move recommendations.
Performance Tracking
The explorer logs search performance:src/app/hex/[gameId]/Algorithm.ts
On modern hardware, the engine typically explores 5,000-15,000 positions per second.
Move Recommendations
The explorer maintains a ranked list of best moves:src/app/hex/[gameId]/Algorithm.ts
UI Integration
The game interface displays analysis results in real-time:src/app/hex/[gameId]/page.tsx
src/app/hex/[gameId]/Grid.tsx
Grid Hashing
The engine uses base-36 encoding for efficient position storage:src/app/hex/[gameId]/Algorithm.ts
instances Map to detect transpositions and avoid re-evaluating identical positions.
Memory Optimization
The engine implements several memory-saving techniques:src/app/hex/[gameId]/Algorithm.ts
Array Caching
Array Caching
The This reduces garbage collection pressure during deep searches.
ArrayCache class reuses array allocations:src/app/hex/[gameId]/ArrayCache.ts
Limitations and Future Improvements
Future enhancements could include:- Parallel worker pool for faster search
- Monte Carlo Tree Search integration
- Neural network evaluation
- Endgame tablebase
Next Steps
Offline Mode
Learn about local gameplay implementation
Game Modes
Explore different ways to play