Skip to content

Feromond/connect_four_solver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Connect Four Solver

Cross-platform desktop app to play and analyze Connect Four. Comes with a fast AI powered by minimax and alpha–beta pruning, a clean GUI, and helpful hints about forced wins.

Quick look

connect-four-menu     
connect-four-board

Features

  • Play vs. AI or watch AI move: Choose who goes first on a simple setup screen.
  • Strong search: Minimax with alpha–beta pruning and a small transposition table speeds up analysis.
  • Smarter move ordering: Searches center columns first to prune more effectively and play more human-like moves.
  • Immediate-win checks: Detects mate-in-1 for the side to move and avoids root-level blunders that allow an immediate reply win.
  • Heuristic evaluation: Scores lines of four based on counts (2/3 in a row with empties) and emphasizes center control.
  • Polished UI: Modern dark theme, 3D disc effects, responsive window resizing, and clear status indicators.
  • Non-blocking AI: Calculation runs on a background thread, keeping the UI responsive and animations smooth.
  • Forced-win indicator: When applicable, clearly warns "AI detects forced win in N turns."

How it works (high level)

  • Game model: A Board tracks a 7×6 grid, current player, legal moves, wins/draws, and applies moves.
  • Search: The Solver runs minimax with alpha–beta pruning. It caches (position, depth) results in a HashMap to avoid recomputation.
  • Ordering: Candidate columns are ordered center-out to improve pruning and play strength.
  • Tactics: Before full search, it checks for immediate winning moves; at the root it filters out moves that allow the opponent an instant win.
  • Evaluation: For non-terminal nodes, a heuristic sums all 4-cell windows, rewarding threats (2/3 in a row with empties) and center occupancy; terminal wins/losses get large scores.
  • Threading: AI move calculation is offloaded to a separate thread to prevent UI freezing.
  • Depth: Default search depth is 9 plies; adjust in src/app.rs if you want a faster or stronger AI.

Build and run

  • Requirements: Latest stable Rust toolchain.
  • Build:
cargo build
  • Run:
cargo run

Controls

  • Setup: Pick who moves first (Human or AI).
  • Play: Click a column to drop a piece.
  • New Game returns to setup; Reset Board clears the current board.

Notes

  • On Windows, if icon.ico exists, it will be embedded; otherwise a transparent placeholder is used.
  • On macOS, the .app icon is provided via bundling metadata; the runtime uses a default icon.

Tech

  • Rust, egui/eframe for the native GUI
  • env_logger + log for simple logging

About

A GUI Connect Four Solver built in Rust

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages