About This Solver

This is a perfect Connect 4 solver — it can find the optimal move for any position on a standard 7×6 board. Given any sequence of moves, the engine will tell you whether the current position is a win, draw, or loss, and by how many moves.

How to Use

  • Click any column (or the arrow above it) to drop a disc.
  • Click Show Solution to see the score for every column.
  • Switch to vs AI mode to play against the unbeatable engine.
  • Use Undo to take back the last move.
  • Use New Game to start over.

Score Meaning

ScoreMeaning
+NCurrent player wins in N moves (positive = win)
0Draw with perfect play from both sides
-NCurrent player loses in N moves (negative = loss)

More precisely, a score of +N means the current player can guarantee a win, finishing the game in at most (43 - N) / 2 total moves from now. A score of -N means the opponent wins in at most (42 - N) / 2 moves even with perfect play.

The Engine

The solver uses the Negamax algorithm with Alpha-Beta pruning, a Transposition Table, Iterative Deepening, and smart Move Ordering heuristics. This combination allows it to solve any position in milliseconds.

The C++ engine was written by Pascal Pons and is published under the GNU AGPL v3 license. Read the full step-by-step tutorial at blog.gamesolver.org.

Algorithm Steps

  1. Min-Max search
  2. Alpha-Beta pruning
  3. Move exploration order (center-first)
  4. Bitboard representation
  5. Transposition table (hash map)
  6. Iterative deepening
  7. Anticipate losing moves
  8. Better move ordering (score-based)
  9. Optimized transposition table
  10. Lower-bound transposition table