diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-02-21 05:22:54 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-02-21 05:28:45 +0300 |
commit | 46e9fcdcce5730827355db9449c41dc0a0e42b1f (patch) | |
tree | 7b9903446bd7884df6a22ddd2c6a2b8a6dcc3d44 /src/board | |
parent | 02ebad55af6c76ac9e36832f9be041f547f63b7c (diff) | |
download | chessnost-46e9fcdcce5730827355db9449c41dc0a0e42b1f.tar.gz |
refactor: use bitboard trait
Diffstat (limited to 'src/board')
-rw-r--r-- | src/board/engine.rs | 14 | ||||
-rw-r--r-- | src/board/mod.rs | 131 |
2 files changed, 73 insertions, 72 deletions
diff --git a/src/board/engine.rs b/src/board/engine.rs index ad2b3b2..5168350 100644 --- a/src/board/engine.rs +++ b/src/board/engine.rs @@ -1,5 +1,5 @@ use std::{time::{Instant, Duration}, f32::INFINITY, cmp::Ordering}; -use crate::{bitboard::pop_count, board::*}; +use crate::board::*; use super::ttable::{NodeType, TranspositionTableItem}; @@ -171,7 +171,7 @@ impl Board { _ => panic!("Unreachable") }; - material += serialize_bitboard(*bitboard).iter().fold(0., |acc, square| { + material += bitboard.serialize().iter().fold(0., |acc, square| { acc + piece_type.static_eval() + bonus_table[ match color { Color::White => square.mirror() as usize, @@ -195,7 +195,7 @@ impl Board { for file in 0..8 { let file_mask = A_FILE << file; - let pawns_on_file = pop_count(pawns & file_mask) as f32; + let pawns_on_file = (pawns & file_mask).pop_count() as f32; // Doubled pawns (-1 because one pawn on a file is ok) result += (pawns_on_file - 1.).max(0.0); @@ -214,7 +214,7 @@ impl Board { Color::White => self.occupancy >> 8, Color::Black => self.occupancy << 8, }; - result += pop_count(pawns & blocked_mask) as f32; + result += (pawns & blocked_mask).pop_count() as f32; result } @@ -224,14 +224,14 @@ impl Board { pub fn king_tropism(&self, color: Color) -> f32 { let mut result = 0.0; - let king_square = bitscan(match color { + let king_square = match color { Color::White => self.pieces[PieceType::King as usize], Color::Black => self.pieces[PieceType::KingBlack as usize], - }); + }.bitscan(); for (piece_type, bitboard) in self.pieces_by_color(color.flip()).iter().enumerate() { if piece_type != PieceType::King as usize && piece_type != PieceType::Pawn as usize { - for square in serialize_bitboard(*bitboard) { + for square in bitboard.serialize() { let distance = (king_square.rank() as f32 - square.rank() as f32).abs() + (king_square.file() as f32 - square.file() as f32).abs(); diff --git a/src/board/mod.rs b/src/board/mod.rs index a1166d6..1533ea2 100644 --- a/src/board/mod.rs +++ b/src/board/mod.rs @@ -1,7 +1,7 @@ use std::io::{stdin, stdout, Write}; use rand::{rngs::StdRng,SeedableRng,Rng}; -use crate::{bitboard::{Bitboard, serialize_bitboard, bitscan, pop_count}, moves::{Move, MoveKind}, attacks::Attacks, square::Square}; +use crate::{bitboard::{Bitboard, BitboardFns}, moves::{Move, MoveKind}, attacks::Attacks, square::Square}; use self::ttable::{TranspositionTable, TTABLE_SIZE}; mod engine; @@ -210,7 +210,7 @@ impl Board { } for (piece_id, bitboard) in self.pieces.iter().enumerate() { - for square in serialize_bitboard(*bitboard) { + for square in bitboard.serialize() { self.hash ^= self.zobrist_seed[piece_id * 64 + square as usize]; } } @@ -267,7 +267,7 @@ impl Board { for (piece_id, piece) in player_pieces.iter().enumerate() { match PieceType::from(piece_id) { PieceType::Pawn => { - for source in serialize_bitboard(*piece) { + for source in piece.serialize() { let ep_bitboard = match self.ep_target { Some(square) => { let rank = square.rank(); @@ -279,7 +279,7 @@ impl Board { } None => 0, }; - for target in serialize_bitboard(self.attacks.pawn[color as usize][source as usize] & capture_targets) { + for target in (self.attacks.pawn[color as usize][source as usize] & capture_targets).serialize() { moves.push(Move { source, target, kind: MoveKind::Capture }); if target.rank() == 7 { for promo_type in [PieceType::Bishop, PieceType::Knight, PieceType::Rook, PieceType::Queen] { @@ -291,10 +291,10 @@ impl Board { } } }; - for target in serialize_bitboard(self.attacks.pawn[color as usize][source as usize] & ep_bitboard) { + for target in (self.attacks.pawn[color as usize][source as usize] & ep_bitboard).serialize() { moves.push(Move { source, target, kind: MoveKind::EnPassant }); } - for target in serialize_bitboard(self.attacks.pawn_pushes[color as usize][source as usize] & empty) { + for target in (self.attacks.pawn_pushes[color as usize][source as usize] & empty).serialize() { moves.push(Move { source, target, kind: MoveKind::Quiet }); if target.rank() == 7 { for promo_type in [PieceType::Bishop, PieceType::Knight, PieceType::Rook, PieceType::Queen] { @@ -314,18 +314,18 @@ impl Board { Color::White => empty >> 8, Color::Black => empty << 8, }; - for source in serialize_bitboard(*piece & able_to_double_push_mask) { - for target in serialize_bitboard(self.attacks.pawn_double_pushes[color as usize][source as usize] & empty) { + for source in (*piece & able_to_double_push_mask).serialize() { + for target in (self.attacks.pawn_double_pushes[color as usize][source as usize] & empty).serialize() { moves.push(Move { source, target, kind: MoveKind::DoublePush }); }; } } PieceType::King => { - for source in serialize_bitboard(*piece) { - for target in serialize_bitboard(self.attacks.king[source as usize] & empty) { + for source in piece.serialize() { + for target in (self.attacks.king[source as usize] & empty).serialize() { moves.push(Move { source, target, kind: MoveKind::Quiet }); }; - for target in serialize_bitboard(self.attacks.king[source as usize] & capture_targets) { + for target in (self.attacks.king[source as usize] & capture_targets).serialize() { moves.push(Move { source, target, kind: MoveKind::Capture }); }; @@ -335,7 +335,7 @@ impl Board { Color::Black => Square::E8, }; if *piece == king_home_position.to_bitboard() { - for rook_square in serialize_bitboard(player_pieces[PieceType::Rook as usize]) + for rook_square in (player_pieces[PieceType::Rook as usize]).serialize() .iter() .filter(|rook_square| rook_square.rank() == king_home_position.rank()) { @@ -390,43 +390,43 @@ impl Board { } } PieceType::Knight => { - for source in serialize_bitboard(*piece) { - for target in serialize_bitboard(self.attacks.knight[source as usize] & empty) { + for source in piece.serialize() { + for target in (self.attacks.knight[source as usize] & empty).serialize() { moves.push(Move { source, target, kind: MoveKind::Quiet }); }; - for target in serialize_bitboard(self.attacks.knight[source as usize] & capture_targets) { + for target in (self.attacks.knight[source as usize] & capture_targets).serialize() { moves.push(Move { source, target, kind: MoveKind::Capture }); }; } } PieceType::Bishop => { - for source in serialize_bitboard(*piece) { - for target in serialize_bitboard(self.attacks.bishop(self.occupancy, source) & empty) { + for source in piece.serialize() { + for target in (self.attacks.bishop(self.occupancy, source) & empty).serialize() { moves.push(Move { source, target, kind: MoveKind::Quiet }); }; - for target in serialize_bitboard(self.attacks.bishop(self.occupancy, source) & capture_targets) { + for target in (self.attacks.bishop(self.occupancy, source) & capture_targets).serialize() { moves.push(Move { source, target, kind: MoveKind::Capture }); }; } } PieceType::Rook => { - for source in serialize_bitboard(*piece) { - for target in serialize_bitboard(self.attacks.rook(self.occupancy, source) & empty) { + for source in piece.serialize() { + for target in (self.attacks.rook(self.occupancy, source) & empty).serialize() { moves.push(Move { source, target, kind: MoveKind::Quiet }); }; } - for source in serialize_bitboard(*piece) { - for target in serialize_bitboard(self.attacks.rook(self.occupancy, source) & capture_targets) { + for source in piece.serialize() { + for target in (self.attacks.rook(self.occupancy, source) & capture_targets).serialize() { moves.push(Move { source, target, kind: MoveKind::Capture }); }; } } PieceType::Queen => { - for source in serialize_bitboard(*piece) { - for target in serialize_bitboard(self.attacks.queen(self.occupancy, source) & empty) { + for source in piece.serialize() { + for target in (self.attacks.queen(self.occupancy, source) & empty).serialize() { moves.push(Move { source, target, kind: MoveKind::Quiet }); }; - for target in serialize_bitboard(self.attacks.queen(self.occupancy, source) & capture_targets) { + for target in (self.attacks.queen(self.occupancy, source) & capture_targets).serialize() { moves.push(Move { source, target, kind: MoveKind::Capture }); }; } @@ -449,7 +449,7 @@ impl Board { Color::White => self.pieces[PieceType::PawnBlack as usize], }; - let pawn_attacked_squares = serialize_bitboard(opponent_pawns).iter().fold(0u64, |acc, square| { + let pawn_attacked_squares = opponent_pawns.serialize().iter().fold(0u64, |acc, square| { acc | self.attacks.pawn[color.flip() as usize][*square as usize] }); @@ -459,7 +459,7 @@ impl Board { for (piece_type, piece) in player_pieces.iter().enumerate() { match PieceType::from(piece_type) { PieceType::Pawn => { - for source in serialize_bitboard(*piece) { + for source in piece.serialize() { let ep_bitboard = match self.ep_target { Some(square) => { let rank = square.rank(); @@ -471,20 +471,20 @@ impl Board { } None => 0, }; - mobility += pop_count(self.attacks.pawn[color as usize][source as usize] & (opponent_occupancy | ep_bitboard)) as f32; - mobility += pop_count(self.attacks.pawn_pushes[color as usize][source as usize] & empty) as f32; + mobility += (self.attacks.pawn[color as usize][source as usize] & (opponent_occupancy | ep_bitboard)).pop_count() as f32; + mobility += (self.attacks.pawn_pushes[color as usize][source as usize] & empty).pop_count() as f32; } let able_to_double_push_mask = match color { Color::White => empty >> 8, Color::Black => empty << 8, }; - for source in serialize_bitboard(*piece & able_to_double_push_mask) { - mobility += pop_count(self.attacks.pawn_double_pushes[color as usize][source as usize] & empty) as f32; + for source in (*piece & able_to_double_push_mask).serialize() { + mobility += (self.attacks.pawn_double_pushes[color as usize][source as usize] & empty).pop_count() as f32; } } PieceType::King => { - for source in serialize_bitboard(*piece) { - mobility += pop_count(self.attacks.king[source as usize] & (empty | opponent_occupancy)) as f32; + for source in piece.serialize() { + mobility += (self.attacks.king[source as usize] & (empty | opponent_occupancy)).pop_count() as f32; // Castling let king_home_position = match color { @@ -492,7 +492,8 @@ impl Board { Color::Black => Square::E8, }; if *piece == king_home_position.to_bitboard() { - for rook_square in serialize_bitboard(player_pieces[PieceType::Rook as usize]) + for rook_square in player_pieces[PieceType::Rook as usize] + .serialize() .iter() .filter(|rook_square| rook_square.rank() == king_home_position.rank()) { @@ -530,18 +531,18 @@ impl Board { } } PieceType::Knight => { - for source in serialize_bitboard(*piece) { - mobility += pop_count(self.attacks.knight[source as usize] & (empty | opponent_occupancy)) as f32; + for source in piece.serialize() { + mobility += (self.attacks.knight[source as usize] & (empty | opponent_occupancy)).pop_count() as f32; } } PieceType::Bishop => { - for source in serialize_bitboard(*piece) { - mobility += pop_count(self.attacks.bishop(self.occupancy, source) & (empty | opponent_occupancy)) as f32; + for source in piece.serialize() { + mobility += (self.attacks.bishop(self.occupancy, source) & (empty | opponent_occupancy)).pop_count() as f32; } } PieceType::Rook => { - for source in serialize_bitboard(*piece) { - mobility += pop_count(self.attacks.rook(self.occupancy, source) & (empty | opponent_occupancy)) as f32; + for source in piece.serialize() { + mobility += (self.attacks.rook(self.occupancy, source) & (empty | opponent_occupancy)).pop_count() as f32; } } PieceType::Queen => { @@ -836,7 +837,7 @@ impl Board { Color::White => self.pieces[PieceType::King as usize], Color::Black => self.pieces[PieceType::KingBlack as usize], }; - let square = bitscan(king_bb); + let square = king_bb.bitscan(); self.is_square_attacked(square, color.flip()) } } @@ -868,7 +869,7 @@ impl Color { #[cfg(test)] mod tests { use super::*; - use crate::{bitboard::{pop_count, bitscan, print}, square::Square}; + use crate::{bitboard::BitboardFns, square::Square}; #[test] fn square_enum() { @@ -882,29 +883,29 @@ mod tests { let board = Board::new(); board.print(); - print(board.empty(), "Empty squares"); - - assert_eq!(pop_count(board.pieces[PieceType::Pawn as usize]), 8); - assert_eq!(pop_count(board.pieces[PieceType::Knight as usize]), 2); - assert_eq!(pop_count(board.pieces[PieceType::Bishop as usize]), 2); - assert_eq!(pop_count(board.pieces[PieceType::Rook as usize]), 2); - assert_eq!(pop_count(board.pieces[PieceType::Queen as usize]), 1); - assert_eq!(pop_count(board.pieces[PieceType::King as usize]), 1); - - assert_eq!(pop_count(board.pieces[PieceType::PawnBlack as usize]), 8); - assert_eq!(pop_count(board.pieces[PieceType::KnightBlack as usize]), 2); - assert_eq!(pop_count(board.pieces[PieceType::BishopBlack as usize]), 2); - assert_eq!(pop_count(board.pieces[PieceType::RookBlack as usize]), 2); - assert_eq!(pop_count(board.pieces[PieceType::QueenBlack as usize]), 1); - assert_eq!(pop_count(board.pieces[PieceType::KingBlack as usize]), 1); - - assert_eq!(bitscan(board.pieces[PieceType::King as usize]), Square::E1); - assert_eq!(bitscan(board.pieces[PieceType::QueenBlack as usize]), Square::D8); - - assert_eq!(pop_count(board.occupancy), 32); - assert_eq!(pop_count(board.empty()), 32); - assert_eq!(pop_count(board.color_occupancy(Color::White)), 16); - assert_eq!(pop_count(board.color_occupancy(Color::Black)), 16); + board.empty().print("Empty squares"); + + assert_eq!(board.pieces[PieceType::Pawn as usize].pop_count(), 8); + assert_eq!(board.pieces[PieceType::Knight as usize].pop_count(), 2); + assert_eq!(board.pieces[PieceType::Bishop as usize].pop_count(), 2); + assert_eq!(board.pieces[PieceType::Rook as usize].pop_count(), 2); + assert_eq!(board.pieces[PieceType::Queen as usize].pop_count(), 1); + assert_eq!(board.pieces[PieceType::King as usize].pop_count(), 1); + + assert_eq!(board.pieces[PieceType::PawnBlack as usize].pop_count(), 8); + assert_eq!(board.pieces[PieceType::KnightBlack as usize].pop_count(), 2); + assert_eq!(board.pieces[PieceType::BishopBlack as usize].pop_count(), 2); + assert_eq!(board.pieces[PieceType::RookBlack as usize].pop_count(), 2); + assert_eq!(board.pieces[PieceType::QueenBlack as usize].pop_count(), 1); + assert_eq!(board.pieces[PieceType::KingBlack as usize].pop_count(), 1); + + assert_eq!(board.pieces[PieceType::King as usize].bitscan(), Square::E1); + assert_eq!(board.pieces[PieceType::QueenBlack as usize].bitscan(), Square::D8); + + assert_eq!(board.occupancy.pop_count(), 32); + assert_eq!(board.empty().pop_count(), 32); + assert_eq!(board.color_occupancy(Color::White).pop_count(), 16); + assert_eq!(board.color_occupancy(Color::Black).pop_count(), 16); } #[test] |