Connect Four

July 20, 2026 ยท View on GitHub

A Rust port of John Tromp's Fhourstones Connect Four solver + a terminal UI for interactive play.

Connect-four [4] is a two player game, and the TUI allows the players to be drawn from:

  • Human
  • Perfect (the solver) [2]
  • Minimax [5]
  • Mcts (Monte Carlo Tree Search) [6]

Gif Image interactive play

Verification

Checked against the original C (v3.2) and Java (v3.1) reference implementations, which agree with each other exactly:

Position (moves played)ScoreNodes searched
45461667Win51,596
35333571Loss8,716,732
13333111Draw169,704,432
(empty board, full 42-ply solve)Win1,479,113,766

Run the exact-node-count regression tests with:

cargo test --release --lib

Usage: solve (benchmark / analysis)

Reads one position per line from stdin, as a string of column digits (1โ€“7, left to right); other characters are ignored, and an empty line solves the starting position. This matches the original Fhourstones CLI input format, so existing test/benchmark input files work unchanged.

echo "4453" | cargo run --release --bin solve

For each position it reports the game-theoretic score, log2 of positions stored in the transposition table ("work"), total nodes searched, search speed, and a histogram of stored transposition-table entries by score.

Usage: play (interactive)

The play terminal app allows two players to play interactively. The flags --player1 and --player2 configures who plays each side. Choices are human, perfect (the solver), minimax (alpha-beta negamax [5]) and mcts (Monte Carlo Tree Search [6]).

cargo run --release --bin play                                       # Human (red) vs perfect-play AI (yellow)
cargo run --release --bin play -- --player1 human --player2 human    # two humans
cargo run --release --bin play -- --player1 perfect --player2 perfect  # watch the perfect solver play itself
cargo run --release --bin play -- --player1 minimax --depth 6 --player2 mcts --mcts-millis 1000
cargo run --release --bin play -- --moves 4453 --player2 perfect     # start from a given position

References

  1. John Tromp's Connect Four page
  2. Fhourstones Benchmark
  3. "The Complete Book of Connect Four", James Dow Allen
  4. Wikipedia's Conect Four
  5. Minimax
  6. Monte Carlo Tree Search