diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-08-22 02:59:20 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-08-22 05:56:11 +0300 |
commit | a3bef6f93a960ad51d7601ed8e53c6a83a532f71 (patch) | |
tree | fffffe84df9c1e4903ccf0fd48db099758078a60 | |
parent | bc599d145a8d3a5e69128bc9e702ac7a32d73a4c (diff) | |
download | chessnost-a3bef6f93a960ad51d7601ed8e53c6a83a532f71.tar.gz |
feat: improve UCI position fen moves case
-rw-r--r-- | src/board/io.rs | 18 | ||||
-rw-r--r-- | src/board/move_generation.rs | 2 | ||||
-rw-r--r-- | src/grossmeister/UCI.rs | 68 | ||||
-rw-r--r-- | src/grossmeister/evaluation.rs | 5 | ||||
-rw-r--r-- | src/grossmeister/search.rs | 11 |
5 files changed, 50 insertions, 54 deletions
diff --git a/src/board/io.rs b/src/board/io.rs index 15d7744..78651d5 100644 --- a/src/board/io.rs +++ b/src/board/io.rs @@ -49,7 +49,9 @@ impl IO for Board { let mut rank = 7; let mut file = 0i32; - for character in fen.chars() { + let mut chars = fen.chars(); + + for character in chars.by_ref() { let index = rank * 8 + file; let position = 1 << index.clamp(0, 63); @@ -79,20 +81,26 @@ impl IO for Board { rank -= 1; file = -1; // So it becomes 0 }, - ' ' => { break }, // TODO: break for now, parse everything else later - '-' => {}, // TODO - 'w' => {}, // TODO + ' ' => { break }, _ => todo!("Unexpected character!"), } file += 1; } } + let ply = match chars.next() { + Some(char) => match char { + 'w' => 0, + 'b' => 1, + _ => panic!("Expected side to move"), + } + None => panic!("Expected side to move"), + }; let mut board = Self { piece_sets, occupancy: 0, - ply: 0, + ply, attacks: Attacks::new(), castling_rights: [[true; 2]; 2], // TODO: actualy parse from FEN ep_target: None, // TODO: parse from FEN diff --git a/src/board/move_generation.rs b/src/board/move_generation.rs index 77714c8..98df056 100644 --- a/src/board/move_generation.rs +++ b/src/board/move_generation.rs @@ -165,7 +165,6 @@ mod tests { fn generate_pseudolegal_moves_starting_position() { let mut board = Board::new(); let moves = board.generate_pseudolegal_moves(); - board.ply += 1; let black_moves = board.generate_pseudolegal_moves(); assert_eq!(moves.len(), 20); @@ -181,7 +180,6 @@ mod tests { fn moved_king_castle() { let fen = String::from("4k2r/ppp1n3/8/4R1Pp/5P2/q1P5/P1P1BP2/1K1R4 b - - 2 22"); let mut board = Board::from_FEN(fen); - board.ply += 1; // Shuffle kings around, returning to the same position board.make_move(Move { source: Square::E8, target: Square::F8, kind: MoveKind::Quiet }); 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::<Vec<_>>().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); |