diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-01-23 09:10:20 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-01-23 09:10:20 +0300 |
commit | 1c0053f6681e4825fa4f5f63031b1ba82678e947 (patch) | |
tree | 4c98f1234648fe1513790935ef6c0d2f02b93900 | |
parent | 6719e247a3e0c75261665eca166fc5c8c8fdaf27 (diff) | |
download | chessnost-1c0053f6681e4825fa4f5f63031b1ba82678e947.tar.gz |
test: implement perft
-rw-r--r-- | src/board.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/board.rs b/src/board.rs index 24bdefa..88b87a9 100644 --- a/src/board.rs +++ b/src/board.rs @@ -118,6 +118,10 @@ impl Board { occupancy } + pub fn color_to_move(&self) -> Color { + Color::from((self.ply % 2) as u8) + } + pub fn print(&self) { println!(); for rank in (0..8).rev() { @@ -158,6 +162,7 @@ impl Board { }; } + // Make sure no blocking piece is standing in front of the pawn let able_to_double_push_mask = match color { Color::White => empty >> 8, Color::Black => empty << 8, @@ -281,6 +286,38 @@ impl Board { self.ply -= 1; } + + fn perft(&mut self, depth: u8, print: bool) -> u64 { + if depth == 0 { + return 1 // This a leaf, exactly one node + } + let color = self.color_to_move(); + + let moves = self.generate_moves(color); + + if print { + println!("Running perft for depth {}. Color to move is {:?}\n{} moves available", depth, color, moves.len()); + println!("{} moves available", moves.len()); + } + + let mut total = 0; + + for mov in moves { + let captured_piece = self.make_move(mov); + if print { + println!("{:?}", mov); + self.print(); + } + total += self.perft(depth - 1, print); + self.unmake_move(mov, captured_piece); + } + + if print { + println!("Found {} nodes in this subtree (depth {})", total, depth); + } + + total + } } @@ -406,4 +443,15 @@ mod tests { assert_eq!(board, initial_board, "Board state after unmake_move should be the same as before make_move"); } + + #[test] + fn test_perft() { + let fen = String::from(DEFAULT_FEN); + let mut board = Board::from_FEN(fen); + + assert_eq!(board.perft(0, false), 1); + assert_eq!(board.perft(1, false), 20); + assert_eq!(board.perft(2, false), 400); + // TODO: assert_eq!(board.perft(3, false), 8902); + } } |