Expand description
§ai-connect4
A Connect Four rules and search library built on two bitboards.
Play against it compiled to Wasm here: https://thegustafson.com/games/connect-four
§Self-play
The selfplay binary runs deterministic paired-color matches. Each opening is
played twice with the engines swapping Red and Yellow.
cargo run --release --bin selfplay -- \
--a maximum --b expert --games 12 --opening-plies 4 --seed 1The six built-in profiles are beginner, easy, medium, hard, expert,
and maximum. Use --a-depth, --a-nodes, and the corresponding --b-*
options to override their limits. Library users can read the same settings with
search_preset or SEARCH_PRESETS.
§Use it
Add the crate to your project:
[dependencies]
ai-connect4 = "0.1"use ai_connect4::{Algorithm, Move, Position, SearchLimits, search};
let mut position = Position::default();
position
.make_move("4".parse::<Move>().expect("valid column"))
.expect("legal move");
let result = search(
position,
Algorithm::TranspositionTable,
SearchLimits::depth(7),
);
assert!(result.best_move.is_some());The crate includes reversible moves, perft, alpha-beta search, move ordering, and an optional transposition table.
cargo test
cargo runLicensed under the MIT License.
Modules§
- verification
- Slow reference code and fixed oracle cases used to verify the engine.
Structs§
- Cell
- A physical board cell. Rank zero is the bottom row.
- Column
- One of Connect Four’s seven columns, indexed left to right.
- Engine
- One line-oriented engine session shared by tests and the native binary.
- Iteration
Summary - Iterative
Search Report - Result of iterative deepening under one cumulative budget.
- Move
- A move drops one disc into a column.
- Move
List - At most seven moves exist, so search never needs a heap-backed move list.
- Position
- Two 42-bit player boards inside Connect Four’s 7×7 sentinel layout.
- Root
Branch - Score
- A raw search score with terminal results deliberately outside the heuristic band. The type prevents an evaluation estimate from masquerading as a proven win or loss.
- Search
Limits - Search
Preset - A named depth and cumulative node budget for interactive play.
- Search
Report - Complete result of one fixed-depth search attempt.
- Search
Stats - Undo
Enums§
- Algorithm
- Controlled configurations used by search regression tests.
- Game
Result - Move
Error - Score
Kind - Side
Constants§
- HEIGHT
- SEARCH_
PRESETS - The built-in strength ladder, ordered from least to most search work.
- WIDTH
Functions§
- iterative_
search - Iteratively deepen while preserving the table and the last completed root move. A cumulative node budget is split exactly across iterations. If an iteration is interrupted, callers receive the prior complete result.
- perft
- Count legal move sequences to exactly
depthplies with the bitboard core. - search
- Search one fixed depth. All algorithms use the same terminal scores and
frontier evaluator, so changing
algorithmisolates search efficiency. - search_
preset - Finds a built-in strength preset by its lowercase name.