Skip to main content
BeeHex features a sophisticated AI analysis engine that evaluates board positions and recommends optimal moves using minimax search with custom heuristics. The engine runs entirely in the browser using Web Workers for parallel computation.

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

The Explorer class coordinates the analysis process:
src/app/hex/[gameId]/Algorithm.ts

Game Instance Types

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

The attributeScore function performs pathfinding to evaluate connection strength:
src/app/hex/[gameId]/Algorithm.ts
The algorithm recognizes “bridges” - connection patterns that cannot be broken by the opponent:
Bridges provide guaranteed connections without requiring additional moves.

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
Recommended moves are highlighted on the board:
src/app/hex/[gameId]/Grid.tsx
The top 4 recommended moves are color-coded on the board, with the best move shown in the brightest shade.

Grid Hashing

The engine uses base-36 encoding for efficient position storage:
src/app/hex/[gameId]/Algorithm.ts
This allows the 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
The ArrayCache class reuses array allocations:
src/app/hex/[gameId]/ArrayCache.ts
This reduces garbage collection pressure during deep searches.

Limitations and Future Improvements

The current implementation has some limitations:
  • Single Web Worker (multi-worker support is commented out)
  • No persistent transposition table
  • Limited depth on larger boards (9x9)
  • No opening book integration
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