engine: fix wrong best move selection

This commit is contained in:
dece 2020-06-06 00:58:11 +02:00
parent 5ff467f899
commit 659094b269
2 changed files with 13 additions and 8 deletions

View file

@ -72,6 +72,7 @@ enum Mode {
pub enum Cmd { pub enum Cmd {
// Commands that can be received by the engine. // Commands that can be received by the engine.
UciChannel(mpsc::Sender<Cmd>), // Provide a sender to UCI to start receiving commands. UciChannel(mpsc::Sender<Cmd>), // Provide a sender to UCI to start receiving commands.
UciDebug(bool), // UCI "debug" command.
UciPosition(Vec<uci::PositionArgs>), // UCI "position" command. UciPosition(Vec<uci::PositionArgs>), // UCI "position" command.
UciGo(Vec<uci::GoArgs>), // UCI "go" command. UciGo(Vec<uci::GoArgs>), // UCI "go" command.
Stop, // Stop working ASAP. Stop, // Stop working ASAP.
@ -110,7 +111,7 @@ pub enum Info {
impl Engine { impl Engine {
pub fn new() -> Engine { pub fn new() -> Engine {
Engine { Engine {
debug: true, debug: false,
node: Node::new(), node: Node::new(),
mode: Mode::No, mode: Mode::No,
listening: false, listening: false,
@ -141,8 +142,9 @@ impl Engine {
fn handle_command(&mut self, cmd: &Cmd) { fn handle_command(&mut self, cmd: &Cmd) {
match cmd { match cmd {
// UCI commands. // UCI commands.
Cmd::UciPosition(args) => self.uci_position(&args), Cmd::UciDebug(on) => self.debug = *on,
Cmd::UciGo(args) => self.uci_go(&args), Cmd::UciPosition(args) => self.uci_position(args),
Cmd::UciGo(args) => self.uci_go(args),
Cmd::Stop => self.stop(), Cmd::Stop => self.stop(),
// Workers commands. // Workers commands.
Cmd::Log(s) => self.reply(Cmd::Log(s.to_string())), Cmd::Log(s) => self.reply(Cmd::Log(s.to_string())),
@ -199,7 +201,6 @@ impl Engine {
} }
fn apply_move(&mut self, m: &rules::Move) { fn apply_move(&mut self, m: &rules::Move) {
eprintln!("Applying {}...", notation::move_to_string(m));
rules::apply_move_to(&mut self.node.board, &mut self.node.game_state, m); rules::apply_move_to(&mut self.node.board, &mut self.node.game_state, m);
} }
@ -314,11 +315,9 @@ fn analyze(
rules::apply_move_to(&mut board, &mut game_state, &m); rules::apply_move_to(&mut board, &mut game_state, &m);
let stats = board::compute_stats(&board); let stats = board::compute_stats(&board);
let e = evaluate(&stats); let e = evaluate(&stats);
tx.send(Cmd::Log(format!("Move {} eval {}", notation::move_to_string(&m), e))).unwrap();
if if
(board::is_white(game_state.color) && e > best_e) || (board::is_white(node.game_state.color) && e > best_e) ||
(board::is_black(game_state.color) && e < best_e) (board::is_black(node.game_state.color) && e < best_e)
{ {
best_e = e; best_e = e;
best_move = Some(m.clone()); best_move = Some(m.clone());

View file

@ -43,6 +43,7 @@ pub enum Cmd {
#[derive(Debug)] #[derive(Debug)]
pub enum UciCmd { pub enum UciCmd {
Uci, Uci,
Debug(bool),
IsReady, IsReady,
UciNewGame, UciNewGame,
Stop, Stop,
@ -173,6 +174,10 @@ impl Uci {
self.send_identities(); self.send_identities();
self.setup_engine(); self.setup_engine();
}, },
UciCmd::Debug(on) => {
let args = engine::Cmd::UciDebug(*on);
self.engine_in.as_ref().unwrap().send(args).unwrap();
}
UciCmd::IsReady => if self.state == State::Ready { self.send_ready() }, UciCmd::IsReady => if self.state == State::Ready { self.send_ready() },
UciCmd::UciNewGame => if self.state == State::Ready { /* Nothing to do. */ }, UciCmd::UciNewGame => if self.state == State::Ready { /* Nothing to do. */ },
UciCmd::Position(args) => if self.state == State::Ready { UciCmd::Position(args) => if self.state == State::Ready {
@ -269,6 +274,7 @@ fn parse_command(s: &str) -> UciCmd {
let fields: Vec<&str> = s.split_whitespace().collect(); let fields: Vec<&str> = s.split_whitespace().collect();
match fields[0] { match fields[0] {
"uci" => UciCmd::Uci, "uci" => UciCmd::Uci,
"debug" => UciCmd::Debug(fields[1] == "on"),
"isready" => UciCmd::IsReady, "isready" => UciCmd::IsReady,
"ucinewgame" => UciCmd::UciNewGame, "ucinewgame" => UciCmd::UciNewGame,
"stop" => UciCmd::Stop, "stop" => UciCmd::Stop,