aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2023-02-21 12:15:35 +0300
committereug-vs <eugene@eug-vs.xyz>2023-02-21 12:15:35 +0300
commit8cd12afe8d057e9f80c50727dea00b40256bca54 (patch)
treeb522754bb18610ca7511e07eebe7b81af2830ee2
parent27c08580d7ae61ea86a3c0bfbb62c5930ec010ac (diff)
downloadchessnost-8cd12afe8d057e9f80c50727dea00b40256bca54.tar.gz
refactor: rename board.pieces -> piece_sets
-rw-r--r--src/board/engine.rs8
-rw-r--r--src/board/mod.rs126
2 files changed, 67 insertions, 67 deletions
diff --git a/src/board/engine.rs b/src/board/engine.rs
index d58e529..c98348e 100644
--- a/src/board/engine.rs
+++ b/src/board/engine.rs
@@ -189,8 +189,8 @@ impl Board {
let mut result = 0.0;
let pawns = match color {
- Color::White => self.pieces[PieceType::Pawn as usize],
- Color::Black => self.pieces[PieceType::PawnBlack as usize],
+ Color::White => self.piece_sets[PieceType::Pawn as usize],
+ Color::Black => self.piece_sets[PieceType::PawnBlack as usize],
};
for file in 0..8 {
@@ -225,8 +225,8 @@ impl Board {
let mut result = 0.0;
let king_square = match color {
- Color::White => self.pieces[PieceType::King as usize],
- Color::Black => self.pieces[PieceType::KingBlack as usize],
+ Color::White => self.piece_sets[PieceType::King as usize],
+ Color::Black => self.piece_sets[PieceType::KingBlack as usize],
}.bitscan();
for (piece_type, bitboard) in self.pieces_by_color(color.flip()).iter().enumerate() {
diff --git a/src/board/mod.rs b/src/board/mod.rs
index fcc476a..c81b088 100644
--- a/src/board/mod.rs
+++ b/src/board/mod.rs
@@ -14,7 +14,7 @@ pub enum CastlingSide {
#[derive(Debug, Clone, PartialEq)]
pub struct Board {
- pub pieces: [Bitboard; 12],
+ pub piece_sets: [Bitboard; 12],
pub occupancy: Bitboard,
pub ply: u16,
@@ -79,7 +79,7 @@ const PIECE_CHARS: [&str; 12] = [
impl Board {
#[allow(non_snake_case)]
pub fn from_FEN(fen: String) -> Self {
- let mut pieces = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
+ let mut piece_sets = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let mut rank = 7;
let mut file = 0i32;
@@ -98,18 +98,18 @@ impl Board {
}
} else {
match character {
- 'P' => pieces[PieceType::Pawn as usize] |= position,
- 'N' => pieces[PieceType::Knight as usize] |= position,
- 'B' => pieces[PieceType::Bishop as usize] |= position,
- 'R' => pieces[PieceType::Rook as usize] |= position,
- 'Q' => pieces[PieceType::Queen as usize] |= position,
- 'K' => pieces[PieceType::King as usize] |= position,
- 'p' => pieces[PieceType::PawnBlack as usize] |= position,
- 'n' => pieces[PieceType::KnightBlack as usize] |= position,
- 'b' => pieces[PieceType::BishopBlack as usize] |= position,
- 'r' => pieces[PieceType::RookBlack as usize] |= position,
- 'q' => pieces[PieceType::QueenBlack as usize] |= position,
- 'k' => pieces[PieceType::KingBlack as usize] |= position,
+ 'P' => piece_sets[PieceType::Pawn as usize] |= position,
+ 'N' => piece_sets[PieceType::Knight as usize] |= position,
+ 'B' => piece_sets[PieceType::Bishop as usize] |= position,
+ 'R' => piece_sets[PieceType::Rook as usize] |= position,
+ 'Q' => piece_sets[PieceType::Queen as usize] |= position,
+ 'K' => piece_sets[PieceType::King as usize] |= position,
+ 'p' => piece_sets[PieceType::PawnBlack as usize] |= position,
+ 'n' => piece_sets[PieceType::KnightBlack as usize] |= position,
+ 'b' => piece_sets[PieceType::BishopBlack as usize] |= position,
+ 'r' => piece_sets[PieceType::RookBlack as usize] |= position,
+ 'q' => piece_sets[PieceType::QueenBlack as usize] |= position,
+ 'k' => piece_sets[PieceType::KingBlack as usize] |= position,
'/' => {
rank -= 1;
file = -1; // So it becomes 0
@@ -127,7 +127,7 @@ impl Board {
let zobrist_seed = [(); 781].map(|_| rng.gen());
let mut board = Self {
- pieces,
+ piece_sets,
occupancy: 0,
ply: 0,
attacks: Attacks::new(),
@@ -179,7 +179,7 @@ impl Board {
}
fn update_occupancy(&mut self) {
- self.occupancy = self.pieces.iter().fold(0, |acc, bitboard| acc | bitboard)
+ self.occupancy = self.piece_sets.iter().fold(0, |acc, bitboard| acc | bitboard)
}
fn empty(&self) -> Bitboard {
@@ -188,8 +188,8 @@ impl Board {
fn pieces_by_color(&self, color: Color) -> &[Bitboard] {
match color {
- Color::White => &self.pieces[0..6],
- Color::Black => &self.pieces[6..12],
+ Color::White => &self.piece_sets[0..6],
+ Color::Black => &self.piece_sets[6..12],
}
}
@@ -209,7 +209,7 @@ impl Board {
};
}
- for (piece_id, bitboard) in self.pieces.iter().enumerate() {
+ for (piece_id, bitboard) in self.piece_sets.iter().enumerate() {
for square in bitboard.serialize() {
self.hash ^= self.zobrist_seed[piece_id * 64 + square as usize];
}
@@ -239,7 +239,7 @@ impl Board {
let index = rank * 8 + file;
let position: Bitboard = 1 << index;
let mut found = false;
- for (piece_type, piece_bitboard) in self.pieces.iter().enumerate() {
+ for (piece_type, piece_bitboard) in self.piece_sets.iter().enumerate() {
if (piece_bitboard & position) > 0 {
found = true;
print!("{} ", PIECE_CHARS[piece_type]);
@@ -285,8 +285,8 @@ impl Board {
let targets = if tactical_only {
self.color_occupancy(color.flip()) ^ match color {
// Exclude opponent king because we can't capture it
- Color::White => self.pieces[PieceType::KingBlack as usize],
- Color::Black => self.pieces[PieceType::King as usize],
+ Color::White => self.piece_sets[PieceType::KingBlack as usize],
+ Color::Black => self.piece_sets[PieceType::King as usize],
}
} else {
empty
@@ -427,8 +427,8 @@ impl Board {
let player_pieces = self.pieces_by_color(color);
let opponent_pawns = match color {
- Color::Black => self.pieces[PieceType::Pawn as usize],
- Color::White => self.pieces[PieceType::PawnBlack as usize],
+ Color::Black => self.piece_sets[PieceType::Pawn as usize],
+ Color::White => self.piece_sets[PieceType::PawnBlack as usize],
};
let pawn_attacked_squares = opponent_pawns.serialize().iter().fold(0u64, |acc, square| {
@@ -539,7 +539,7 @@ impl Board {
fn piece_by_square(&self, square: Square) -> Option<PieceType> {
let square_bb = square.to_bitboard();
- self.pieces
+ self.piece_sets
.iter()
.enumerate()
.find(|(_, bitboard)| *bitboard & square_bb > 0)
@@ -555,7 +555,7 @@ impl Board {
// Remove existing piece (if any) from target square
let mut captured_piece = match self.piece_by_square(mov.target) {
Some(target_piece) => {
- self.pieces[target_piece as usize] ^= move_target_bb;
+ self.piece_sets[target_piece as usize] ^= move_target_bb;
self.hash ^= self.zobrist_seed[(target_piece as usize) * 64 + mov.target as usize];
Some(target_piece)
},
@@ -569,7 +569,7 @@ impl Board {
captured_piece = match self.piece_by_square(captured_square) {
Some(pawn_type) => {
let captured_bb = captured_square.to_bitboard();
- self.pieces[pawn_type as usize] ^= captured_bb;
+ self.piece_sets[pawn_type as usize] ^= captured_bb;
self.occupancy ^= captured_bb;
self.hash ^= self.zobrist_seed[pawn_type as usize* 64 + captured_square as usize];
Some(pawn_type)
@@ -585,20 +585,20 @@ impl Board {
match mov.kind {
MoveKind::Promotion(promotion_piece) => {
let promo_id = promotion_piece as usize;
- self.pieces[source_id] ^= move_source_bb;
+ self.piece_sets[source_id] ^= move_source_bb;
self.occupancy ^= move_source_bb;
self.hash ^= self.zobrist_seed[source_id * 64 + mov.source as usize];
- self.pieces[promo_id] |= move_target_bb;
+ self.piece_sets[promo_id] |= move_target_bb;
self.occupancy |= move_target_bb;
self.hash ^= self.zobrist_seed[promo_id * 64 + mov.target as usize];
},
_ => {
- self.pieces[source_id] ^= move_source_bb;
+ self.piece_sets[source_id] ^= move_source_bb;
self.occupancy ^= move_source_bb;
self.hash ^= self.zobrist_seed[source_id * 64 + mov.source as usize];
- self.pieces[source_id] |= move_target_bb;
+ self.piece_sets[source_id] |= move_target_bb;
self.occupancy |= move_target_bb;
self.hash ^= self.zobrist_seed[source_id * 64 + mov.target as usize];
}
@@ -628,11 +628,11 @@ impl Board {
match self.piece_by_square(rook_source_square) {
Some(rook_type) => {
let rook_id = rook_type as usize;
- self.pieces[rook_id] ^= rook_source_bb;
+ self.piece_sets[rook_id] ^= rook_source_bb;
self.occupancy ^= rook_source_bb;
self.hash ^= self.zobrist_seed[rook_id * 64 + rook_source_square as usize];
- self.pieces[rook_id] |= rook_target_bb;
+ self.piece_sets[rook_id] |= rook_target_bb;
self.occupancy |= rook_target_bb;
self.hash ^= self.zobrist_seed[rook_id * 64 + rook_target_square as usize];
},
@@ -699,7 +699,7 @@ impl Board {
match mov.kind {
MoveKind::Promotion(promotion_piece) => {
let promo_id = promotion_piece as usize;
- self.pieces[promo_id] ^= move_target_bb;
+ self.piece_sets[promo_id] ^= move_target_bb;
self.occupancy ^= move_target_bb;
let source_id = match Color::from_piece(promotion_piece) {
@@ -707,15 +707,15 @@ impl Board {
Color::Black => PieceType::PawnBlack,
} as usize;
- self.pieces[source_id] |= move_source_bb;
+ self.piece_sets[source_id] |= move_source_bb;
self.occupancy |= move_source_bb;
}
_ => {
let source_id = source_piece as usize;
- self.pieces[source_id] ^= move_target_bb;
+ self.piece_sets[source_id] ^= move_target_bb;
self.occupancy ^= move_target_bb;
- self.pieces[source_id] |= move_source_bb;
+ self.piece_sets[source_id] |= move_source_bb;
self.occupancy |= move_source_bb;
}
}
@@ -733,16 +733,16 @@ impl Board {
let rook_source_bb = Square::from_coords(mov.target.rank(), rook_source_file).to_bitboard();
let rook_target_bb = Square::from_coords(mov.target.rank(), rook_target_file).to_bitboard();
- match self.pieces
+ match self.piece_sets
.iter()
.enumerate()
.find(|(_, bitboard)| *bitboard & rook_target_bb > 0)
{
Some((rook_type, _)) => {
- self.pieces[rook_type] |= rook_source_bb;
+ self.piece_sets[rook_type] |= rook_source_bb;
self.occupancy |= rook_source_bb;
- self.pieces[rook_type] ^= rook_target_bb;
+ self.piece_sets[rook_type] ^= rook_target_bb;
self.occupancy ^= rook_target_bb;
},
None => panic!("Rook was not found when castling"),
@@ -756,11 +756,11 @@ impl Board {
// Return pawn captured by En Passant pawn if needed
MoveKind::EnPassant => {
let original_dead_pawn_bb = Square::from_coords(mov.source.rank(), mov.target.file()).to_bitboard();
- self.pieces[target_piece as usize] |= original_dead_pawn_bb;
+ self.piece_sets[target_piece as usize] |= original_dead_pawn_bb;
self.occupancy |= original_dead_pawn_bb;
},
_ => {
- self.pieces[target_piece as usize] |= move_target_bb;
+ self.piece_sets[target_piece as usize] |= move_target_bb;
self.occupancy |= move_target_bb;
},
}
@@ -816,8 +816,8 @@ impl Board {
fn is_king_in_check(&self, color: Color) -> bool {
let king_bb = match color {
- Color::White => self.pieces[PieceType::King as usize],
- Color::Black => self.pieces[PieceType::KingBlack as usize],
+ Color::White => self.piece_sets[PieceType::King as usize],
+ Color::Black => self.piece_sets[PieceType::KingBlack as usize],
};
let square = king_bb.bitscan();
self.is_square_attacked(square, color.flip())
@@ -867,22 +867,22 @@ mod tests {
board.print();
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.piece_sets[PieceType::Pawn as usize].pop_count(), 8);
+ assert_eq!(board.piece_sets[PieceType::Knight as usize].pop_count(), 2);
+ assert_eq!(board.piece_sets[PieceType::Bishop as usize].pop_count(), 2);
+ assert_eq!(board.piece_sets[PieceType::Rook as usize].pop_count(), 2);
+ assert_eq!(board.piece_sets[PieceType::Queen as usize].pop_count(), 1);
+ assert_eq!(board.piece_sets[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.piece_sets[PieceType::PawnBlack as usize].pop_count(), 8);
+ assert_eq!(board.piece_sets[PieceType::KnightBlack as usize].pop_count(), 2);
+ assert_eq!(board.piece_sets[PieceType::BishopBlack as usize].pop_count(), 2);
+ assert_eq!(board.piece_sets[PieceType::RookBlack as usize].pop_count(), 2);
+ assert_eq!(board.piece_sets[PieceType::QueenBlack as usize].pop_count(), 1);
+ assert_eq!(board.piece_sets[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.piece_sets[PieceType::King as usize].bitscan(), Square::E1);
+ assert_eq!(board.piece_sets[PieceType::QueenBlack as usize].bitscan(), Square::D8);
assert_eq!(board.occupancy.pop_count(), 32);
assert_eq!(board.empty().pop_count(), 32);
@@ -936,8 +936,8 @@ mod tests {
board.update_zobrist_hash();
assert_eq!(hash, board.hash, "Hash should be correctly updated after move");
- assert!(board.pieces[PieceType::PawnBlack as usize] & Square::F7.to_bitboard() == 0);
- assert!(board.pieces[PieceType::PawnBlack as usize] & Square::F5.to_bitboard() > 0);
+ assert!(board.piece_sets[PieceType::PawnBlack as usize] & Square::F7.to_bitboard() == 0);
+ assert!(board.piece_sets[PieceType::PawnBlack as usize] & Square::F5.to_bitboard() > 0);
assert!(board.ply == 1);
let white_move = Move { source: Square::D2, target: Square::A5, kind: MoveKind::Capture };
@@ -950,9 +950,9 @@ mod tests {
board.print();
- assert!(board.pieces[PieceType::PawnBlack as usize] & Square::A5.to_bitboard() == 0, "Target piece should be captured");
- assert!(board.pieces[PieceType::Queen as usize] & Square::D2.to_bitboard() == 0);
- assert!(board.pieces[PieceType::Queen as usize] & Square::A5.to_bitboard() > 0);
+ assert!(board.piece_sets[PieceType::PawnBlack as usize] & Square::A5.to_bitboard() == 0, "Target piece should be captured");
+ assert!(board.piece_sets[PieceType::Queen as usize] & Square::D2.to_bitboard() == 0);
+ assert!(board.piece_sets[PieceType::Queen as usize] & Square::A5.to_bitboard() > 0);
assert_ne!(board.occupancy, initial_board.occupancy, "Occupancy should change after make_move");
assert!(board.ply == 2);
}