Rename fn names for better readability

This commit is contained in:
stefiosif
2024-10-06 11:15:29 +03:00
parent 28409d203a
commit 3fd3b1ea96
12 changed files with 102 additions and 121 deletions

View File

@@ -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) { pub fn move_piece(&mut self, src: usize, dst: usize, piece_type: PieceType) {
let pieces = self.own_pieces(); let pieces = self.own_pieces();
pieces[piece_type].bitboard &= !square_to_bitboard(src); pieces[piece_type].bitboard &= !square_to_bitboard(src);

View File

@@ -26,7 +26,7 @@ pub fn from_fen(fen: &str) -> Result<Game, FenError> {
fullmove_counter, fullmove_counter,
)); ));
let mailbox = Mailbox::new_from_board(&board); let mailbox = Mailbox::from_board(&board);
let hash = zobrist_keys().calculate_hash(&board); let hash = zobrist_keys().calculate_hash(&board);
Ok(Game { Ok(Game {

View File

@@ -34,12 +34,12 @@ impl Game {
Self { Self {
board: Board::new(), board: Board::new(),
history: History::new(), history: History::new(),
mailbox: Mailbox::new_from_board(&Board::new()), mailbox: Mailbox::from_board(&Board::new()),
hash: zobrist_keys().calculate_hash(&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) from_fen(fen)
} }
@@ -64,9 +64,9 @@ impl Game {
let old_castling_ability = board.state.castling_ability; let old_castling_ability = board.state.castling_ability;
let piece_at_src = mailbox let piece_at_src = mailbox
.find_piece_at(mv.src) .piece_at(mv.src)
.expect("Expected 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 { match &mv.move_type {
MoveType::Quiet => { MoveType::Quiet => {
board.move_piece(mv.src, mv.dst, piece_at_src); board.move_piece(mv.src, mv.dst, piece_at_src);
@@ -83,7 +83,7 @@ impl Game {
MoveType::EnPassant => { MoveType::EnPassant => {
board.move_piece(mv.src, mv.dst, piece_at_src); board.move_piece(mv.src, mv.dst, piece_at_src);
let piece_at_capture = mailbox let piece_at_capture = mailbox
.find_piece_at(capture_square) .piece_at(capture_square)
.expect("Expected piece at: {capture_square}"); .expect("Expected piece at: {capture_square}");
board.remove_opponent_piece(capture_square, PieceType::Pawn); board.remove_opponent_piece(capture_square, PieceType::Pawn);
hash.update_en_passant(mv.src, mv.dst, piece_at_src, piece_at_capture, color); 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 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 { match &mv.move_type {
MoveType::Quiet | MoveType::DoublePush => { MoveType::Quiet | MoveType::DoublePush => {
board.move_piece(mv.dst, mv.src, piece_at_dst); 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); mailbox.set_piece_at(mv.dst, None);
} }
MoveType::Capture => { MoveType::Capture => {
@@ -240,7 +240,7 @@ impl Game {
board.move_piece(mv.dst, mv.src, piece_at_dst); board.move_piece(mv.dst, mv.src, piece_at_dst);
board.remove_own_piece(rook_dst, PieceType::Rook); board.remove_own_piece(rook_dst, PieceType::Rook);
board.insert_own_piece(rook_src, 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(mv.dst, None);
mailbox.set_piece_at(rook_src, Some(PieceType::Rook)); mailbox.set_piece_at(rook_src, Some(PieceType::Rook));
mailbox.set_piece_at(rook_dst, None); mailbox.set_piece_at(rook_dst, None);
@@ -283,7 +283,7 @@ mod tests {
#[test] #[test]
fn test_make_move_capture() -> Result<(), String> { fn test_make_move_capture() -> Result<(), String> {
let mut game = from_fen(FEN)?; 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); game.make_move(&f3f5);
assert_eq!(game, from_fen(FEN_CAPTURE)?); assert_eq!(game, from_fen(FEN_CAPTURE)?);
@@ -297,7 +297,7 @@ mod tests {
#[test] #[test]
fn test_make_move_en_passant() -> Result<(), String> { fn test_make_move_en_passant() -> Result<(), String> {
let mut game = from_fen(FEN)?; 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); game.make_move(&h5g6);
assert_eq!(game, from_fen(FEN_EN_PASSANT)?); assert_eq!(game, from_fen(FEN_EN_PASSANT)?);
@@ -311,7 +311,7 @@ mod tests {
#[test] #[test]
fn test_make_move_double_push() -> Result<(), String> { fn test_make_move_double_push() -> Result<(), String> {
let mut game = from_fen(FEN)?; 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); game.make_move(&b2b4);
assert_eq!(game, from_fen(FEN_DOUBLE_PUSH)?); assert_eq!(game, from_fen(FEN_DOUBLE_PUSH)?);
@@ -323,7 +323,7 @@ mod tests {
#[test] #[test]
fn test_make_move_promotion() -> Result<(), String> { fn test_make_move_promotion() -> Result<(), String> {
let mut game = from_fen(FEN)?; 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); game.make_move(&c7c8);
assert_eq!(game, from_fen(FEN_PROMOTION)?); assert_eq!(game, from_fen(FEN_PROMOTION)?);
@@ -336,7 +336,7 @@ mod tests {
#[test] #[test]
fn test_make_move_promotion_capture() -> Result<(), String> { fn test_make_move_promotion_capture() -> Result<(), String> {
let mut game = from_fen(FEN)?; let mut game = from_fen(FEN)?;
let c7b8 = Move::new_with_type( let c7b8 = Move::with_type(
Square::C7, Square::C7,
Square::B8, Square::B8,
MoveType::PromotionCapture(Promote::Queen), MoveType::PromotionCapture(Promote::Queen),
@@ -352,7 +352,7 @@ mod tests {
#[test] #[test]
fn test_make_move_castle() -> Result<(), String> { fn test_make_move_castle() -> Result<(), String> {
let mut game = from_fen(FEN)?; 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); game.make_move(&e1g1);
assert_eq!(game, from_fen(FEN_CASTLE)?); assert_eq!(game, from_fen(FEN_CASTLE)?);
Ok(()) Ok(())
@@ -365,13 +365,13 @@ mod tests {
fn test_unmake_quiet_and_double_push() -> Result<(), String> { fn test_unmake_quiet_and_double_push() -> Result<(), String> {
let mut game = from_fen(FEN_1)?; let mut game = from_fen(FEN_1)?;
let game_before_make = game.clone(); 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.make_move(&mv);
game.unmake_move(); game.unmake_move();
assert_eq!(game_before_make, game); 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.make_move(&mv);
game.unmake_move(); game.unmake_move();
@@ -384,19 +384,19 @@ mod tests {
fn test_unmake_capture_and_promotion() -> Result<(), String> { fn test_unmake_capture_and_promotion() -> Result<(), String> {
let mut game = from_fen(FEN_1)?; let mut game = from_fen(FEN_1)?;
let game_before_make = game.clone(); 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.make_move(&mv);
game.unmake_move(); game.unmake_move();
assert_eq!(game_before_make, game); 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.make_move(&mv);
game.unmake_move(); game.unmake_move();
assert_eq!(game_before_make, game); assert_eq!(game_before_make, game);
let mv = Move::new_with_type( let mv = Move::with_type(
Square::C7, Square::C7,
Square::B8, Square::B8,
MoveType::PromotionCapture(Promote::Queen), MoveType::PromotionCapture(Promote::Queen),
@@ -413,7 +413,7 @@ mod tests {
fn test_unmake_en_passant() -> Result<(), String> { fn test_unmake_en_passant() -> Result<(), String> {
let mut game = from_fen(FEN_2)?; let mut game = from_fen(FEN_2)?;
let game_before_make = game.clone(); 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.make_move(&mv);
game.unmake_move(); game.unmake_move();
@@ -426,7 +426,7 @@ mod tests {
fn test_unmake_castle() -> Result<(), String> { fn test_unmake_castle() -> Result<(), String> {
let mut game = from_fen(FEN_1)?; let mut game = from_fen(FEN_1)?;
let game_before_make = game.clone(); 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.make_move(&mv);
game.unmake_move(); game.unmake_move();

View File

@@ -78,12 +78,12 @@ impl MoveParameters {
} }
fn add_moved_piece(&mut self, mailbox: &Mailbox, mv: &Move) { 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) { fn add_captured_piece(&mut self, mailbox: &Mailbox, mv: &Move) {
if let MoveType::Capture | MoveType::PromotionCapture(_) = mv.move_type { 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)
} }
} }

View File

@@ -10,13 +10,7 @@ pub struct Mailbox {
} }
impl Mailbox { impl Mailbox {
pub const fn new() -> Self { pub fn from_board(board: &Board) -> Self {
Self {
mailbox: [None; 64],
}
}
pub fn new_from_board(board: &Board) -> Self {
let mut mailbox: [Option<PieceType>; 64] = [None; 64]; let mut mailbox: [Option<PieceType>; 64] = [None; 64];
for square in Square::A1..=Square::H8 { for square in Square::A1..=Square::H8 {
mailbox[square] = board mailbox[square] = board
@@ -34,7 +28,7 @@ impl Mailbox {
self.mailbox[square] = piece_type; 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] 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"; const FEN_STARTPOS: &str = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
#[test] #[test]
fn test_new_from_board() -> Result<(), String> { fn test_from_board() -> Result<(), String> {
let game = from_fen(FEN_MATE_IN_1)?; 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] #[rustfmt::skip]
let expected: [Option<PieceType>; 64] = [ let expected: [Option<PieceType>; 64] = [
None, None, None, None, None, Some(PieceType::King), None, None, None, None, None, None, None, Some(PieceType::King), None, None,
@@ -67,7 +61,7 @@ mod tests {
assert_eq!(expected, mailbox.mailbox); assert_eq!(expected, mailbox.mailbox);
let game = from_fen(FEN_STARTPOS)?; let game = from_fen(FEN_STARTPOS)?;
let mailbox = Mailbox::new_from_board(&game.board); let mailbox = Mailbox::from_board(&game.board);
#[rustfmt::skip] #[rustfmt::skip]
let expected: [Option<PieceType>; 64] = [ let expected: [Option<PieceType>; 64] = [
Some(PieceType::Rook), Some(PieceType::Knight), Some(PieceType::Bishop), Some(PieceType::Queen), Some(PieceType::Rook), Some(PieceType::Knight), Some(PieceType::Bishop), Some(PieceType::Queen),
@@ -90,11 +84,11 @@ mod tests {
} }
#[test] #[test]
fn test_find_piece_at() -> Result<(), String> { fn test_piece_at() -> Result<(), String> {
let game = from_fen(FEN_MATE_IN_1)?; 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(()) Ok(())
} }

View File

@@ -74,15 +74,15 @@ impl Square {
pub const H8: usize = 63; 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 rank * 8 + file
} }
pub const fn square_to_file(square: usize) -> usize { pub const fn to_file(square: usize) -> usize {
square % 8 square % 8
} }
pub fn square_to_algebraic(square: usize) -> String { pub fn to_algebraic(square: usize) -> String {
let file = (square % 8) as u8; let file = (square % 8) as u8;
let rank = (square / 8) as u8; let rank = (square / 8) as u8;

View File

@@ -3,7 +3,7 @@ use crate::movegen::r#move::Promote;
use super::{ use super::{
bitboard::lsb, bitboard::lsb,
board::{self, Board, Color, PieceType}, board::{self, Board, Color, PieceType},
square::{self, square_to_file, Square}, square::{self, to_file, Square},
state::Castle, state::Castle,
}; };
use rand::{rngs::SmallRng, RngCore, SeedableRng}; use rand::{rngs::SmallRng, RngCore, SeedableRng};
@@ -86,7 +86,7 @@ impl ZobristKeys {
hash ^= self.castling_ability[board.state.castling_ability[1].idx()][1]; hash ^= self.castling_ability[board.state.castling_ability[1].idx()][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[to_file(ep)];
} }
ZobristHash::new(hash) ZobristHash::new(hash)
@@ -121,7 +121,7 @@ impl ZobristHash {
pub fn update_en_passant_keys(&mut self, old_en_passant: usize) { pub fn update_en_passant_keys(&mut self, old_en_passant: usize) {
let keys = zobrist_keys(); 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( 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()]; self.hash ^= keys.piece_square_color[dst][piece_at_src.idx()][color.idx()];
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::square_to_file(new_en_passant)]; self.hash ^= keys.en_passant[square::to_file(new_en_passant)];
} }
} }

View File

@@ -100,7 +100,7 @@ pub fn uci_position(position: &mut SplitWhitespace) -> Result<Game, String> {
"fen" => { "fen" => {
let fen_parts: Vec<&str> = position.take_while(|&part| part != "moves").collect(); let fen_parts: Vec<&str> = position.take_while(|&part| part != "moves").collect();
let fen = fen_parts.join(" "); let fen = fen_parts.join(" ");
Game::new_from_fen(&fen)? Game::from_fen(&fen)?
} }
_ => return Err("Expected startpos or fen".to_string()), _ => return Err("Expected startpos or fen".to_string()),
}; };

View File

@@ -4,7 +4,7 @@ use crate::board::{
EMPTY, NOT_FILE_A, NOT_FILE_AB, NOT_FILE_GH, NOT_FILE_H, EMPTY, NOT_FILE_A, NOT_FILE_AB, NOT_FILE_GH, NOT_FILE_H,
}, },
board::Color, board::Color,
square::coords_to_square, square::from_coords,
}; };
use u64 as Bitboard; use u64 as Bitboard;
@@ -96,7 +96,7 @@ pub fn mask_bishop_attacks(bitboard: Bitboard) -> Bitboard {
while (1..=6).contains(&(rank + rank_step)) && (1..=6).contains(&(file + file_step)) { while (1..=6).contains(&(rank + rank_step)) && (1..=6).contains(&(file + file_step)) {
rank += rank_step; rank += rank_step;
file += file_step; file += file_step;
attacks |= square_to_bitboard(coords_to_square(rank as usize, file as usize)); attacks |= square_to_bitboard(from_coords(rank as usize, file as usize));
} }
}; };
@@ -117,9 +117,9 @@ pub fn mask_rook_attacks(bitboard: Bitboard) -> Bitboard {
coord = (coord as isize + step) as usize; coord = (coord as isize + step) as usize;
let attack_square = if is_vertical { let attack_square = if is_vertical {
square_to_bitboard(coords_to_square(coord, file_dst)) square_to_bitboard(from_coords(coord, file_dst))
} else { } else {
square_to_bitboard(coords_to_square(rank_dst, coord)) square_to_bitboard(from_coords(rank_dst, coord))
}; };
attacks |= attack_square; attacks |= attack_square;
@@ -146,7 +146,7 @@ pub fn bishop_attacks_on_the_fly(bitboard: Bitboard, blocker: Bitboard) -> Bitbo
rank += rank_step; rank += rank_step;
file += file_step; file += file_step;
let attack_square = square_to_bitboard(coords_to_square(rank as usize, file as usize)); let attack_square = square_to_bitboard(from_coords(rank as usize, file as usize));
attacks |= attack_square; attacks |= attack_square;
if have_common_bit(attack_square, blocker) { if have_common_bit(attack_square, blocker) {
@@ -172,9 +172,9 @@ pub fn rook_attacks_on_the_fly(bitboard: Bitboard, blocker: Bitboard) -> Bitboar
coord = (coord as isize + step) as usize; coord = (coord as isize + step) as usize;
let attack_square = if is_vertical { let attack_square = if is_vertical {
square_to_bitboard(coords_to_square(coord, file_dst)) square_to_bitboard(from_coords(coord, file_dst))
} else { } else {
square_to_bitboard(coords_to_square(rank_dst, coord)) square_to_bitboard(from_coords(rank_dst, coord))
}; };
attacks |= attack_square; attacks |= attack_square;

View File

@@ -5,7 +5,7 @@ use crate::board::{
board::PieceType, board::PieceType,
game::Game, game::Game,
mailbox::Mailbox, mailbox::Mailbox,
square::{coords_to_square, square_to_algebraic}, square::{from_coords, to_algebraic},
}; };
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
@@ -47,12 +47,7 @@ pub struct Move {
impl fmt::Debug for Move { impl fmt::Debug for Move {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!( write!(f, "{}{}", to_algebraic(self.src), to_algebraic(self.dst))?;
f,
"{}{}",
square_to_algebraic(self.src),
square_to_algebraic(self.dst)
)?;
if let MoveType::Promotion(piece) | MoveType::PromotionCapture(piece) = &self.move_type { if let MoveType::Promotion(piece) | MoveType::PromotionCapture(piece) = &self.move_type {
let promote_char = match piece { let promote_char = match piece {
@@ -80,7 +75,7 @@ impl Move {
} }
} }
pub const fn new_with_type(src: usize, dst: usize, move_type: MoveType) -> Self { pub const fn with_type(src: usize, dst: usize, move_type: MoveType) -> Self {
Self { Self {
src, src,
dst, dst,
@@ -109,8 +104,8 @@ impl Move {
let dst_file = (mv_chars.next().unwrap() as usize) - ('a' as usize); let dst_file = (mv_chars.next().unwrap() as usize) - ('a' as usize);
let dst_rank = (mv_chars.next().unwrap() as usize) - ('1' as usize); let dst_rank = (mv_chars.next().unwrap() as usize) - ('1' as usize);
let src = coords_to_square(src_rank, src_file); let src = from_coords(src_rank, src_file);
let dst = coords_to_square(dst_rank, dst_file); let dst = from_coords(dst_rank, dst_file);
let promote_into = if mv.len() == 5 { let promote_into = if mv.len() == 5 {
Some(Self::parse_promotion(mv_chars)?) Some(Self::parse_promotion(mv_chars)?)
@@ -127,32 +122,32 @@ impl Move {
dst: usize, dst: usize,
promote_into: Option<Promote>, promote_into: Option<Promote>,
) -> Self { ) -> Self {
let moving = mailbox.find_piece_at(src); let moving = mailbox.piece_at(src);
let captured = mailbox.find_piece_at(dst); let captured = mailbox.piece_at(dst);
match (captured, promote_into) { match (captured, promote_into) {
(Some(_), None) => return Self::new_with_type(src, dst, MoveType::Capture), (Some(_), None) => return Self::with_type(src, dst, MoveType::Capture),
(None, Some(promote_into)) => { (None, Some(promote_into)) => {
return Self::new_with_type(src, dst, MoveType::Promotion(promote_into)) return Self::with_type(src, dst, MoveType::Promotion(promote_into))
} }
(Some(_), Some(promote_into)) => { (Some(_), Some(promote_into)) => {
return Self::new_with_type(src, dst, MoveType::PromotionCapture(promote_into)) return Self::with_type(src, dst, MoveType::PromotionCapture(promote_into))
} }
_ => (), _ => (),
} }
if moving == Some(PieceType::Pawn) { if moving == Some(PieceType::Pawn) {
if src.abs_diff(dst) == 16 { if src.abs_diff(dst) == 16 {
return Self::new_with_type(src, dst, MoveType::DoublePush); return Self::with_type(src, dst, MoveType::DoublePush);
} }
if captured.is_none() && src.abs_diff(dst) != 8 { if captured.is_none() && src.abs_diff(dst) != 8 {
return Self::new_with_type(src, dst, MoveType::EnPassant); return Self::with_type(src, dst, MoveType::EnPassant);
} }
} }
if moving == Some(PieceType::King) && src.abs_diff(dst) == 2 { if moving == Some(PieceType::King) && src.abs_diff(dst) == 2 {
return Self::new_with_type(src, dst, MoveType::Castle); return Self::with_type(src, dst, MoveType::Castle);
} }
Self::new(src, dst) Self::new(src, dst)
@@ -188,7 +183,7 @@ mod tests {
let game = from_fen(FEN)?; let game = from_fen(FEN)?;
let mv_str = "d3b5"; let mv_str = "d3b5";
let actual = Move::parse_from_str(&game, &mv_str)?; let actual = Move::parse_from_str(&game, &mv_str)?;
let expected = Move::new_with_type(Square::D3, Square::B5, MoveType::Capture); let expected = Move::with_type(Square::D3, Square::B5, MoveType::Capture);
assert_eq!(expected, actual); assert_eq!(expected, actual);
@@ -200,7 +195,7 @@ mod tests {
let game = from_fen(FEN)?; let game = from_fen(FEN)?;
let mv_str = "h5g6"; let mv_str = "h5g6";
let actual = Move::parse_from_str(&game, &mv_str)?; let actual = Move::parse_from_str(&game, &mv_str)?;
let expected = Move::new_with_type(Square::H5, Square::G6, MoveType::EnPassant); let expected = Move::with_type(Square::H5, Square::G6, MoveType::EnPassant);
assert_eq!(expected, actual); assert_eq!(expected, actual);
@@ -212,7 +207,7 @@ mod tests {
let game = from_fen(FEN)?; let game = from_fen(FEN)?;
let mv_str = "b2b4"; let mv_str = "b2b4";
let actual = Move::parse_from_str(&game, &mv_str)?; let actual = Move::parse_from_str(&game, &mv_str)?;
let expected = Move::new_with_type(Square::B2, Square::B4, MoveType::DoublePush); let expected = Move::with_type(Square::B2, Square::B4, MoveType::DoublePush);
assert_eq!(expected, actual); assert_eq!(expected, actual);
@@ -224,8 +219,7 @@ mod tests {
let game = from_fen(FEN)?; let game = from_fen(FEN)?;
let mv_str = "c7c8q"; let mv_str = "c7c8q";
let actual = Move::parse_from_str(&game, &mv_str)?; let actual = Move::parse_from_str(&game, &mv_str)?;
let expected = let expected = Move::with_type(Square::C7, Square::C8, MoveType::Promotion(Promote::Queen));
Move::new_with_type(Square::C7, Square::C8, MoveType::Promotion(Promote::Queen));
assert_eq!(expected, actual); assert_eq!(expected, actual);
@@ -237,7 +231,7 @@ mod tests {
let game = from_fen(FEN)?; let game = from_fen(FEN)?;
let mv_str = "c7b8q"; let mv_str = "c7b8q";
let actual = Move::parse_from_str(&game, &mv_str)?; let actual = Move::parse_from_str(&game, &mv_str)?;
let expected = Move::new_with_type( let expected = Move::with_type(
Square::C7, Square::C7,
Square::B8, Square::B8,
MoveType::PromotionCapture(Promote::Queen), MoveType::PromotionCapture(Promote::Queen),

View File

@@ -48,10 +48,10 @@ fn add_promotion_moves(moves: &mut Vec<Move>, src: usize, dst: usize, capture: b
}; };
moves.extend([ moves.extend([
Move::new_with_type(src, dst, promotion_type(Promote::Knight)), Move::with_type(src, dst, promotion_type(Promote::Knight)),
Move::new_with_type(src, dst, promotion_type(Promote::Bishop)), Move::with_type(src, dst, promotion_type(Promote::Bishop)),
Move::new_with_type(src, dst, promotion_type(Promote::Rook)), Move::with_type(src, dst, promotion_type(Promote::Rook)),
Move::new_with_type(src, dst, promotion_type(Promote::Queen)), Move::with_type(src, dst, promotion_type(Promote::Queen)),
]); ]);
} }
@@ -77,7 +77,7 @@ fn white_pawn_quiet_moves(pawns: Bitboard, occupancies: Bitboard) -> Vec<Move> {
while double_push != 0 { while double_push != 0 {
let dst = lsb(double_push); let dst = lsb(double_push);
let src = dst - 16; let src = dst - 16;
moves.push(Move::new_with_type(src, dst, MoveType::DoublePush)); moves.push(Move::with_type(src, dst, MoveType::DoublePush));
double_push &= double_push - 1; double_push &= double_push - 1;
} }
moves moves
@@ -105,7 +105,7 @@ fn black_pawn_quiet_moves(pawns: Bitboard, occupancies: Bitboard) -> Vec<Move> {
while double_push != 0 { while double_push != 0 {
let dst = lsb(double_push); let dst = lsb(double_push);
let src = dst + 16; let src = dst + 16;
moves.push(Move::new_with_type(src, dst, MoveType::DoublePush)); moves.push(Move::with_type(src, dst, MoveType::DoublePush));
double_push &= double_push - 1; double_push &= double_push - 1;
} }
moves moves
@@ -127,7 +127,7 @@ fn white_pawn_capture_moves(
if have_common_bit(square_to_bitboard(dst), RANK_8) { if have_common_bit(square_to_bitboard(dst), RANK_8) {
add_promotion_moves(&mut moves, src, dst, true); add_promotion_moves(&mut moves, src, dst, true);
} else { } else {
moves.push(Move::new_with_type(src, dst, MoveType::Capture)); moves.push(Move::with_type(src, dst, MoveType::Capture));
} }
w_pawns_capture_east &= w_pawns_capture_east - 1; w_pawns_capture_east &= w_pawns_capture_east - 1;
} }
@@ -138,7 +138,7 @@ fn white_pawn_capture_moves(
if have_common_bit(square_to_bitboard(dst), RANK_8) { if have_common_bit(square_to_bitboard(dst), RANK_8) {
add_promotion_moves(&mut moves, src, dst, true); add_promotion_moves(&mut moves, src, dst, true);
} else { } else {
moves.push(Move::new_with_type(src, dst, MoveType::Capture)); moves.push(Move::with_type(src, dst, MoveType::Capture));
} }
w_pawns_capture_west &= w_pawns_capture_west - 1; w_pawns_capture_west &= w_pawns_capture_west - 1;
} }
@@ -147,7 +147,7 @@ fn white_pawn_capture_moves(
let attacked_src = fetch_pawn_attacks(en_passant_sq, Color::Black); let attacked_src = fetch_pawn_attacks(en_passant_sq, Color::Black);
let mut result = attacked_src & pawns; let mut result = attacked_src & pawns;
while result != 0 { while result != 0 {
moves.push(Move::new_with_type( moves.push(Move::with_type(
lsb(result), lsb(result),
en_passant_sq, en_passant_sq,
MoveType::EnPassant, MoveType::EnPassant,
@@ -174,7 +174,7 @@ fn black_pawn_capture_moves(
if have_common_bit(square_to_bitboard(dst), RANK_1) { if have_common_bit(square_to_bitboard(dst), RANK_1) {
add_promotion_moves(&mut moves, src, dst, true); add_promotion_moves(&mut moves, src, dst, true);
} else { } else {
moves.push(Move::new_with_type(src, dst, MoveType::Capture)); moves.push(Move::with_type(src, dst, MoveType::Capture));
} }
b_pawns_capture_east &= b_pawns_capture_east - 1; b_pawns_capture_east &= b_pawns_capture_east - 1;
} }
@@ -185,7 +185,7 @@ fn black_pawn_capture_moves(
if have_common_bit(square_to_bitboard(dst), RANK_1) { if have_common_bit(square_to_bitboard(dst), RANK_1) {
add_promotion_moves(&mut moves, src, dst, true); add_promotion_moves(&mut moves, src, dst, true);
} else { } else {
moves.push(Move::new_with_type(src, dst, MoveType::Capture)); moves.push(Move::with_type(src, dst, MoveType::Capture));
} }
b_pawns_capture_west &= b_pawns_capture_west - 1; b_pawns_capture_west &= b_pawns_capture_west - 1;
} }
@@ -194,7 +194,7 @@ fn black_pawn_capture_moves(
let attacked_src = fetch_pawn_attacks(en_passant_square, Color::White); let attacked_src = fetch_pawn_attacks(en_passant_square, Color::White);
let mut result = attacked_src & pawns; let mut result = attacked_src & pawns;
while result != 0 { while result != 0 {
moves.push(Move::new_with_type( moves.push(Move::with_type(
lsb(result), lsb(result),
en_passant_square, en_passant_square,
MoveType::EnPassant, MoveType::EnPassant,
@@ -221,7 +221,7 @@ pub fn knight_pseudo_moves(
while attacks != 0 { while attacks != 0 {
let attack_sq = lsb(attacks); let attack_sq = lsb(attacks);
if have_common_bit(square_to_bitboard(attack_sq), opponent_occupancies) { if have_common_bit(square_to_bitboard(attack_sq), opponent_occupancies) {
moves.push(Move::new_with_type(src, attack_sq, MoveType::Capture)); moves.push(Move::with_type(src, attack_sq, MoveType::Capture));
} else { } else {
moves.push(Move::new(src, attack_sq)); moves.push(Move::new(src, attack_sq));
} }
@@ -248,7 +248,7 @@ pub fn bishop_pseudo_moves(
while attacks != 0 { while attacks != 0 {
let attack_sq = lsb(attacks); let attack_sq = lsb(attacks);
if have_common_bit(square_to_bitboard(attack_sq), opponent_occupancies) { if have_common_bit(square_to_bitboard(attack_sq), opponent_occupancies) {
moves.push(Move::new_with_type(src, attack_sq, MoveType::Capture)); moves.push(Move::with_type(src, attack_sq, MoveType::Capture));
} else { } else {
moves.push(Move::new(src, attack_sq)); moves.push(Move::new(src, attack_sq));
} }
@@ -275,7 +275,7 @@ pub fn rook_pseudo_moves(
while attacks != 0 { while attacks != 0 {
let attack_sq = lsb(attacks); let attack_sq = lsb(attacks);
if have_common_bit(square_to_bitboard(attack_sq), opponent_occupancies) { if have_common_bit(square_to_bitboard(attack_sq), opponent_occupancies) {
moves.push(Move::new_with_type(src, attack_sq, MoveType::Capture)); moves.push(Move::with_type(src, attack_sq, MoveType::Capture));
} else { } else {
moves.push(Move::new(src, attack_sq)); moves.push(Move::new(src, attack_sq));
} }
@@ -302,7 +302,7 @@ pub fn queen_pseudo_moves(
while attacks != 0 { while attacks != 0 {
let attack_sq = lsb(attacks); let attack_sq = lsb(attacks);
if have_common_bit(square_to_bitboard(attack_sq), opponent_occupancies) { if have_common_bit(square_to_bitboard(attack_sq), opponent_occupancies) {
moves.push(Move::new_with_type(src, attack_sq, MoveType::Capture)); moves.push(Move::with_type(src, attack_sq, MoveType::Capture));
} else { } else {
moves.push(Move::new(src, attack_sq)); moves.push(Move::new(src, attack_sq));
} }
@@ -329,7 +329,7 @@ pub fn king_pseudo_moves(
while attacks != 0 { while attacks != 0 {
let attack_sq = lsb(attacks); let attack_sq = lsb(attacks);
if have_common_bit(square_to_bitboard(attack_sq), opponent_occupancies) { if have_common_bit(square_to_bitboard(attack_sq), opponent_occupancies) {
moves.push(Move::new_with_type(src, attack_sq, MoveType::Capture)); moves.push(Move::with_type(src, attack_sq, MoveType::Capture));
} else { } else {
moves.push(Move::new(src, attack_sq)); moves.push(Move::new(src, attack_sq));
} }
@@ -349,7 +349,7 @@ fn king_castling_moves(board: &Board, color: Color, all_occupancies: Bitboard) -
let mut add_move_if_empty_path = |path_mask, king_dst| { let mut add_move_if_empty_path = |path_mask, king_dst| {
if !have_common_bit(all_occupancies, path_mask) { if !have_common_bit(all_occupancies, path_mask) {
moves.push(Move::new_with_type(king_src, king_dst, MoveType::Castle)); moves.push(Move::with_type(king_src, king_dst, MoveType::Castle));
} }
}; };
@@ -408,19 +408,19 @@ mod tests {
let new_game = from_fen(FEN_PAWN_MOVES)?; let new_game = from_fen(FEN_PAWN_MOVES)?;
let expected = vec![ let expected = vec![
Move::new(9, 17), Move::new(9, 17),
Move::new_with_type(9, 25, MoveType::DoublePush), Move::with_type(9, 25, MoveType::DoublePush),
Move::new(10, 18), Move::new(10, 18),
Move::new_with_type(10, 26, MoveType::DoublePush), Move::with_type(10, 26, MoveType::DoublePush),
Move::new(14, 22), Move::new(14, 22),
Move::new_with_type(14, 30, MoveType::DoublePush), Move::with_type(14, 30, MoveType::DoublePush),
Move::new(15, 23), Move::new(15, 23),
Move::new_with_type(15, 31, MoveType::DoublePush), Move::with_type(15, 31, MoveType::DoublePush),
Move::new(29, 37), Move::new(29, 37),
Move::new(32, 40), Move::new(32, 40),
Move::new_with_type(32, 41, MoveType::Capture), Move::with_type(32, 41, MoveType::Capture),
Move::new_with_type(36, 43, MoveType::Capture), Move::with_type(36, 43, MoveType::Capture),
Move::new(36, 44), Move::new(36, 44),
Move::new_with_type(36, 45, MoveType::Capture), Move::with_type(36, 45, MoveType::Capture),
]; ];
let mut actual = new_game.board.pseudo_moves(Color::White, PieceType::Pawn); let mut actual = new_game.board.pseudo_moves(Color::White, PieceType::Pawn);
@@ -428,12 +428,12 @@ mod tests {
assert_eq!(expected, actual); assert_eq!(expected, actual);
let expected = vec![ let expected = vec![
Move::new_with_type(41, 32, MoveType::Capture), Move::with_type(41, 32, MoveType::Capture),
Move::new(41, 33), Move::new(41, 33),
Move::new(43, 35), Move::new(43, 35),
Move::new_with_type(43, 36, MoveType::Capture), Move::with_type(43, 36, MoveType::Capture),
Move::new(48, 40), Move::new(48, 40),
Move::new_with_type(55, 39, MoveType::DoublePush), Move::with_type(55, 39, MoveType::DoublePush),
Move::new(55, 47), Move::new(55, 47),
]; ];
let mut actual = new_game.board.pseudo_moves(Color::Black, PieceType::Pawn); let mut actual = new_game.board.pseudo_moves(Color::Black, PieceType::Pawn);
@@ -457,7 +457,7 @@ mod tests {
Move::new(21, 11), Move::new(21, 11),
Move::new(21, 31), Move::new(21, 31),
Move::new(21, 36), Move::new(21, 36),
Move::new_with_type(21, 38, MoveType::Capture), Move::with_type(21, 38, MoveType::Capture),
]; ];
let mut actual = new_game.board.pseudo_moves(Color::White, PieceType::Knight); let mut actual = new_game.board.pseudo_moves(Color::White, PieceType::Knight);
actual.sort(); actual.sort();
@@ -483,7 +483,7 @@ mod tests {
Move::new(26, 35), Move::new(26, 35),
Move::new(26, 40), Move::new(26, 40),
Move::new(26, 44), Move::new(26, 44),
Move::new_with_type(26, 53, MoveType::Capture), Move::with_type(26, 53, MoveType::Capture),
]; ];
let mut actual = new_game.board.pseudo_moves(Color::White, PieceType::Bishop); let mut actual = new_game.board.pseudo_moves(Color::White, PieceType::Bishop);
actual.sort(); actual.sort();
@@ -504,14 +504,14 @@ mod tests {
Move::new(11, 35), Move::new(11, 35),
Move::new(11, 43), Move::new(11, 43),
Move::new(11, 51), Move::new(11, 51),
Move::new_with_type(11, 59, MoveType::Capture), Move::with_type(11, 59, MoveType::Capture),
Move::new(12, 13), Move::new(12, 13),
Move::new(12, 14), Move::new(12, 14),
Move::new(12, 15), Move::new(12, 15),
Move::new(12, 20), Move::new(12, 20),
Move::new(12, 28), Move::new(12, 28),
Move::new(12, 36), Move::new(12, 36),
Move::new_with_type(12, 44, MoveType::Capture), Move::with_type(12, 44, MoveType::Capture),
]; ];
let mut actual = new_game.board.pseudo_moves(Color::White, PieceType::Rook); let mut actual = new_game.board.pseudo_moves(Color::White, PieceType::Rook);
actual.sort(); actual.sort();
@@ -534,7 +534,7 @@ mod tests {
Move::new(17, 25), Move::new(17, 25),
Move::new(17, 33), Move::new(17, 33),
Move::new(17, 41), Move::new(17, 41),
Move::new_with_type(17, 49, MoveType::Capture), Move::with_type(17, 49, MoveType::Capture),
]; ];
let mut actual = new_game.board.pseudo_moves(Color::White, PieceType::Queen); let mut actual = new_game.board.pseudo_moves(Color::White, PieceType::Queen);
actual.sort(); actual.sort();
@@ -567,7 +567,7 @@ mod tests {
assert_eq!(expected, actual); assert_eq!(expected, actual);
let new_game_2 = from_fen(FEN_KING_MOVES[1])?; let new_game_2 = from_fen(FEN_KING_MOVES[1])?;
let expected = vec![Move::new_with_type(4, 2, MoveType::Castle), Move::new(4, 3)]; let expected = vec![Move::with_type(4, 2, MoveType::Castle), Move::new(4, 3)];
let mut actual = new_game_2.board.pseudo_moves(Color::White, PieceType::King); let mut actual = new_game_2.board.pseudo_moves(Color::White, PieceType::King);
actual.sort(); actual.sort();
assert_eq!(expected, actual); assert_eq!(expected, actual);
@@ -578,7 +578,7 @@ mod tests {
Move::new(60, 53), Move::new(60, 53),
Move::new(60, 59), Move::new(60, 59),
Move::new(60, 61), Move::new(60, 61),
Move::new_with_type(60, 62, MoveType::Castle), Move::with_type(60, 62, MoveType::Castle),
]; ];
let mut actual = new_game_3.board.pseudo_moves(Color::Black, PieceType::King); let mut actual = new_game_3.board.pseudo_moves(Color::Black, PieceType::King);
actual.sort(); actual.sort();

View File

@@ -13,7 +13,7 @@ const MVV_LVA: [[usize; 6]; 6] = [
]; ];
pub const fn score_by_mvv_lva(mailbox: &Mailbox, mv: Move) -> usize { pub const fn score_by_mvv_lva(mailbox: &Mailbox, mv: Move) -> usize {
match (mailbox.find_piece_at(mv.src), mailbox.find_piece_at(mv.dst)) { match (mailbox.piece_at(mv.src), mailbox.piece_at(mv.dst)) {
(Some(aggressor), Some(victim)) => MVV_LVA[aggressor.idx()][victim.idx()], (Some(aggressor), Some(victim)) => MVV_LVA[aggressor.idx()][victim.idx()],
_ => 0, _ => 0,
} }
@@ -32,7 +32,7 @@ mod tests {
#[test] #[test]
fn test_score_by_mvv_lva() -> Result<(), String> { fn test_score_by_mvv_lva() -> Result<(), String> {
let game = from_fen(FEN)?; let 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);
let actual = score_by_mvv_lva(&game.mailbox, f3f5); let actual = score_by_mvv_lva(&game.mailbox, f3f5);
let expected = MVV_LVA[PieceType::Queen.idx()][PieceType::Pawn.idx()]; let expected = MVV_LVA[PieceType::Queen.idx()][PieceType::Pawn.idx()];