Skip to main content

Crate ai_connect4

Crate ai_connect4 

Source
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 1

The 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 run

Licensed 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.
IterationSummary
IterativeSearchReport
Result of iterative deepening under one cumulative budget.
Move
A move drops one disc into a column.
MoveList
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.
RootBranch
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.
SearchLimits
SearchPreset
A named depth and cumulative node budget for interactive play.
SearchReport
Complete result of one fixed-depth search attempt.
SearchStats
Undo

Enums§

Algorithm
Controlled configurations used by search regression tests.
GameResult
MoveError
ScoreKind
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 depth plies with the bitboard core.
search
Search one fixed depth. All algorithms use the same terminal scores and frontier evaluator, so changing algorithm isolates search efficiency.
search_preset
Finds a built-in strength preset by its lowercase name.