From 4bdcafa83c60b71688ca692373373e5a9c1bb5a5 Mon Sep 17 00:00:00 2001 From: mrw1593 Date: Tue, 20 Jul 2021 12:19:22 -0400 Subject: Added a minimax evaluation function --- ai/src/lib.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'ai/src/lib.rs') diff --git a/ai/src/lib.rs b/ai/src/lib.rs index 4e22708..c5b60f0 100644 --- a/ai/src/lib.rs +++ b/ai/src/lib.rs @@ -1,3 +1,5 @@ +use std::f32::NEG_INFINITY; + pub use model::{CheckersBitBoard, Move, PieceColor, PossibleMoves}; const KING_WORTH: u32 = 2; @@ -25,3 +27,26 @@ fn eval_position(board: CheckersBitBoard) -> f32 { 0.0 } } + +fn eval(depth: usize, board: CheckersBitBoard) -> f32 { + if depth == 0 { + eval_position(board) + } else { + let turn = board.turn(); + let mut best_eval = -NEG_INFINITY; + for current_move in PossibleMoves::moves(board) { + let board = unsafe {current_move.apply_to(board)}; + let current_eval = if board.turn() != turn { + -eval(depth - 1, board) + } else { + eval(depth - 1, board) + }; + + if best_eval < current_eval { + best_eval = current_eval; + } + } + + best_eval + } +} \ No newline at end of file -- cgit v1.2.3