add zobrist hashing, WIP transposition table

This commit is contained in:
jackjohn7 2026-05-04 20:49:31 -05:00
parent aa562d4ab6
commit 88131d9ab0
9 changed files with 362 additions and 137 deletions

100
src/ai.rs
View file

@ -142,106 +142,6 @@ pub fn alphabeta(mut game: Game, depth: u8, mut alpha: i8, mut beta: i8) -> (Boa
}
}
/// Using alpha-beta pruning with printing of intermediate data for debugging
/// and monitoring purposes.
pub fn alphabeta_with_printing(game: Game, depth: u8) -> (Board, i8) {
let (b, moves, eval) = alphabeta_with_printing_inner(game, depth, i8::MIN + 1, i8::MAX - 1);
println!("beep. boop. we assessed {} moves.", moves);
(b, eval)
}
/// Using alpha-beta pruning and the minimax algorithm, determine the best move
/// for a game with a recursion depth of `depth`.
///
/// We use a very simple evaluation heuristic: (Black squares - White squares).
fn alphabeta_with_printing_inner(
mut game: Game,
depth: u8,
mut alpha: i8,
mut beta: i8,
) -> (Board, usize, i8) {
// if we reach our maximum recursion depth, return evaluation
if depth == 0 {
return (0, 0, game.score().diff());
}
let moves = game.available();
if moves == 0 {
// if no move, skip and continue recursion
// this seems to technically introduce a bias against move-chains
// that include skips. I haven't found it to be a big deal in play.
game.skip();
return (
0,
0,
alphabeta_with_printing_inner(game, depth - 1, alpha, beta).2,
);
}
// just initially assume that the best move is no move at all. This will
// inevitably be corrected.
let mut best_move: Board = 0;
// we initially rank moves based on a couple basic heuristics:
// - corner pieces are best
// - edge pieces are great
// - others considered last
// This just allows us to prune the tree a bit more aggressively
// since we're considering the "best" moves first.
// We do this by mapping moves to ranked moves and then sorting.
let mut moves = explode_board(moves).map(MoveRank::from).collect::<Vec<_>>();
moves.sort();
let moves = moves
.into_iter()
.map(MoveRank::into_inner)
.collect::<Vec<_>>();
let mut num_moves = moves.len();
// I just establish a convention of maximizing for black and minimizing for white.
// I'm not sure if that's conventional or not, but it's what I chose.
match game.current_team {
Team::Black => {
for mv in moves {
let mut g = game.clone();
g.play(mv);
// maximize for the evaluation of subsequent moves
let (_, num_moves_prime, evaluation) =
alphabeta_with_printing_inner(g, depth - 1, alpha, beta);
num_moves += num_moves_prime;
// if our evaluated move is superior to the alpha, update
// it.
if evaluation > alpha {
alpha = evaluation;
best_move = mv;
};
// if our beta is less than alpha, prune the node.
if beta <= alpha {
break;
}
}
(best_move, num_moves, alpha)
}
Team::White => {
for mv in moves {
let mut g = game.clone();
g.play(mv);
// minimize for the evaluation of subsequent moves
let (_, num_moves_prime, evaluation) =
alphabeta_with_printing_inner(g, depth - 1, alpha, beta);
num_moves += num_moves_prime;
// if our evaluated move produces lower eval than the beta,
// update beta.
if evaluation < beta {
beta = evaluation;
best_move = mv;
};
// if our beta is less than alpha, prune the node.
if beta <= alpha {
break;
}
}
(best_move, num_moves, beta)
}
}
}
#[cfg(test)]
mod tests {
use super::*;