engine: move stats out of Node

This commit is contained in:
dece 2020-06-11 20:42:32 +02:00
parent c3f4c80631
commit d8fa2f74ec

View file

@ -2,7 +2,6 @@
use std::sync::{Arc, atomic, mpsc}; use std::sync::{Arc, atomic, mpsc};
use std::thread; use std::thread;
use std::time;
use crate::board; use crate::board;
use crate::notation; use crate::notation;
@ -26,15 +25,13 @@ pub struct Engine {
working: Arc<atomic::AtomicBool>, working: Arc<atomic::AtomicBool>,
} }
/// Analysis node: a board along with the game state and some stats. /// Analysis node: a board along with the game state.
#[derive(Clone)] #[derive(Clone)]
struct Node { struct Node {
/// Board for this node. /// Board for this node.
board: board::Board, board: board::Board,
/// Game state. /// Game state.
game_state: rules::GameState, game_state: rules::GameState,
/// White and black pieces stats; have to be recomputed if board changes.
stats: (board::BoardStats, board::BoardStats), // white and black pieces stats
} }
impl Node { impl Node {
@ -42,7 +39,6 @@ impl Node {
Node { Node {
board: board::new_empty(), board: board::new_empty(),
game_state: rules::GameState::new(), game_state: rules::GameState::new(),
stats: (board::BoardStats::new(), board::BoardStats::new()),
} }
} }
} }
@ -51,8 +47,8 @@ impl std::fmt::Debug for Node {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!( write!(
f, f,
"Node {{ board: [...], game_state: {:?}, stats: {:?} }}", "Node {{ board: [...], game_state: {:?} }}",
self.game_state, self.stats self.game_state
) )
} }
} }
@ -65,10 +61,8 @@ impl std::fmt::Display for Node {
write!( write!(
f, f,
"* Board:\n{}\n\ "* Board:\n{}\n\
* Game state:\n{}\n\ * Game state:\n{}",
* White statistics:\n{}\n\ board_drawing, self.game_state
* Black statistics:\n{}",
board_drawing, self.game_state, self.stats.0, self.stats.1
) )
} }
} }
@ -227,10 +221,6 @@ impl Engine {
/// ///
/// Stop working after `movetime` ms, or go on forever if it's -1. /// Stop working after `movetime` ms, or go on forever if it's -1.
fn work(&mut self, args: &WorkArgs) { fn work(&mut self, args: &WorkArgs) {
if self.debug {
self.reply(Cmd::Log(format!("Current evaluation: {}", evaluate(&self.node.stats))));
}
self.working.store(true, atomic::Ordering::Relaxed); self.working.store(true, atomic::Ordering::Relaxed);
let mut node = self.node.clone(); let mut node = self.node.clone();
let args = args.clone(); let args = args.clone();
@ -274,7 +264,6 @@ impl Engine {
} }
} }
} }
board::compute_stats_into(&self.node.board, &mut self.node.stats);
} }
/// Start working using parameters passed with a "go" command. /// Start working using parameters passed with a "go" command.
@ -353,8 +342,8 @@ fn minimax(
maximizing: bool maximizing: bool
) -> (f32, Option<rules::Move>) { ) -> (f32, Option<rules::Move>) {
if depth == max_depth { if depth == max_depth {
board::compute_stats_into(&node.board, &mut node.stats); let stats = board::compute_stats(&node.board);
return (evaluate(&node.stats), None); return (evaluate(&stats), None);
} }
let mut minmax = if maximizing { MIN_F32 } else { MAX_F32 }; let mut minmax = if maximizing { MIN_F32 } else { MAX_F32 };
let mut minmax_move = None; let mut minmax_move = None;