stats: use new structures

This commit is contained in:
dece 2020-06-19 20:00:33 +02:00
parent 4cea0e34e9
commit 5b678dd595

View file

@ -1,7 +1,7 @@
//! Board statistics used for heuristics. //! Board statistics used for heuristics.
use crate::board::*; use crate::board::*;
use crate::rules::GameState; use crate::rules::{GameState, get_player_moves};
/// Storage for board pieces stats. /// Storage for board pieces stats.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -27,6 +27,20 @@ impl BoardStats {
} }
} }
/// Create two new BoardStats objects from the board, for both sides.
///
/// The playing color will have its stats filled in the first
/// BoardStats object, its opponent in the second.
pub fn new_from(board: &Board, game_state: &GameState) -> (BoardStats, BoardStats) {
let mut stats = (BoardStats::new(), BoardStats::new());
let mut gs = game_state.clone();
stats.0.compute(board, &gs);
gs.color = opposite(gs.color);
stats.1.compute(board, &gs);
stats
}
/// Reset all stats to 0.
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.num_pawns = 0; self.num_pawns = 0;
self.num_bishops = 0; self.num_bishops = 0;
@ -48,86 +62,88 @@ impl BoardStats {
self.reset(); self.reset();
let color = game_state.color; let color = game_state.color;
// Compute mobility for all pieces. // Compute mobility for all pieces.
self.mobility = rules::get_player_moves(board, game_state, true).len() as i32; self.mobility = get_player_moves(board, game_state, true).len() as i32;
// Compute amount of each piece. // Compute amount of each piece.
for (piece, p) in get_piece_iterator(board) { for file in 0..8 {
let (pos_f, pos_r) = p; for rank in 0..8 {
if piece == SQ_E || !is_color(piece, color) { let square = sq(file, rank);
continue if board.is_empty(square) || board.get_color(square) != color {
} continue
match get_type(piece) { }
SQ_R => stats.num_rooks += 1, match board.get_piece(square) {
SQ_N => stats.num_knights += 1, ROOK => self.num_rooks += 1,
SQ_B => stats.num_bishops += 1, KNIGHT => self.num_knights += 1,
SQ_Q => stats.num_queens += 1, BISHOP => self.num_bishops += 1,
SQ_K => stats.num_kings += 1, QUEEN => self.num_queens += 1,
SQ_P => { KING => self.num_kings += 1,
stats.num_pawns += 1; PAWN => {
let mut doubled = false; self.num_pawns += 1;
let mut isolated = true; // FIXME redo pawn stats properly
let mut backward = true; // let mut doubled = false;
for r in 0..8 { // let mut isolated = true;
// Check for doubled pawns. // let mut backward = true;
if // for r in 0..8 {
!doubled && // // Check for doubled pawns.
is_piece(get_square(board, &(pos_f, r)), color|SQ_P) && r != pos_r // if !doubled {
{ // let other_color = board.get_color(sq(file, r));
doubled = true; // is_piece(get_square(board, &(pos_f, r)), color|SQ_P) && r != pos_r
} // doubled = true;
// Check for isolated pawns. // }
if // // Check for isolated pawns.
isolated && // if
( // isolated &&
// Check on the left file if not on a-file... // (
( // // Check on the left file if not on a-file...
pos_f > POS_MIN && // (
is_piece(get_square(board, &(pos_f - 1, r)), color|SQ_P) // pos_f > POS_MIN &&
) || // is_piece(get_square(board, &(pos_f - 1, r)), color|SQ_P)
// Check on the right file if not on h-file... // ) ||
( // // Check on the right file if not on h-file...
pos_f < POS_MAX && // (
is_piece(get_square(board, &(pos_f + 1, r)), color|SQ_P) // pos_f < POS_MAX &&
) // is_piece(get_square(board, &(pos_f + 1, r)), color|SQ_P)
) // )
{ // )
isolated = false; // {
} // isolated = false;
// Check for backward pawns. // }
if backward { // // Check for backward pawns.
if color == SQ_WH && r <= pos_r { // if backward {
if ( // if color == SQ_WH && r <= pos_r {
pos_f > POS_MIN && // if (
is_type(get_square(board, &(pos_f - 1, r)), SQ_P) // pos_f > POS_MIN &&
) || ( // is_type(get_square(board, &(pos_f - 1, r)), SQ_P)
pos_f < POS_MAX && // ) || (
is_type(get_square(board, &(pos_f + 1, r)), SQ_P) // pos_f < POS_MAX &&
) { // is_type(get_square(board, &(pos_f + 1, r)), SQ_P)
backward = false; // ) {
} // backward = false;
} else if color == SQ_BL && r >= pos_r { // }
if ( // } else if color == SQ_BL && r >= pos_r {
pos_f > POS_MIN && // if (
is_type(get_square(board, &(pos_f - 1, r)), SQ_P) // pos_f > POS_MIN &&
) || ( // is_type(get_square(board, &(pos_f - 1, r)), SQ_P)
pos_f < POS_MAX && // ) || (
is_type(get_square(board, &(pos_f + 1, r)), SQ_P) // pos_f < POS_MAX &&
) { // is_type(get_square(board, &(pos_f + 1, r)), SQ_P)
backward = false; // ) {
} // backward = false;
} // }
} // }
} // }
if doubled { // }
stats.num_doubled_pawns += 1; // if doubled {
} // self.num_doubled_pawns += 1;
if isolated { // }
stats.num_isolated_pawns += 1; // if isolated {
} // self.num_isolated_pawns += 1;
if backward { // }
stats.num_backward_pawns += 1; // if backward {
} // self.num_backward_pawns += 1;
}, // }
_ => {} },
_ => {}
}
} }
} }
} }
@ -146,31 +162,6 @@ impl std::fmt::Display for BoardStats {
} }
} }
/// Create two new BoardStats objects from the board, for both sides.
///
/// See `compute_stats_into` for details.
pub fn compute_stats(board: &Board, game_state: &GameState) -> (BoardStats, BoardStats) {
let mut stats = (BoardStats::new(), BoardStats::new());
compute_stats_into(board, game_state, &mut stats);
stats
}
/// Compute stats for both the current player and its opponent.
///
/// The playing color will have its stats filled in the first
/// BoardStats object, its opponent in the second.
pub fn compute_stats_into(
board: &Board,
game_state: &rules::GameState,
stats: &mut (BoardStats, BoardStats)
) {
let mut gs = game_state.clone();
compute_color_stats_into(board, &gs, &mut stats.0);
gs.color = opposite(gs.color);
compute_color_stats_into(board, &gs, &mut stats.1);
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -178,8 +169,8 @@ mod tests {
#[test] #[test]
fn test_compute_stats() { fn test_compute_stats() {
// Check that initial stats are correct. // Check that initial stats are correct.
let b = new(); let b = Board::new();
let gs = rules::GameState::new(); let gs = GameState::new();
let initial_stats = BoardStats { let initial_stats = BoardStats {
num_pawns: 8, num_pawns: 8,
num_bishops: 2, num_bishops: 2,
@ -192,56 +183,56 @@ mod tests {
num_isolated_pawns: 0, num_isolated_pawns: 0,
mobility: 20, mobility: 20,
}; };
let mut stats = compute_stats(&b, &gs); let mut stats = BoardStats::new_from(&b, &gs);
eprintln!("{}", stats.0); eprintln!("{}", stats.0);
eprintln!("{}", stats.1); eprintln!("{}", stats.1);
assert!(stats.0 == stats.1); assert!(stats.0 == stats.1);
assert!(stats.0 == initial_stats); assert!(stats.0 == initial_stats);
// Check that doubled pawns are correctly counted. // Check that doubled pawns are correctly counted.
let mut b = new_empty(); let mut b = Board::new_empty();
set_square(&mut b, &pos("d4"), SQ_WH_P); b.set_square(D4, WHITE, PAWN);
set_square(&mut b, &pos("d6"), SQ_WH_P); b.set_square(D6, WHITE, PAWN);
compute_color_stats_into(&b, &gs, &mut stats.0); stats.0.compute(&b, &gs);
assert_eq!(stats.0.num_doubled_pawns, 2); assert_eq!(stats.0.num_doubled_pawns, 2);
// Add a pawn on another file, no changes expected. // Add a pawn on another file, no changes expected.
set_square(&mut b, &pos("e6"), SQ_WH_P); b.set_square(E6, WHITE, PAWN);
compute_color_stats_into(&b, &gs, &mut stats.0); stats.0.compute(&b, &gs);
assert_eq!(stats.0.num_doubled_pawns, 2); assert_eq!(stats.0.num_doubled_pawns, 2);
// Add a pawn backward in the d-file: there are now 3 doubled pawns. // Add a pawn backward in the d-file: there are now 3 doubled pawns.
set_square(&mut b, &pos("d2"), SQ_WH_P); b.set_square(D2, WHITE, PAWN);
compute_color_stats_into(&b, &gs, &mut stats.0); stats.0.compute(&b, &gs);
assert_eq!(stats.0.num_doubled_pawns, 3); assert_eq!(stats.0.num_doubled_pawns, 3);
// Check that isolated and backward pawns are correctly counted. // Check that isolated and backward pawns are correctly counted.
assert_eq!(stats.0.num_isolated_pawns, 0); assert_eq!(stats.0.num_isolated_pawns, 0);
assert_eq!(stats.0.num_backward_pawns, 2); // A bit weird? assert_eq!(stats.0.num_backward_pawns, 2); // A bit weird?
// Protect d4 pawn with a friend in e3: it is not isolated nor backward anymore. // Protect d4 pawn with a friend in e3: it is not isolated nor backward anymore.
set_square(&mut b, &pos("e3"), SQ_WH_P); b.set_square(E3, WHITE, PAWN);
compute_color_stats_into(&b, &gs, &mut stats.0); stats.0.compute(&b, &gs);
assert_eq!(stats.0.num_doubled_pawns, 5); assert_eq!(stats.0.num_doubled_pawns, 5);
assert_eq!(stats.0.num_isolated_pawns, 0); assert_eq!(stats.0.num_isolated_pawns, 0);
assert_eq!(stats.0.num_backward_pawns, 1); assert_eq!(stats.0.num_backward_pawns, 1);
// Add an adjacent friend to d2 pawn: no pawns are left isolated or backward. // Add an adjacent friend to d2 pawn: no pawns are left isolated or backward.
set_square(&mut b, &pos("c2"), SQ_WH_P); b.set_square(C2, WHITE, PAWN);
compute_color_stats_into(&b, &gs, &mut stats.0); stats.0.compute(&b, &gs);
assert_eq!(stats.0.num_doubled_pawns, 5); assert_eq!(stats.0.num_doubled_pawns, 5);
assert_eq!(stats.0.num_isolated_pawns, 0); assert_eq!(stats.0.num_isolated_pawns, 0);
assert_eq!(stats.0.num_backward_pawns, 0); assert_eq!(stats.0.num_backward_pawns, 0);
// Add an isolated/backward white pawn in a far file. // Add an isolated/backward white pawn in a far file.
set_square(&mut b, &pos("a2"), SQ_WH_P); b.set_square(A2, WHITE, PAWN);
compute_color_stats_into(&b, &gs, &mut stats.0); stats.0.compute(&b, &gs);
assert_eq!(stats.0.num_doubled_pawns, 5); assert_eq!(stats.0.num_doubled_pawns, 5);
assert_eq!(stats.0.num_isolated_pawns, 1); assert_eq!(stats.0.num_isolated_pawns, 1);
assert_eq!(stats.0.num_backward_pawns, 1); assert_eq!(stats.0.num_backward_pawns, 1);
// Check for pawns that are backward but not isolated. // Check for pawns that are backward but not isolated.
let mut b = new_empty(); let mut b = Board::new_empty();
// Here, d4 pawn protects both e5 and e3, but it is backward. // Here, d4 pawn protects both e5 and e3, but it is backward.
set_square(&mut b, &pos("d4"), SQ_WH_P); b.set_square(D4, WHITE, PAWN);
set_square(&mut b, &pos("e5"), SQ_WH_P); b.set_square(E5, WHITE, PAWN);
set_square(&mut b, &pos("e3"), SQ_WH_P); b.set_square(E3, WHITE, PAWN);
compute_color_stats_into(&b, &gs, &mut stats.0); stats.0.compute(&b, &gs);
assert_eq!(stats.0.num_doubled_pawns, 2); assert_eq!(stats.0.num_doubled_pawns, 2);
assert_eq!(stats.0.num_isolated_pawns, 0); assert_eq!(stats.0.num_isolated_pawns, 0);
assert_eq!(stats.0.num_backward_pawns, 1); assert_eq!(stats.0.num_backward_pawns, 1);