diff options
| author | mrw1593 <botahamec@outlook.com> | 2021-07-20 12:19:22 -0400 |
|---|---|---|
| committer | mrw1593 <botahamec@outlook.com> | 2021-07-20 12:19:22 -0400 |
| commit | 4bdcafa83c60b71688ca692373373e5a9c1bb5a5 (patch) | |
| tree | ca822424fbb0e9b4b3bbb16e05b0b22f8c9a143c /ai/src/lib.rs | |
| parent | 244786ead1ab1549a8ccc723addda90a7481daea (diff) | |
Added a minimax evaluation function
Diffstat (limited to 'ai/src/lib.rs')
| -rw-r--r-- | ai/src/lib.rs | 25 |
1 files changed, 25 insertions, 0 deletions
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 |
