Connect Four Browser Game
The definitive Connect Four implementation featuring a 64-bit Bitboard AI, real-time win probability analysis, procedural audio, and rigorous state management.
About
This tool is a high-fidelity implementation of the classic "Four-in-a-Row" strategy game, engineered for both casual play and algorithmic analysis. Unlike standard browser games, this engine utilizes 64-bit Bitboards (using JavaScript BigInt) to represent the game state, allowing for ultra-fast win detection and AI calculation speeds surpassing 20,000 nodes per second on standard hardware.
The core solver employs a Minimax Algorithm with Alpha-Beta Pruning and Iterative Deepening. This allows the AI to look ahead up to 10 plies (moves) into the future, evaluating positions based on material advantage, center control, and threats. The application also features a real-time Win Probability gauge, visualizing the engine's confidence in the current board state.
Note: Connect Four is a "solved game". With perfect play, the first player (Red) can always force a win. This engine approximates perfect play within the browser's execution time limits.
Formulas
The engine uses bitwise operations for instant win detection. A board state is mapped to a bitmask. For a board with R rows and C columns, we check for wins using bit shifts:
Horizontal Win Check:
m = pos & (pos >> 7)
IF (m & (m >> 14)) != 0 β TRUE
Heuristic Scoring Function:
Score(S) = βlines (w4ΓN4 + w3ΓN3 + w2ΓN2)
Where Nk is the count of k-length chains open on at least one side.
Reference Data
| Dimension | Value | Description |
|---|---|---|
| Grid Size | 7 Γ 6 | Standard 7 columns, 6 rows layout. |
| State Space | 4.5 Γ 1012 | Total number of legal positions (approx 4.5 trillion). |
| Perfect Game | 41 Moves | Maximum length of a game with optimal play on both sides. |
| First Player | Advantage | Mathematically proven win for Player 1 (Red). |
| Algorithm | Minimax | Recursive decision rule used for AI. |
| Heuristic | Ξ±-Ξ² Pruning | Optimization to eliminate irrelevant search branches. |
| Bitboard | 64-bit | State storage using binary integers for O(1) ops. |