diff --git a/src/board/board.rs b/src/board/board.rs index 895f80e..81e5eca 100644 --- a/src/board/board.rs +++ b/src/board/board.rs @@ -1,6 +1,6 @@ use u64 as Bitboard; -use crate::board::bitboard::{have_common_bit, lsb}; +use crate::board::bitboard::{have_common_bit, lsb, square_to_bitboard}; use crate::board::state::State; use crate::movegen::attack_generator::{ fetch_bishop_attacks, fetch_king_attacks, fetch_knight_attacks, fetch_pawn_attacks, @@ -272,17 +272,15 @@ impl PieceType { } use std::ops::{Index, IndexMut}; -use super::bitboard::square_to_bitboard; - -impl Index for [Piece] { - type Output = Piece; +impl Index for [T] { + type Output = T; fn index(&self, piece_type: PieceType) -> &Self::Output { &self[piece_type.idx()] } } -impl IndexMut for [Piece] { +impl IndexMut for [T] { fn index_mut(&mut self, piece_type: PieceType) -> &mut Self::Output { &mut self[piece_type.idx()] } @@ -307,6 +305,20 @@ impl Color { } } +impl Index for [T] { + type Output = T; + + fn index(&self, color: Color) -> &Self::Output { + &self[color.idx()] + } +} + +impl IndexMut for [T] { + fn index_mut(&mut self, color: Color) -> &mut Self::Output { + &mut self[color.idx()] + } +} + #[cfg(test)] mod tests { use crate::{board::fen::from_fen, movegen::attack_generator::init_attacks}; diff --git a/src/board/state.rs b/src/board/state.rs index d6b558d..eb1659a 100644 --- a/src/board/state.rs +++ b/src/board/state.rs @@ -164,3 +164,19 @@ impl Castle { self as usize } } + +use std::ops::{Index, IndexMut}; + +impl Index for [T] { + type Output = T; + + fn index(&self, castle: Castle) -> &Self::Output { + &self[castle.idx()] + } +} + +impl IndexMut for [T] { + fn index_mut(&mut self, castle: Castle) -> &mut Self::Output { + &mut self[castle.idx()] + } +} diff --git a/src/board/zobrist.rs b/src/board/zobrist.rs index 41b3c62..4844f13 100644 --- a/src/board/zobrist.rs +++ b/src/board/zobrist.rs @@ -63,7 +63,7 @@ impl ZobristKeys { while bb != 0 { let square = lsb(bb); - hash ^= self.piece_square_color[square][piece.piece_type.idx()][piece.color.idx()]; + hash ^= self.piece_square_color[square][piece.piece_type][piece.color]; bb &= bb - 1; } } @@ -73,7 +73,7 @@ impl ZobristKeys { while bb != 0 { let square = lsb(bb); - hash ^= self.piece_square_color[square][piece.piece_type.idx()][piece.color.idx()]; + hash ^= self.piece_square_color[square][piece.piece_type][piece.color]; bb &= bb - 1; } } @@ -82,8 +82,8 @@ impl ZobristKeys { hash ^= self.side_to_move } - hash ^= self.castling_ability[board.state.castling_ability[0].idx()][0]; - hash ^= self.castling_ability[board.state.castling_ability[1].idx()][1]; + hash ^= self.castling_ability[board.state.castling_ability[0]][0]; + hash ^= self.castling_ability[board.state.castling_ability[1]][1]; if let Some(ep) = board.state.en_passant_square { hash ^= self.en_passant[square::to_file(ep)]; @@ -125,16 +125,16 @@ impl ZobristHash { new_castling_ability: [Castle; 2], ) { let keys = zobrist_keys(); - self.hash ^= keys.castling_ability[old_castling_ability[0].idx()][0]; - self.hash ^= keys.castling_ability[old_castling_ability[1].idx()][1]; - self.hash ^= keys.castling_ability[new_castling_ability[0].idx()][0]; - self.hash ^= keys.castling_ability[new_castling_ability[1].idx()][1] + self.hash ^= keys.castling_ability[old_castling_ability[0]][0]; + self.hash ^= keys.castling_ability[old_castling_ability[1]][1]; + self.hash ^= keys.castling_ability[new_castling_ability[0]][0]; + self.hash ^= keys.castling_ability[new_castling_ability[1]][1] } pub fn update_quiet(&mut self, src: usize, dst: usize, piece_at_src: PieceType, color: Color) { let keys = zobrist_keys(); - self.hash ^= keys.piece_square_color[src][piece_at_src.idx()][color.idx()]; - self.hash ^= keys.piece_square_color[dst][piece_at_src.idx()][color.idx()]; + self.hash ^= keys.piece_square_color[src][piece_at_src][color]; + self.hash ^= keys.piece_square_color[dst][piece_at_src][color]; } pub fn update_capture( @@ -146,17 +146,16 @@ impl ZobristHash { color: Color, ) { let keys = zobrist_keys(); - self.hash ^= keys.piece_square_color[src][piece_at_src.idx()][color.idx()]; - self.hash ^= keys.piece_square_color[dst][piece_at_src.idx()][color.idx()]; - self.hash ^= keys.piece_square_color[dst][piece_at_dst.idx()][color.opponent().idx()] + self.hash ^= keys.piece_square_color[src][piece_at_src][color]; + self.hash ^= keys.piece_square_color[dst][piece_at_src][color]; + self.hash ^= keys.piece_square_color[dst][piece_at_dst][color.opponent()] } pub fn update_en_passant(&mut self, src: usize, dst: usize, ep_capture: usize, color: Color) { let keys = zobrist_keys(); - self.hash ^= keys.piece_square_color[src][PieceType::Pawn.idx()][color.idx()]; - self.hash ^= keys.piece_square_color[dst][PieceType::Pawn.idx()][color.idx()]; - self.hash ^= - keys.piece_square_color[ep_capture][PieceType::Pawn.idx()][color.opponent().idx()] + self.hash ^= keys.piece_square_color[src][PieceType::Pawn][color]; + self.hash ^= keys.piece_square_color[dst][PieceType::Pawn][color]; + self.hash ^= keys.piece_square_color[ep_capture][PieceType::Pawn][color.opponent()] } pub fn update_double_push( @@ -167,8 +166,8 @@ impl ZobristHash { new_en_passant_target: Option, ) { let keys = zobrist_keys(); - self.hash ^= keys.piece_square_color[src][PieceType::Pawn.idx()][color.idx()]; - self.hash ^= keys.piece_square_color[dst][PieceType::Pawn.idx()][color.idx()]; + self.hash ^= keys.piece_square_color[src][PieceType::Pawn][color]; + self.hash ^= keys.piece_square_color[dst][PieceType::Pawn][color]; if let Some(new_en_passant) = new_en_passant_target { self.hash ^= keys.en_passant[square::to_file(new_en_passant)]; @@ -177,8 +176,8 @@ impl ZobristHash { pub fn update_promotion(&mut self, src: usize, dst: usize, promote: &Promote, color: Color) { let keys = zobrist_keys(); - self.hash ^= keys.piece_square_color[src][PieceType::Pawn.idx()][color.idx()]; - self.hash ^= keys.piece_square_color[dst][promote.into_piece_type().idx()][color.idx()]; + self.hash ^= keys.piece_square_color[src][PieceType::Pawn][color]; + self.hash ^= keys.piece_square_color[dst][promote.into_piece_type()][color]; } pub fn update_promotion_capture( @@ -190,9 +189,9 @@ impl ZobristHash { color: Color, ) { let keys = zobrist_keys(); - self.hash ^= keys.piece_square_color[src][PieceType::Pawn.idx()][color.idx()]; - self.hash ^= keys.piece_square_color[dst][piece_at_dst.idx()][color.opponent().idx()]; - self.hash ^= keys.piece_square_color[dst][promote.into_piece_type().idx()][color.idx()]; + self.hash ^= keys.piece_square_color[src][PieceType::Pawn][color]; + self.hash ^= keys.piece_square_color[dst][piece_at_dst][color.opponent()]; + self.hash ^= keys.piece_square_color[dst][promote.into_piece_type()][color]; } pub fn update_castle( @@ -205,10 +204,10 @@ impl ZobristHash { color: Color, ) { let keys = zobrist_keys(); - self.hash ^= keys.piece_square_color[src][piece_at_src.idx()][color.idx()]; - self.hash ^= keys.piece_square_color[dst][piece_at_src.idx()][color.idx()]; - self.hash ^= keys.piece_square_color[rook_src][PieceType::Rook.idx()][color.idx()]; - self.hash ^= keys.piece_square_color[rook_dst][PieceType::Rook.idx()][color.idx()]; + self.hash ^= keys.piece_square_color[src][piece_at_src][color]; + self.hash ^= keys.piece_square_color[dst][piece_at_src][color]; + self.hash ^= keys.piece_square_color[rook_src][PieceType::Rook][color]; + self.hash ^= keys.piece_square_color[rook_dst][PieceType::Rook][color]; } pub fn drop_en_passant_hash(&mut self, en_passant: Option) { diff --git a/src/search/move_ordering.rs b/src/search/move_ordering.rs index 6212eb0..3fb0793 100644 --- a/src/search/move_ordering.rs +++ b/src/search/move_ordering.rs @@ -5,7 +5,7 @@ use crate::{ pub fn sort_moves(mut moves: Vec, mailbox: &Mailbox, tt_move: Option) -> Vec { if let Some(tt_move) = tt_move { if let Some(tt_move_index) = moves.iter().position(|&mv| mv == tt_move) { - moves.remove(tt_move_index); + moves.swap_remove(tt_move_index); moves.insert(0, tt_move); return moves; }