Rename fn names for better readability
This commit is contained in:
@@ -183,13 +183,6 @@ impl Board {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn all_pieces(&mut self) -> (&mut [Piece; 6], &mut [Piece; 6]) {
|
||||
match self.state.current_player() {
|
||||
Color::White => (&mut self.white_pieces, &mut self.black_pieces),
|
||||
Color::Black => (&mut self.black_pieces, &mut self.white_pieces),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_piece(&mut self, src: usize, dst: usize, piece_type: PieceType) {
|
||||
let pieces = self.own_pieces();
|
||||
pieces[piece_type].bitboard &= !square_to_bitboard(src);
|
||||
|
||||
@@ -26,7 +26,7 @@ pub fn from_fen(fen: &str) -> Result<Game, FenError> {
|
||||
fullmove_counter,
|
||||
));
|
||||
|
||||
let mailbox = Mailbox::new_from_board(&board);
|
||||
let mailbox = Mailbox::from_board(&board);
|
||||
let hash = zobrist_keys().calculate_hash(&board);
|
||||
|
||||
Ok(Game {
|
||||
|
||||
@@ -34,12 +34,12 @@ impl Game {
|
||||
Self {
|
||||
board: Board::new(),
|
||||
history: History::new(),
|
||||
mailbox: Mailbox::new_from_board(&Board::new()),
|
||||
mailbox: Mailbox::from_board(&Board::new()),
|
||||
hash: zobrist_keys().calculate_hash(&Board::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_from_fen(fen: &str) -> Result<Self, FenError> {
|
||||
pub fn from_fen(fen: &str) -> Result<Self, FenError> {
|
||||
from_fen(fen)
|
||||
}
|
||||
|
||||
@@ -64,9 +64,9 @@ impl Game {
|
||||
let old_castling_ability = board.state.castling_ability;
|
||||
|
||||
let piece_at_src = mailbox
|
||||
.find_piece_at(mv.src)
|
||||
.piece_at(mv.src)
|
||||
.expect("Expected piece at: {mv.src}");
|
||||
let piece_at_dst = mailbox.find_piece_at(mv.dst);
|
||||
let piece_at_dst = mailbox.piece_at(mv.dst);
|
||||
match &mv.move_type {
|
||||
MoveType::Quiet => {
|
||||
board.move_piece(mv.src, mv.dst, piece_at_src);
|
||||
@@ -83,7 +83,7 @@ impl Game {
|
||||
MoveType::EnPassant => {
|
||||
board.move_piece(mv.src, mv.dst, piece_at_src);
|
||||
let piece_at_capture = mailbox
|
||||
.find_piece_at(capture_square)
|
||||
.piece_at(capture_square)
|
||||
.expect("Expected piece at: {capture_square}");
|
||||
board.remove_opponent_piece(capture_square, PieceType::Pawn);
|
||||
hash.update_en_passant(mv.src, mv.dst, piece_at_src, piece_at_capture, color);
|
||||
@@ -182,11 +182,11 @@ impl Game {
|
||||
}
|
||||
|
||||
let mv = move_parameters.mv.unwrap();
|
||||
let piece_at_dst = mailbox.find_piece_at(mv.dst).expect("Expected set piece");
|
||||
let piece_at_dst = mailbox.piece_at(mv.dst).expect("Expected set piece");
|
||||
match &mv.move_type {
|
||||
MoveType::Quiet | MoveType::DoublePush => {
|
||||
board.move_piece(mv.dst, mv.src, piece_at_dst);
|
||||
mailbox.set_piece_at(mv.src, mailbox.find_piece_at(mv.dst));
|
||||
mailbox.set_piece_at(mv.src, mailbox.piece_at(mv.dst));
|
||||
mailbox.set_piece_at(mv.dst, None);
|
||||
}
|
||||
MoveType::Capture => {
|
||||
@@ -240,7 +240,7 @@ impl Game {
|
||||
board.move_piece(mv.dst, mv.src, piece_at_dst);
|
||||
board.remove_own_piece(rook_dst, PieceType::Rook);
|
||||
board.insert_own_piece(rook_src, PieceType::Rook);
|
||||
mailbox.set_piece_at(mv.src, mailbox.find_piece_at(mv.dst));
|
||||
mailbox.set_piece_at(mv.src, mailbox.piece_at(mv.dst));
|
||||
mailbox.set_piece_at(mv.dst, None);
|
||||
mailbox.set_piece_at(rook_src, Some(PieceType::Rook));
|
||||
mailbox.set_piece_at(rook_dst, None);
|
||||
@@ -283,7 +283,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_make_move_capture() -> Result<(), String> {
|
||||
let mut game = from_fen(FEN)?;
|
||||
let f3f5 = Move::new_with_type(Square::F3, Square::F5, MoveType::Capture);
|
||||
let f3f5 = Move::with_type(Square::F3, Square::F5, MoveType::Capture);
|
||||
game.make_move(&f3f5);
|
||||
|
||||
assert_eq!(game, from_fen(FEN_CAPTURE)?);
|
||||
@@ -297,7 +297,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_make_move_en_passant() -> Result<(), String> {
|
||||
let mut game = from_fen(FEN)?;
|
||||
let h5g6 = Move::new_with_type(Square::H5, Square::G6, MoveType::EnPassant);
|
||||
let h5g6 = Move::with_type(Square::H5, Square::G6, MoveType::EnPassant);
|
||||
game.make_move(&h5g6);
|
||||
|
||||
assert_eq!(game, from_fen(FEN_EN_PASSANT)?);
|
||||
@@ -311,7 +311,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_make_move_double_push() -> Result<(), String> {
|
||||
let mut game = from_fen(FEN)?;
|
||||
let b2b4 = Move::new_with_type(Square::B2, Square::B4, MoveType::DoublePush);
|
||||
let b2b4 = Move::with_type(Square::B2, Square::B4, MoveType::DoublePush);
|
||||
game.make_move(&b2b4);
|
||||
assert_eq!(game, from_fen(FEN_DOUBLE_PUSH)?);
|
||||
|
||||
@@ -323,7 +323,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_make_move_promotion() -> Result<(), String> {
|
||||
let mut game = from_fen(FEN)?;
|
||||
let c7c8 = Move::new_with_type(Square::C7, Square::C8, MoveType::Promotion(Promote::Queen));
|
||||
let c7c8 = Move::with_type(Square::C7, Square::C8, MoveType::Promotion(Promote::Queen));
|
||||
game.make_move(&c7c8);
|
||||
assert_eq!(game, from_fen(FEN_PROMOTION)?);
|
||||
|
||||
@@ -336,7 +336,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_make_move_promotion_capture() -> Result<(), String> {
|
||||
let mut game = from_fen(FEN)?;
|
||||
let c7b8 = Move::new_with_type(
|
||||
let c7b8 = Move::with_type(
|
||||
Square::C7,
|
||||
Square::B8,
|
||||
MoveType::PromotionCapture(Promote::Queen),
|
||||
@@ -352,7 +352,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_make_move_castle() -> Result<(), String> {
|
||||
let mut game = from_fen(FEN)?;
|
||||
let e1g1 = Move::new_with_type(Square::E1, Square::G1, MoveType::Castle);
|
||||
let e1g1 = Move::with_type(Square::E1, Square::G1, MoveType::Castle);
|
||||
game.make_move(&e1g1);
|
||||
assert_eq!(game, from_fen(FEN_CASTLE)?);
|
||||
Ok(())
|
||||
@@ -365,13 +365,13 @@ mod tests {
|
||||
fn test_unmake_quiet_and_double_push() -> Result<(), String> {
|
||||
let mut game = from_fen(FEN_1)?;
|
||||
let game_before_make = game.clone();
|
||||
let mv = Move::new_with_type(Square::B2, Square::B3, MoveType::Quiet);
|
||||
let mv = Move::with_type(Square::B2, Square::B3, MoveType::Quiet);
|
||||
game.make_move(&mv);
|
||||
game.unmake_move();
|
||||
|
||||
assert_eq!(game_before_make, game);
|
||||
|
||||
let mv = Move::new_with_type(Square::B2, Square::B4, MoveType::DoublePush);
|
||||
let mv = Move::with_type(Square::B2, Square::B4, MoveType::DoublePush);
|
||||
game.make_move(&mv);
|
||||
game.unmake_move();
|
||||
|
||||
@@ -384,19 +384,19 @@ mod tests {
|
||||
fn test_unmake_capture_and_promotion() -> Result<(), String> {
|
||||
let mut game = from_fen(FEN_1)?;
|
||||
let game_before_make = game.clone();
|
||||
let mv = Move::new_with_type(Square::D3, Square::B5, MoveType::Capture);
|
||||
let mv = Move::with_type(Square::D3, Square::B5, MoveType::Capture);
|
||||
game.make_move(&mv);
|
||||
game.unmake_move();
|
||||
|
||||
assert_eq!(game_before_make, game);
|
||||
|
||||
let mv = Move::new_with_type(Square::C7, Square::C8, MoveType::Promotion(Promote::Queen));
|
||||
let mv = Move::with_type(Square::C7, Square::C8, MoveType::Promotion(Promote::Queen));
|
||||
game.make_move(&mv);
|
||||
game.unmake_move();
|
||||
|
||||
assert_eq!(game_before_make, game);
|
||||
|
||||
let mv = Move::new_with_type(
|
||||
let mv = Move::with_type(
|
||||
Square::C7,
|
||||
Square::B8,
|
||||
MoveType::PromotionCapture(Promote::Queen),
|
||||
@@ -413,7 +413,7 @@ mod tests {
|
||||
fn test_unmake_en_passant() -> Result<(), String> {
|
||||
let mut game = from_fen(FEN_2)?;
|
||||
let game_before_make = game.clone();
|
||||
let mv = Move::new_with_type(Square::A4, Square::B3, MoveType::EnPassant);
|
||||
let mv = Move::with_type(Square::A4, Square::B3, MoveType::EnPassant);
|
||||
game.make_move(&mv);
|
||||
game.unmake_move();
|
||||
|
||||
@@ -426,7 +426,7 @@ mod tests {
|
||||
fn test_unmake_castle() -> Result<(), String> {
|
||||
let mut game = from_fen(FEN_1)?;
|
||||
let game_before_make = game.clone();
|
||||
let mv = Move::new_with_type(Square::E1, Square::C1, MoveType::Castle);
|
||||
let mv = Move::with_type(Square::E1, Square::C1, MoveType::Castle);
|
||||
game.make_move(&mv);
|
||||
game.unmake_move();
|
||||
|
||||
|
||||
@@ -78,12 +78,12 @@ impl MoveParameters {
|
||||
}
|
||||
|
||||
fn add_moved_piece(&mut self, mailbox: &Mailbox, mv: &Move) {
|
||||
self.moved_piece = mailbox.find_piece_at(mv.src);
|
||||
self.moved_piece = mailbox.piece_at(mv.src);
|
||||
}
|
||||
|
||||
fn add_captured_piece(&mut self, mailbox: &Mailbox, mv: &Move) {
|
||||
if let MoveType::Capture | MoveType::PromotionCapture(_) = mv.move_type {
|
||||
self.captured_piece = mailbox.find_piece_at(mv.dst)
|
||||
self.captured_piece = mailbox.piece_at(mv.dst)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,13 +10,7 @@ pub struct Mailbox {
|
||||
}
|
||||
|
||||
impl Mailbox {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
mailbox: [None; 64],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_from_board(board: &Board) -> Self {
|
||||
pub fn from_board(board: &Board) -> Self {
|
||||
let mut mailbox: [Option<PieceType>; 64] = [None; 64];
|
||||
for square in Square::A1..=Square::H8 {
|
||||
mailbox[square] = board
|
||||
@@ -34,7 +28,7 @@ impl Mailbox {
|
||||
self.mailbox[square] = piece_type;
|
||||
}
|
||||
|
||||
pub const fn find_piece_at(&self, square: usize) -> Option<PieceType> {
|
||||
pub const fn piece_at(&self, square: usize) -> Option<PieceType> {
|
||||
self.mailbox[square]
|
||||
}
|
||||
}
|
||||
@@ -49,9 +43,9 @@ mod tests {
|
||||
const FEN_STARTPOS: &str = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||
|
||||
#[test]
|
||||
fn test_new_from_board() -> Result<(), String> {
|
||||
fn test_from_board() -> Result<(), String> {
|
||||
let game = from_fen(FEN_MATE_IN_1)?;
|
||||
let mailbox = Mailbox::new_from_board(&game.board);
|
||||
let mailbox = Mailbox::from_board(&game.board);
|
||||
#[rustfmt::skip]
|
||||
let expected: [Option<PieceType>; 64] = [
|
||||
None, None, None, None, None, Some(PieceType::King), None, None,
|
||||
@@ -67,7 +61,7 @@ mod tests {
|
||||
assert_eq!(expected, mailbox.mailbox);
|
||||
|
||||
let game = from_fen(FEN_STARTPOS)?;
|
||||
let mailbox = Mailbox::new_from_board(&game.board);
|
||||
let mailbox = Mailbox::from_board(&game.board);
|
||||
#[rustfmt::skip]
|
||||
let expected: [Option<PieceType>; 64] = [
|
||||
Some(PieceType::Rook), Some(PieceType::Knight), Some(PieceType::Bishop), Some(PieceType::Queen),
|
||||
@@ -90,11 +84,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_piece_at() -> Result<(), String> {
|
||||
fn test_piece_at() -> Result<(), String> {
|
||||
let game = from_fen(FEN_MATE_IN_1)?;
|
||||
let mailbox = Mailbox::new_from_board(&game.board);
|
||||
let mailbox = Mailbox::from_board(&game.board);
|
||||
|
||||
assert_eq!(PieceType::King, mailbox.find_piece_at(Square::F1).unwrap());
|
||||
assert_eq!(PieceType::King, mailbox.piece_at(Square::F1).unwrap());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -74,15 +74,15 @@ impl Square {
|
||||
pub const H8: usize = 63;
|
||||
}
|
||||
|
||||
pub const fn coords_to_square(rank: usize, file: usize) -> usize {
|
||||
pub const fn from_coords(rank: usize, file: usize) -> usize {
|
||||
rank * 8 + file
|
||||
}
|
||||
|
||||
pub const fn square_to_file(square: usize) -> usize {
|
||||
pub const fn to_file(square: usize) -> usize {
|
||||
square % 8
|
||||
}
|
||||
|
||||
pub fn square_to_algebraic(square: usize) -> String {
|
||||
pub fn to_algebraic(square: usize) -> String {
|
||||
let file = (square % 8) as u8;
|
||||
let rank = (square / 8) as u8;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ use crate::movegen::r#move::Promote;
|
||||
use super::{
|
||||
bitboard::lsb,
|
||||
board::{self, Board, Color, PieceType},
|
||||
square::{self, square_to_file, Square},
|
||||
square::{self, to_file, Square},
|
||||
state::Castle,
|
||||
};
|
||||
use rand::{rngs::SmallRng, RngCore, SeedableRng};
|
||||
@@ -86,7 +86,7 @@ impl ZobristKeys {
|
||||
hash ^= self.castling_ability[board.state.castling_ability[1].idx()][1];
|
||||
|
||||
if let Some(ep) = board.state.en_passant_square {
|
||||
hash ^= self.en_passant[square_to_file(ep)];
|
||||
hash ^= self.en_passant[to_file(ep)];
|
||||
}
|
||||
|
||||
ZobristHash::new(hash)
|
||||
@@ -121,7 +121,7 @@ impl ZobristHash {
|
||||
|
||||
pub fn update_en_passant_keys(&mut self, old_en_passant: usize) {
|
||||
let keys = zobrist_keys();
|
||||
self.hash ^= keys.en_passant[square::square_to_file(old_en_passant)]
|
||||
self.hash ^= keys.en_passant[square::to_file(old_en_passant)]
|
||||
}
|
||||
|
||||
pub fn update_castling_ability_keys(
|
||||
@@ -183,7 +183,7 @@ impl ZobristHash {
|
||||
self.hash ^= keys.piece_square_color[dst][piece_at_src.idx()][color.idx()];
|
||||
|
||||
if let Some(new_en_passant) = new_en_passant_target {
|
||||
self.hash ^= keys.en_passant[square::square_to_file(new_en_passant)];
|
||||
self.hash ^= keys.en_passant[square::to_file(new_en_passant)];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user