From a3bef6f93a960ad51d7601ed8e53c6a83a532f71 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Tue, 22 Aug 2023 02:59:20 +0300 Subject: feat: improve UCI position fen moves case --- src/grossmeister/UCI.rs | 68 +++++++++++++++++------------------------- src/grossmeister/evaluation.rs | 5 ++-- src/grossmeister/search.rs | 11 ++++--- 3 files changed, 37 insertions(+), 47 deletions(-) (limited to 'src/grossmeister') diff --git a/src/grossmeister/UCI.rs b/src/grossmeister/UCI.rs index e10088a..1bd5f92 100644 --- a/src/grossmeister/UCI.rs +++ b/src/grossmeister/UCI.rs @@ -44,49 +44,37 @@ impl Grossmeister { *self = Self::default() } "position" => { - if let Some(token) = tokens.next() { - match token { - "startpos" => { - self.board = Board::new(); - - if let Some(token) = tokens.next() { - if token == "moves" { - for token in tokens.by_ref() { - let input_move = Move::from_notation(token.chars()); - let moves = self.board.generate_pseudolegal_moves(); - if let Some(mov) = moves - .iter() - .find(|m| { - let promo_matches = match input_move.kind { - MoveKind::Promotion(piece) => match m.kind { - MoveKind::Promotion(another_piece) => piece.without_color() == another_piece.without_color(), - _ => false - }, - _ => true, - }; - promo_matches && m.source == input_move.source && m.target == input_move.target - }) { - self.board.make_move(*mov); - } else { - panic!("Illegal move: {}", input_move); - } - } + match tokens.next() { + Some("startpos") => { + self.board = Board::new(); + } + Some("fen") => { + let fen = (0..6).filter_map(|_| tokens.next()).collect::>().join(" "); + self.board = Board::from_FEN(fen.to_string()); + } + _ => panic!("Expected position"), + } + if let Some("moves") = tokens.next() { + for token in tokens.by_ref() { + let input_move = Move::from_notation(token.chars()); + let moves = self.board.generate_pseudolegal_moves(); + if let Some(mov) = moves + .iter() + .find(|m| { + let promo_matches = match input_move.kind { + MoveKind::Promotion(piece) => match m.kind { + MoveKind::Promotion(another_piece) => piece.without_color() == another_piece.without_color(), + _ => false + }, + _ => true, + }; + promo_matches && m.source == input_move.source && m.target == input_move.target + }) { + self.board.make_move(*mov); } else { - panic!("Unexpected token: {}. CMD: {}", token, cmd); + panic!("Illegal move: {}", input_move); } - } - } - "fen" => { - // TODO: handle "moves" command after "fen"? - let index = "position fen".len() + 1; - if let Some(fen) = cmd.get(index..) { - self.board = Board::from_FEN(fen.to_string()); - }; - } - _ => {} } - } else { - todo!("Decide what to do when position is now provided") } } "go" => { diff --git a/src/grossmeister/evaluation.rs b/src/grossmeister/evaluation.rs index 9215420..3f94199 100644 --- a/src/grossmeister/evaluation.rs +++ b/src/grossmeister/evaluation.rs @@ -515,7 +515,7 @@ mod tests { #[test] fn white_winning() { - let fen = String::from("8/5pk1/6p1/R4b1p/3P4/1P2N3/P1r2PPP/R5K1 b - - 1 27"); + let fen = String::from("8/5pk1/6p1/R4b1p/3P4/1P2N3/P1r2PPP/R5K1 w - - 1 27"); let board = Board::from_FEN(fen); let gm = Grossmeister::new(board); let score = gm.evaluate(); @@ -602,8 +602,7 @@ mod tests { let score2 = { let fen = String::from("r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 1 "); let board = Board::from_FEN(fen); - let mut gm = Grossmeister::new(board); - gm.board.ply += 1; // TODO: remove me when FEN parsing includes side to move + let gm = Grossmeister::new(board); let score = gm.evaluate(); gm.board.print(); println!("Score {}", score); diff --git a/src/grossmeister/search.rs b/src/grossmeister/search.rs index 7b40fab..cfe1e6d 100644 --- a/src/grossmeister/search.rs +++ b/src/grossmeister/search.rs @@ -236,14 +236,18 @@ impl Grossmeister { gradual_widening_counter += 1; beta = alpha + window_size * 0.1; alpha = search_result.0 - window_size * 2.0f32.powi(gradual_widening_counter); - println!("info score upperbound {:.0}", beta * 100.0); + if self.debug { + println!("info score upperbound {:.0}", beta * 100.0); + } continue; } if search_result.0 >= beta { // Beta-cutoff gradual_widening_counter += 1; alpha = beta - window_size * 0.1; beta = search_result.0 + window_size * 2.0f32.powi(gradual_widening_counter); - println!("info score lowerbound {:.0}", alpha * 100.0); + if self.debug { + println!("info score lowerbound {:.0}", alpha * 100.0); + } continue; } @@ -471,8 +475,7 @@ mod tests { #[test] fn stupid_knight_sac() { let fen = String::from("r3k1r1/pp3ppp/1q6/2ppPn2/6P1/1PPP1P2/P1N3KP/R2QR3 b - - 0 18"); - let mut board = Board::from_FEN(fen); - board.ply += 1; // TODO: remove me when FEN parsing includes side to move + let board = Board::from_FEN(fen); let mut gm = Grossmeister::new(board); let (_, pv) = gm.iterative_deepening(6); -- cgit v1.2.3