Implement Index<T>, IndexMut<T> for Castle, PieceType and Color enums, use swap_remove instead of remove on sort_moves
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
use u64 as Bitboard;
|
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::board::state::State;
|
||||||
use crate::movegen::attack_generator::{
|
use crate::movegen::attack_generator::{
|
||||||
fetch_bishop_attacks, fetch_king_attacks, fetch_knight_attacks, fetch_pawn_attacks,
|
fetch_bishop_attacks, fetch_king_attacks, fetch_knight_attacks, fetch_pawn_attacks,
|
||||||
@@ -272,17 +272,15 @@ impl PieceType {
|
|||||||
}
|
}
|
||||||
use std::ops::{Index, IndexMut};
|
use std::ops::{Index, IndexMut};
|
||||||
|
|
||||||
use super::bitboard::square_to_bitboard;
|
impl<T> Index<PieceType> for [T] {
|
||||||
|
type Output = T;
|
||||||
impl Index<PieceType> for [Piece] {
|
|
||||||
type Output = Piece;
|
|
||||||
|
|
||||||
fn index(&self, piece_type: PieceType) -> &Self::Output {
|
fn index(&self, piece_type: PieceType) -> &Self::Output {
|
||||||
&self[piece_type.idx()]
|
&self[piece_type.idx()]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IndexMut<PieceType> for [Piece] {
|
impl<T> IndexMut<PieceType> for [T] {
|
||||||
fn index_mut(&mut self, piece_type: PieceType) -> &mut Self::Output {
|
fn index_mut(&mut self, piece_type: PieceType) -> &mut Self::Output {
|
||||||
&mut self[piece_type.idx()]
|
&mut self[piece_type.idx()]
|
||||||
}
|
}
|
||||||
@@ -307,6 +305,20 @@ impl Color {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Index<Color> for [T] {
|
||||||
|
type Output = T;
|
||||||
|
|
||||||
|
fn index(&self, color: Color) -> &Self::Output {
|
||||||
|
&self[color.idx()]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> IndexMut<Color> for [T] {
|
||||||
|
fn index_mut(&mut self, color: Color) -> &mut Self::Output {
|
||||||
|
&mut self[color.idx()]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{board::fen::from_fen, movegen::attack_generator::init_attacks};
|
use crate::{board::fen::from_fen, movegen::attack_generator::init_attacks};
|
||||||
|
|||||||
@@ -164,3 +164,19 @@ impl Castle {
|
|||||||
self as usize
|
self as usize
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use std::ops::{Index, IndexMut};
|
||||||
|
|
||||||
|
impl<T> Index<Castle> for [T] {
|
||||||
|
type Output = T;
|
||||||
|
|
||||||
|
fn index(&self, castle: Castle) -> &Self::Output {
|
||||||
|
&self[castle.idx()]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> IndexMut<Castle> for [T] {
|
||||||
|
fn index_mut(&mut self, castle: Castle) -> &mut Self::Output {
|
||||||
|
&mut self[castle.idx()]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ impl ZobristKeys {
|
|||||||
|
|
||||||
while bb != 0 {
|
while bb != 0 {
|
||||||
let square = lsb(bb);
|
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;
|
bb &= bb - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -73,7 +73,7 @@ impl ZobristKeys {
|
|||||||
|
|
||||||
while bb != 0 {
|
while bb != 0 {
|
||||||
let square = lsb(bb);
|
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;
|
bb &= bb - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,8 +82,8 @@ impl ZobristKeys {
|
|||||||
hash ^= self.side_to_move
|
hash ^= self.side_to_move
|
||||||
}
|
}
|
||||||
|
|
||||||
hash ^= self.castling_ability[board.state.castling_ability[0].idx()][0];
|
hash ^= self.castling_ability[board.state.castling_ability[0]][0];
|
||||||
hash ^= self.castling_ability[board.state.castling_ability[1].idx()][1];
|
hash ^= self.castling_ability[board.state.castling_ability[1]][1];
|
||||||
|
|
||||||
if let Some(ep) = board.state.en_passant_square {
|
if let Some(ep) = board.state.en_passant_square {
|
||||||
hash ^= self.en_passant[square::to_file(ep)];
|
hash ^= self.en_passant[square::to_file(ep)];
|
||||||
@@ -125,16 +125,16 @@ impl ZobristHash {
|
|||||||
new_castling_ability: [Castle; 2],
|
new_castling_ability: [Castle; 2],
|
||||||
) {
|
) {
|
||||||
let keys = zobrist_keys();
|
let keys = zobrist_keys();
|
||||||
self.hash ^= keys.castling_ability[old_castling_ability[0].idx()][0];
|
self.hash ^= keys.castling_ability[old_castling_ability[0]][0];
|
||||||
self.hash ^= keys.castling_ability[old_castling_ability[1].idx()][1];
|
self.hash ^= keys.castling_ability[old_castling_ability[1]][1];
|
||||||
self.hash ^= keys.castling_ability[new_castling_ability[0].idx()][0];
|
self.hash ^= keys.castling_ability[new_castling_ability[0]][0];
|
||||||
self.hash ^= keys.castling_ability[new_castling_ability[1].idx()][1]
|
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) {
|
pub fn update_quiet(&mut self, src: usize, dst: usize, piece_at_src: PieceType, color: Color) {
|
||||||
let keys = zobrist_keys();
|
let keys = zobrist_keys();
|
||||||
self.hash ^= keys.piece_square_color[src][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.idx()][color.idx()];
|
self.hash ^= keys.piece_square_color[dst][piece_at_src][color];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_capture(
|
pub fn update_capture(
|
||||||
@@ -146,17 +146,16 @@ impl ZobristHash {
|
|||||||
color: Color,
|
color: Color,
|
||||||
) {
|
) {
|
||||||
let keys = zobrist_keys();
|
let keys = zobrist_keys();
|
||||||
self.hash ^= keys.piece_square_color[src][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.idx()][color.idx()];
|
self.hash ^= keys.piece_square_color[dst][piece_at_src][color];
|
||||||
self.hash ^= keys.piece_square_color[dst][piece_at_dst.idx()][color.opponent().idx()]
|
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) {
|
pub fn update_en_passant(&mut self, src: usize, dst: usize, ep_capture: usize, color: Color) {
|
||||||
let keys = zobrist_keys();
|
let keys = zobrist_keys();
|
||||||
self.hash ^= keys.piece_square_color[src][PieceType::Pawn.idx()][color.idx()];
|
self.hash ^= keys.piece_square_color[src][PieceType::Pawn][color];
|
||||||
self.hash ^= keys.piece_square_color[dst][PieceType::Pawn.idx()][color.idx()];
|
self.hash ^= keys.piece_square_color[dst][PieceType::Pawn][color];
|
||||||
self.hash ^=
|
self.hash ^= keys.piece_square_color[ep_capture][PieceType::Pawn][color.opponent()]
|
||||||
keys.piece_square_color[ep_capture][PieceType::Pawn.idx()][color.opponent().idx()]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_double_push(
|
pub fn update_double_push(
|
||||||
@@ -167,8 +166,8 @@ impl ZobristHash {
|
|||||||
new_en_passant_target: Option<usize>,
|
new_en_passant_target: Option<usize>,
|
||||||
) {
|
) {
|
||||||
let keys = zobrist_keys();
|
let keys = zobrist_keys();
|
||||||
self.hash ^= keys.piece_square_color[src][PieceType::Pawn.idx()][color.idx()];
|
self.hash ^= keys.piece_square_color[src][PieceType::Pawn][color];
|
||||||
self.hash ^= keys.piece_square_color[dst][PieceType::Pawn.idx()][color.idx()];
|
self.hash ^= keys.piece_square_color[dst][PieceType::Pawn][color];
|
||||||
|
|
||||||
if let Some(new_en_passant) = new_en_passant_target {
|
if let Some(new_en_passant) = new_en_passant_target {
|
||||||
self.hash ^= keys.en_passant[square::to_file(new_en_passant)];
|
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) {
|
pub fn update_promotion(&mut self, src: usize, dst: usize, promote: &Promote, color: Color) {
|
||||||
let keys = zobrist_keys();
|
let keys = zobrist_keys();
|
||||||
self.hash ^= keys.piece_square_color[src][PieceType::Pawn.idx()][color.idx()];
|
self.hash ^= keys.piece_square_color[src][PieceType::Pawn][color];
|
||||||
self.hash ^= keys.piece_square_color[dst][promote.into_piece_type().idx()][color.idx()];
|
self.hash ^= keys.piece_square_color[dst][promote.into_piece_type()][color];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_promotion_capture(
|
pub fn update_promotion_capture(
|
||||||
@@ -190,9 +189,9 @@ impl ZobristHash {
|
|||||||
color: Color,
|
color: Color,
|
||||||
) {
|
) {
|
||||||
let keys = zobrist_keys();
|
let keys = zobrist_keys();
|
||||||
self.hash ^= keys.piece_square_color[src][PieceType::Pawn.idx()][color.idx()];
|
self.hash ^= keys.piece_square_color[src][PieceType::Pawn][color];
|
||||||
self.hash ^= keys.piece_square_color[dst][piece_at_dst.idx()][color.opponent().idx()];
|
self.hash ^= keys.piece_square_color[dst][piece_at_dst][color.opponent()];
|
||||||
self.hash ^= keys.piece_square_color[dst][promote.into_piece_type().idx()][color.idx()];
|
self.hash ^= keys.piece_square_color[dst][promote.into_piece_type()][color];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_castle(
|
pub fn update_castle(
|
||||||
@@ -205,10 +204,10 @@ impl ZobristHash {
|
|||||||
color: Color,
|
color: Color,
|
||||||
) {
|
) {
|
||||||
let keys = zobrist_keys();
|
let keys = zobrist_keys();
|
||||||
self.hash ^= keys.piece_square_color[src][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.idx()][color.idx()];
|
self.hash ^= keys.piece_square_color[dst][piece_at_src][color];
|
||||||
self.hash ^= keys.piece_square_color[rook_src][PieceType::Rook.idx()][color.idx()];
|
self.hash ^= keys.piece_square_color[rook_src][PieceType::Rook][color];
|
||||||
self.hash ^= keys.piece_square_color[rook_dst][PieceType::Rook.idx()][color.idx()];
|
self.hash ^= keys.piece_square_color[rook_dst][PieceType::Rook][color];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn drop_en_passant_hash(&mut self, en_passant: Option<usize>) {
|
pub fn drop_en_passant_hash(&mut self, en_passant: Option<usize>) {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use crate::{
|
|||||||
pub fn sort_moves(mut moves: Vec<Move>, mailbox: &Mailbox, tt_move: Option<Move>) -> Vec<Move> {
|
pub fn sort_moves(mut moves: Vec<Move>, mailbox: &Mailbox, tt_move: Option<Move>) -> Vec<Move> {
|
||||||
if let Some(tt_move) = tt_move {
|
if let Some(tt_move) = tt_move {
|
||||||
if let Some(tt_move_index) = moves.iter().position(|&mv| mv == 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);
|
moves.insert(0, tt_move);
|
||||||
return moves;
|
return moves;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user