2024-10-26 19:53:20 -04:00
|
|
|
/*
|
|
|
|
|
|
|
|
This file is part of chess_inator.
|
|
|
|
|
|
|
|
chess_inator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
chess_inator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with chess_inator. If not, see https://www.gnu.org/licenses/.
|
|
|
|
|
|
|
|
Copyright © 2024 dogeystamp <dogeystamp@disroot.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
//! Game-tree search.
|
|
|
|
|
|
|
|
use crate::eval::{Eval, EvalInt};
|
2024-11-01 21:58:38 -04:00
|
|
|
use crate::movegen::{Move, MoveGen, ToUCIAlgebraic};
|
2024-10-26 19:53:20 -04:00
|
|
|
use crate::Board;
|
|
|
|
use std::cmp::max;
|
|
|
|
|
2024-10-26 21:05:51 -04:00
|
|
|
// min can't be represented as positive
|
|
|
|
const EVAL_WORST: EvalInt = -(EvalInt::MAX);
|
2024-10-27 17:34:41 -04:00
|
|
|
const EVAL_BEST: EvalInt = EvalInt::MAX;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_eval_int {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eval_worst_best_symm() {
|
|
|
|
// int limits will bite you if you don't test this
|
|
|
|
assert_eq!(EVAL_WORST, -EVAL_BEST);
|
|
|
|
assert_eq!(-EVAL_WORST, EVAL_BEST);
|
|
|
|
}
|
|
|
|
}
|
2024-10-26 21:05:51 -04:00
|
|
|
|
2024-11-02 16:05:04 -04:00
|
|
|
/// Eval in the context of search.
|
|
|
|
#[derive(PartialEq, Eq, Clone, Copy)]
|
|
|
|
enum SearchEval {
|
|
|
|
/// Mate in |n| - 1 half moves, negative for own mate.
|
|
|
|
Checkmate(i8),
|
|
|
|
/// Centipawn score.
|
|
|
|
Centipawns(EvalInt),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SearchEval {
|
|
|
|
/// Flip side, and increment the "mate in n" counter.
|
|
|
|
fn increment(self) -> Self {
|
|
|
|
match self {
|
|
|
|
SearchEval::Checkmate(n) => {
|
|
|
|
debug_assert_ne!(n, 0);
|
|
|
|
if n < 0 {
|
|
|
|
Self::Checkmate(-(n - 1))
|
|
|
|
} else {
|
|
|
|
Self::Checkmate(-(n + 1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SearchEval::Centipawns(eval) => Self::Centipawns(-eval),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SearchEval> for EvalInt {
|
|
|
|
fn from(value: SearchEval) -> Self {
|
|
|
|
match value {
|
|
|
|
SearchEval::Checkmate(n) => {
|
|
|
|
debug_assert_ne!(n, 0);
|
|
|
|
if n < 0 {
|
|
|
|
EVAL_WORST + EvalInt::from(n)
|
|
|
|
} else {
|
|
|
|
EVAL_BEST - EvalInt::from(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SearchEval::Centipawns(eval) => eval,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for SearchEval {
|
|
|
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
|
|
|
let e1 = EvalInt::from(*self);
|
|
|
|
let e2 = EvalInt::from(*other);
|
|
|
|
e1.cmp(&e2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for SearchEval {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Search the game tree to find the absolute (positive good) move and corresponding eval for the
|
|
|
|
/// current player.
|
2024-10-27 17:34:41 -04:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * board: board position to analyze.
|
|
|
|
/// * depth: how deep to analyze the game tree.
|
|
|
|
/// * alpha: best score (absolute, from current player perspective) guaranteed for current player.
|
|
|
|
/// * beta: best score (absolute, from current player perspective) guaranteed for other player.
|
2024-11-02 16:05:04 -04:00
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
|
|
|
/// The best move, and its corresponding absolute eval for the current player.
|
|
|
|
fn minmax(
|
|
|
|
board: &mut Board,
|
|
|
|
depth: usize,
|
|
|
|
alpha: Option<EvalInt>,
|
|
|
|
beta: Option<EvalInt>,
|
|
|
|
) -> (Option<Move>, SearchEval) {
|
2024-10-27 17:34:41 -04:00
|
|
|
// default to worst, then gradually improve
|
|
|
|
let mut alpha = alpha.unwrap_or(EVAL_WORST);
|
|
|
|
// our best is their worst
|
|
|
|
let beta = beta.unwrap_or(EVAL_BEST);
|
|
|
|
|
2024-10-26 19:53:20 -04:00
|
|
|
if depth == 0 {
|
|
|
|
let eval = board.eval();
|
|
|
|
match board.turn {
|
2024-11-02 16:05:04 -04:00
|
|
|
crate::Color::White => return (None, SearchEval::Centipawns(eval)),
|
|
|
|
crate::Color::Black => return (None, SearchEval::Centipawns(-eval)),
|
2024-10-26 19:53:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-01 21:58:38 -04:00
|
|
|
let mvs: Vec<_> = board.gen_moves().into_iter().collect();
|
2024-10-26 19:53:20 -04:00
|
|
|
|
2024-11-02 16:05:04 -04:00
|
|
|
let mut abs_best = SearchEval::Centipawns(EVAL_WORST);
|
|
|
|
let mut best_move: Option<Move> = None;
|
2024-10-26 21:05:51 -04:00
|
|
|
|
|
|
|
if mvs.is_empty() {
|
|
|
|
if board.is_check(board.turn) {
|
2024-11-02 16:05:04 -04:00
|
|
|
return (None, SearchEval::Checkmate(-1));
|
2024-10-26 21:05:51 -04:00
|
|
|
} else {
|
|
|
|
// stalemate
|
2024-11-02 16:05:04 -04:00
|
|
|
return (None, SearchEval::Centipawns(0));
|
2024-10-26 21:05:51 -04:00
|
|
|
}
|
|
|
|
}
|
2024-10-26 19:53:20 -04:00
|
|
|
|
|
|
|
for mv in mvs {
|
|
|
|
let anti_mv = mv.make(board);
|
2024-11-02 16:05:04 -04:00
|
|
|
let (_best_continuation, score) = minmax(board, depth - 1, Some(-beta), Some(-alpha));
|
|
|
|
let abs_score = score.increment();
|
|
|
|
if abs_score >= abs_best {
|
|
|
|
abs_best = abs_score;
|
|
|
|
best_move = Some(mv);
|
|
|
|
}
|
|
|
|
alpha = max(alpha, abs_best.into());
|
2024-10-26 19:53:20 -04:00
|
|
|
anti_mv.unmake(board);
|
2024-11-02 16:05:04 -04:00
|
|
|
if alpha >= beta {
|
2024-10-27 17:34:41 -04:00
|
|
|
// alpha-beta prune.
|
|
|
|
//
|
|
|
|
// Beta represents the best eval that the other player can get in sibling branches
|
|
|
|
// (different moves in the parent node). Alpha >= beta means the eval here is _worse_
|
|
|
|
// for the other player, so they will never make the move that leads into this branch.
|
|
|
|
// Therefore, we stop evaluating this branch at all.
|
|
|
|
break;
|
|
|
|
}
|
2024-10-26 19:53:20 -04:00
|
|
|
}
|
|
|
|
|
2024-11-02 16:05:04 -04:00
|
|
|
(best_move, abs_best)
|
2024-10-26 19:53:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Find the best move.
|
|
|
|
pub fn best_move(board: &mut Board) -> Option<Move> {
|
2024-11-02 16:05:04 -04:00
|
|
|
let (mv, eval) = minmax(board, 5, None, None);
|
|
|
|
match eval {
|
|
|
|
SearchEval::Checkmate(n) => println!("info score mate {}", n / 2),
|
|
|
|
SearchEval::Centipawns(eval) => println!("info score cp {eval}"),
|
|
|
|
}
|
|
|
|
mv
|
2024-10-26 19:53:20 -04:00
|
|
|
}
|