aboutsummaryrefslogtreecommitdiff
path: root/src/board/engine.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/board/engine.rs')
-rw-r--r--src/board/engine.rs42
1 files changed, 31 insertions, 11 deletions
diff --git a/src/board/engine.rs b/src/board/engine.rs
index 61d9fda..9b0e36c 100644
--- a/src/board/engine.rs
+++ b/src/board/engine.rs
@@ -17,7 +17,7 @@ impl Board {
result.leaf_nodes = 1;
return result;
}
- let color = self.color_to_move();
+ let color = self.color();
let moves = self.generate_pseudolegal_moves(color);
@@ -73,24 +73,37 @@ impl Board {
result
}
- pub fn evaluate(&self) -> f32 {
+
+ /// Compute material advantage relative to the current player
+ pub fn material_advantage(&self) -> f32 {
let mut eval = 0f32;
- let pieces = self.pieces_by_color(self.color());
- eval += pop_count(pieces[PieceType::Pawn as usize]) as f32;
- eval += pop_count(pieces[PieceType::Bishop as usize]) as f32 * 3.;
- eval += pop_count(pieces[PieceType::Knight as usize]) as f32 * 3.;
- eval += pop_count(pieces[PieceType::Rook as usize]) as f32 * 4.5;
- eval += pop_count(pieces[PieceType::Queen as usize]) as f32 * 9.;
+ for (piece_index, bitboard) in self.pieces.iter().enumerate() {
+ let piece_type = PieceType::from(piece_index);
+ let sign = if Color::from_piece(piece_type) == self.color() {
+ 1.
+ } else {
+ -1.
+ };
+ let multiplier = match piece_type.without_color() {
+ PieceType::Pawn => 1.,
+ PieceType::Bishop => 3.,
+ PieceType::Knight => 3.,
+ PieceType::Rook => 4.5,
+ PieceType::Queen => 9.,
+ PieceType::King => 0.,
+ _ => panic!("Piece should be without color"),
+ };
+
+ eval += sign * multiplier * pop_count(*bitboard) as f32;
+ }
eval
}
pub fn negamax_search(&mut self, mut alpha: f32, beta: f32, depth_left: u8) -> f32 {
let color = Color::from(self.ply as u8 % 2);
- self.print();
-
if depth_left == 0 {
- return self.evaluate();
+ return self.material_advantage();
}
let moves = self.generate_pseudolegal_moves(color);
@@ -156,4 +169,11 @@ mod tests {
let eval = board.negamax_search(-INFINITY, INFINITY, 4);
println!("{}", eval);
}
+
+ #[test]
+ fn material_advantage() {
+ let board = Board::new();
+ assert_eq!(board.material_advantage(), 0.0, "Material advantage should be 0 at starting position");
+
+ }
}