use alloc::vec::Vec;
use crate::game::{expanded_moves, oriented};
use crate::minimax::stand_pat;
use crate::result::Line;
use crate::{Bound, Game, Score, ScoredMove, SearchResult, Side, Stats};
#[must_use]
pub fn negamax<G: Game>(
game: &G,
position: &G::Position,
depth: u32,
) -> SearchResult<G::Move, G::Score> {
let mut stats = Stats::default();
let mut root_moves = Vec::new();
let (score, mut pv) = search(
game,
position,
(0, depth),
Side::Max,
Some(&mut root_moves),
&mut stats,
);
pv.reverse();
SearchResult {
score,
bound: Bound::Exact,
pv,
root_moves,
unexamined: 0,
depth,
proven: stats.horizon == 0,
stats,
}
}
fn search<G: Game>(
game: &G,
position: &G::Position,
(ply, depth): (u32, u32),
viewer: Side,
mut root_moves: Option<&mut Vec<ScoredMove<G::Move, G::Score>>>,
stats: &mut Stats,
) -> (G::Score, Line<G>) {
stats.visit(ply);
let moves = expanded_moves(game, position, depth, stats);
if moves.is_empty() {
return (oriented(game.evaluate(position), viewer), Vec::new());
}
let to_move = game.side_to_move(position);
let mut best =
stand_pat(game, position, depth).map(|(score, line)| (oriented(score, to_move), line));
for mv in moves {
let child = game.play(position, &mv);
let next = (ply + 1, depth.saturating_sub(1));
let (score, mut line) = search(game, &child, next, to_move, None, stats);
let max_view = oriented(score, to_move);
if let Some(root_moves) = root_moves.as_deref_mut() {
root_moves.push(ScoredMove::exact(mv.clone(), max_view));
}
if best.as_ref().is_none_or(|&(best, _)| score > best) {
line.push(ScoredMove::exact(mv, max_view));
best = Some((score, line));
}
}
let (score, line) = best.expect("a position with moves has a best child");
let score = if to_move == viewer {
score
} else {
score.negate()
};
(score, line)
}
#[cfg(test)]
mod tests {
use alloc::vec::Vec;
use crate::{Game, Side, minimax, negamax};
struct Extremes;
impl Game for Extremes {
type Position = Option<u8>;
type Move = u8;
type Score = i32;
fn side_to_move(&self, _: &Option<u8>) -> Side {
Side::Min
}
fn moves(&self, position: &Option<u8>, moves: &mut Vec<u8>) {
if position.is_none() {
moves.extend([1, 0]);
}
}
fn play(&self, _: &Option<u8>, &leaf: &u8) -> Option<u8> {
Some(leaf)
}
fn evaluate(&self, position: &Option<u8>) -> i32 {
i32::MIN + i32::from(position.unwrap_or(0))
}
}
#[test]
fn agrees_with_minimax_at_the_integer_minimum() {
let result = negamax(&Extremes, &None, 2);
assert_eq!(result.score, i32::MIN);
assert_eq!(result, minimax(&Extremes, &None, 2));
}
}