Skip to content

Commit df4b8da

Browse files
committed
Large amounts of refactoring
Extract a lot of functions from the big function into their own files
1 parent 6c57b64 commit df4b8da

12 files changed

+378
-198
lines changed

src/algorithms/draw_checker.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::collections::HashMap;
2+
3+
use chess::Board;
4+
5+
pub fn uncount_board(board_played_times_prediction: &mut HashMap<Board, u32>, new_board: &Board) {
6+
board_played_times_prediction.insert(
7+
// TODO Hash it to avoid copying, we need a good hash function for Board
8+
*new_board,
9+
*board_played_times_prediction.get(&new_board).unwrap_or(&0) - 1,
10+
);
11+
}
12+
13+
pub fn count_board(board_played_times_prediction: &mut HashMap<Board, u32>, new_board: &Board) {
14+
board_played_times_prediction.insert(
15+
// TODO Hash it to avoid copying, we need a good hash function for Board
16+
*new_board,
17+
*board_played_times_prediction.get(&new_board).unwrap_or(&0) + 1,
18+
);
19+
}

src/algorithms/eval.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::algorithms::utils::Evaluation;
2+
use chess::{Board, Color};
3+
4+
/// Investigate the reason for there being no legal moves, and return a score based on that.
5+
pub fn eval_no_legal_moves(board: &Board) -> f32 {
6+
if board.checkers().popcnt() == 0 {
7+
// Is Stalemate, no checking pieces
8+
return 0.;
9+
}
10+
11+
// If we arrive at here while it is checkmate, then we know that the side playing
12+
// has been checkmated.
13+
return if board.side_to_move() == Color::White {
14+
f32::MIN
15+
} else {
16+
f32::MAX
17+
};
18+
}
19+
pub fn new_eval_is_better(maximise: bool, old: &Evaluation, new: &Evaluation) -> bool {
20+
new.eval.is_some()
21+
&& (old.eval.is_none()
22+
|| maximise && new.eval.unwrap() > old.eval.unwrap()
23+
|| !maximise && new.eval.unwrap() < old.eval.unwrap())
24+
}

src/algorithms/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
mod draw_checker;
2+
mod eval;
13
pub(crate) mod the_algorithm;
24
pub(crate) mod utils;

0 commit comments

Comments
 (0)