Overview
Offline mode simulates the multiplayer experience entirely client-side, making it perfect for practice, testing strategies, or playing when network connectivity is unavailable.Zero Network
Complete game logic runs locally in the browser
Full Rules
All standard Hex rules and win conditions enforced
Hot Seat Play
Two players can alternate turns on the same device
Game Export
Export move sequences for later analysis
Starting an Offline Game
Offline games are initiated from the game mode selection page:src/app/game_mode/page.tsx
l_{time_limit}_{board_size}, where:
l_prefix indicates local/offline modetime_limit: Time control setting (currently 0 for unlimited)board_size: Board dimension (5, 7, or 9)
Example:
/hex/l_0_9 creates an offline 9x9 game with unlimited time.OfflineHandler Architecture
TheOfflineHandler class mimics the WebSocket handler interface while running entirely locally:
src/app/hex/[gameId]/OfflineHandler.ts
- Interface Compatibility
- Instant Connection
- Local Packet Processing
The offline handler implements the same callback interface as the WebSocket handler:This allows the same UI code to work for both online and offline games.
src/app/hex/[gameId]/OfflineHandler.ts
GameInstance Class
The core game logic is encapsulated in theGameInstance class:
src/app/hex/[gameId]/OfflineHandler.ts
Grid Representation
The game board is represented as a 2D array:0: Empty cell1: Player 1 (Red) stone2: Player 2 (Blue) stone
src/app/hex/[gameId]/OfflineHandler.ts
Move Processing
When a player clicks a hexagon, the move is processed locally:src/app/hex/[gameId]/OfflineHandler.ts
1
Validation
Check if the selected cell is empty. Invalid moves are silently rejected.
2
Update Grid
Place the current player’s stone (1 for odd turns, 2 for even turns).
3
Notify UI
Send a MOVE_PLAYED packet to update the visual board.
4
Record Move
Add the move to the history for later export.
5
Check Win
Run win detection from the newly placed stone.
6
Advance Turn
Increment turn counter for next player.
Win Detection
The offline mode implements a flood-fill algorithm to detect winning connections:src/app/hex/[gameId]/OfflineHandler.ts
Win Detection Logic
Player 1 (Red) Win Condition
Player 1 (Red) Win Condition
Player 1 wins by connecting the left and right edges:
- Left edge:
hexagon[1] === 0 - Right edge:
hexagon[1] === board_size - 1
Player 2 (Blue) Win Condition
Player 2 (Blue) Win Condition
Player 2 wins by connecting the top and bottom edges:
- Top edge:
hexagon[0] === 0 - Bottom edge:
hexagon[0] === board_size - 1
Connected Components
Connected Components
The algorithm explores all hexagons connected to the most recent move:Each hexagon has up to 6 neighbors in the hex grid.
src/app/hex/[gameId]/OfflineHandler.ts
Winning Path Tracking
Winning Path Tracking
The algorithm tracks all hexagons in the winning path:This data is used to highlight the winning path in the UI (feature currently commented out in the codebase).
src/app/hex/[gameId]/OfflineHandler.ts
Game End Handling
When a win is detected or a player forfeits, the game ends:src/app/hex/[gameId]/OfflineHandler.ts
Move Export
Games can be exported as a move sequence:src/app/hex/[gameId]/OfflineHandler.ts
x + y * board_size
Example: On a 9x9 board, move at (3, 5) is encoded as
3 + 5 * 9 = 48Initialization Flow
The offline game initialization follows these steps:src/app/hex/[gameId]/page.tsx
1
Create Handler
Instantiate
OfflineHandler with game parameters and callbacks.2
Request Game
Send JOIN_GAME packet (processed locally, no network request).
3
Initialize Board
The
joinGameCallback receives the initial empty board and sets up the UI.4
Set Player Names
Display generic “Joueur 1” and “Joueur 2” labels.
5
Enable Interaction
Set click callback to allow both players to make moves.
Game Review Mode
After an offline game ends, it automatically enters review mode:src/app/hex/[gameId]/page.tsx
- Use a slider to navigate through moves
- Click cells to play alternate variations
- View AI analysis for each position
- See top move recommendations
Local Parameters
Offline games use simplified parameters:src/app/definitions.ts
ranked: Offline games are never ranked- Player IDs: Both players use dummy ID “1”
- Server-side state: No database persistence
Comparison: Online vs Offline
- Network
- Game State
- Features
- Use Cases
Online:
- WebSocket connection to server
- Network latency affects responsiveness
- Requires authentication
- Can reconnect after disconnect
- No network communication
- Instant response to moves
- No authentication needed
- Connection always “ready”
Limitations
Future Enhancements
Potential improvements for offline mode:Local Storage
Save games to browser localStorage for persistence
AI Opponent
Computer opponent using the analysis engine
Position Setup
Load custom positions for analysis
PGN Export
Export games in a standard notation format
Code Architecture Benefits
The offline handler demonstrates excellent software design:- Interface Segregation: Same
WebsocketCallbacksinterface for online and offline - Dependency Injection: Callbacks passed to constructor
- Single Responsibility:
GameInstancehandles logic,OfflineHandlerhandles communication - Open/Closed Principle: Easy to extend with new game modes
src/app/hex/[gameId]/OfflineHandler.ts
Next Steps
Multiplayer System
Compare with the online WebSocket implementation
AI Analysis
Use the analysis engine to improve your offline games