2020-05-31 02:41:02 +02:00
|
|
|
//! Functions to determine legal moves.
|
|
|
|
|
|
|
|
use crate::board::*;
|
2020-06-14 20:32:40 +02:00
|
|
|
use crate::castling::*;
|
2020-06-19 19:29:10 +02:00
|
|
|
use crate::fen;
|
|
|
|
use crate::movement::Move;
|
|
|
|
|
2020-06-05 18:46:08 +02:00
|
|
|
/// Characteristics of the state of a game.
|
|
|
|
///
|
|
|
|
/// It does not include various parameters such as clocks that are
|
|
|
|
/// more aimed for engine analysis than typical rules checking.
|
2020-06-06 00:47:07 +02:00
|
|
|
///
|
|
|
|
/// - `color`: current player's turn
|
|
|
|
/// - `castling`: which castling options are available; updated throughout the game.
|
2020-06-14 01:33:11 +02:00
|
|
|
/// - `en_passant`: position of a pawn that can be taken using en passant attack.
|
|
|
|
/// - `halfmove`: eh not sure
|
|
|
|
/// - `fullmove`: same
|
|
|
|
#[derive(Debug, PartialEq, Clone, Hash)]
|
2020-06-05 18:46:08 +02:00
|
|
|
pub struct GameState {
|
2020-06-19 02:44:33 +02:00
|
|
|
pub color: Color,
|
2020-06-05 18:46:08 +02:00
|
|
|
pub castling: u8,
|
2020-06-19 02:44:33 +02:00
|
|
|
pub en_passant: Option<Square>,
|
2020-06-05 18:46:08 +02:00
|
|
|
pub halfmove: i32,
|
|
|
|
pub fullmove: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GameState {
|
|
|
|
pub const fn new() -> GameState {
|
|
|
|
GameState {
|
2020-06-19 02:44:33 +02:00
|
|
|
color: WHITE,
|
2020-06-22 21:33:23 +02:00
|
|
|
castling: CASTLE_MASK,
|
2020-06-05 18:46:08 +02:00
|
|
|
en_passant: None,
|
|
|
|
halfmove: 0,
|
|
|
|
fullmove: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 19:33:18 +02:00
|
|
|
impl std::fmt::Display for GameState {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
2020-06-20 21:05:54 +02:00
|
|
|
"[color: {}, castling: {:04b}, en_passant: {}, halfmove: {}, fullmove: {}]",
|
2020-06-11 20:40:08 +02:00
|
|
|
color_to_string(self.color), self.castling,
|
2020-06-19 19:29:10 +02:00
|
|
|
fen::en_passant_to_string(self.en_passant),
|
2020-06-11 19:33:18 +02:00
|
|
|
self.halfmove, self.fullmove
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 00:47:07 +02:00
|
|
|
/// Get a list of moves for all pieces of the playing color.
|
|
|
|
///
|
2020-06-21 00:33:05 +02:00
|
|
|
/// If `pseudo_legal` is true, do not check for illegal moves. This is
|
|
|
|
/// used to avoid endless recursion when checking if a P move is
|
|
|
|
/// illegal, as it needs to check all possible following enemy moves,
|
|
|
|
/// e.g. to see if P's king can be taken. Consider a call with true
|
|
|
|
/// `pseudo_legal` as a collection of attacked squares instead of legal
|
|
|
|
/// move collection.
|
2020-06-14 20:32:40 +02:00
|
|
|
pub fn get_player_moves(
|
|
|
|
board: &Board,
|
|
|
|
game_state: &GameState,
|
|
|
|
) -> Vec<Move> {
|
2020-06-22 21:33:23 +02:00
|
|
|
let attacked_bb = board.get_rays(opposite(game_state.color));
|
2020-06-21 00:33:05 +02:00
|
|
|
let mut moves = Vec::with_capacity(32);
|
2020-05-31 02:41:02 +02:00
|
|
|
for r in 0..8 {
|
|
|
|
for f in 0..8 {
|
2020-06-19 19:29:10 +02:00
|
|
|
let square = sq(f, r);
|
|
|
|
if board.is_empty(square) {
|
2020-05-31 19:00:56 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-06-20 03:42:20 +02:00
|
|
|
if board.get_color_on(square) == game_state.color {
|
2020-06-19 19:29:10 +02:00
|
|
|
moves.append(
|
2020-06-22 21:33:23 +02:00
|
|
|
&mut get_piece_moves(board, game_state, square, game_state.color, attacked_bb)
|
2020-06-19 19:29:10 +02:00
|
|
|
);
|
2020-05-31 02:41:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
moves
|
|
|
|
}
|
|
|
|
|
2020-06-19 19:29:10 +02:00
|
|
|
/// Get a list of moves for the piece of `color` on `square`.
|
|
|
|
///
|
|
|
|
/// Use `board` and `game_state` to get the moves. `color` is the color
|
|
|
|
/// of the piece on `square`; it could technically be found from the
|
|
|
|
/// board but that would require an additional lookup and this function
|
|
|
|
/// is always called in a context where the piece color is known.
|
2020-06-21 00:33:05 +02:00
|
|
|
fn get_piece_moves(
|
2020-06-14 20:32:40 +02:00
|
|
|
board: &Board,
|
|
|
|
game_state: &GameState,
|
2020-06-19 19:29:10 +02:00
|
|
|
square: Square,
|
|
|
|
color: Color,
|
2020-06-22 21:33:23 +02:00
|
|
|
attacked_bb: Bitboard,
|
2020-06-06 00:47:07 +02:00
|
|
|
) -> Vec<Move> {
|
2020-06-22 21:33:23 +02:00
|
|
|
let piece = board.get_piece_on(square);
|
|
|
|
let mut moves = Vec::with_capacity(32);
|
2020-06-21 01:26:04 +02:00
|
|
|
get_moves_from_bb(
|
|
|
|
board,
|
|
|
|
game_state,
|
2020-06-22 21:33:23 +02:00
|
|
|
match piece {
|
|
|
|
PAWN => {
|
|
|
|
board.get_pawn_progresses(square, color)
|
|
|
|
| board.get_pawn_captures(square, color)
|
|
|
|
}
|
|
|
|
KING => board.get_king_rays(square, color),
|
|
|
|
BISHOP => board.get_bishop_rays(square, color),
|
|
|
|
KNIGHT => board.get_knight_rays(square, color),
|
|
|
|
ROOK => board.get_rook_rays(square, color),
|
|
|
|
QUEEN => board.get_queen_rays(square, color),
|
|
|
|
_ => { panic!("Invalid piece.") }
|
|
|
|
},
|
2020-06-21 01:26:04 +02:00
|
|
|
square,
|
|
|
|
color,
|
2020-06-22 21:33:23 +02:00
|
|
|
piece,
|
|
|
|
attacked_bb,
|
|
|
|
&mut moves
|
2020-06-21 01:26:04 +02:00
|
|
|
);
|
2020-06-22 21:33:23 +02:00
|
|
|
if piece == KING && sq_rank(square) == CASTLE_RANK_BY_COLOR[color] {
|
|
|
|
get_king_castles(board, game_state, square, color, attacked_bb, &mut moves);
|
2020-05-31 02:41:02 +02:00
|
|
|
}
|
|
|
|
moves
|
|
|
|
}
|
|
|
|
|
2020-06-21 01:26:04 +02:00
|
|
|
/// Get moves from this ray bitboard.
|
2020-06-21 15:57:58 +02:00
|
|
|
///
|
|
|
|
/// Inspect all moves from the bitboard and produce a Move for each
|
2020-06-22 21:33:23 +02:00
|
|
|
/// legal move. Does not take castle into account. Pawns that reach
|
|
|
|
/// the last rank are promoted as queens.
|
2020-06-21 01:26:04 +02:00
|
|
|
fn get_moves_from_bb(
|
2020-06-20 19:56:00 +02:00
|
|
|
board: &Board,
|
|
|
|
game_state: &GameState,
|
2020-06-21 01:26:04 +02:00
|
|
|
bitboard: Bitboard,
|
2020-06-20 19:56:00 +02:00
|
|
|
square: Square,
|
|
|
|
color: Color,
|
2020-06-21 01:26:04 +02:00
|
|
|
piece: Piece,
|
2020-06-22 21:33:23 +02:00
|
|
|
attacked_bb: Bitboard,
|
|
|
|
moves: &mut Vec<Move>
|
|
|
|
) {
|
2020-06-21 01:26:04 +02:00
|
|
|
for ray_square in 0..NUM_SQUARES {
|
|
|
|
if ray_square == square || bitboard & bit_pos(ray_square) == 0 {
|
|
|
|
continue
|
2020-06-20 19:56:00 +02:00
|
|
|
}
|
2020-06-22 21:33:23 +02:00
|
|
|
if let Some(mut m) = inspect_move(board, game_state, square, ray_square, attacked_bb) {
|
2020-06-21 01:26:04 +02:00
|
|
|
// Automatic queen promotion for pawns moving to the opposite rank.
|
|
|
|
if
|
|
|
|
piece == PAWN
|
2020-06-22 19:17:09 +02:00
|
|
|
&& (
|
|
|
|
(color == WHITE && sq_rank(ray_square) == RANK_8)
|
|
|
|
|| (color == BLACK && sq_rank(ray_square) == RANK_1)
|
|
|
|
)
|
2020-06-21 01:26:04 +02:00
|
|
|
{
|
|
|
|
m.promotion = Some(QUEEN);
|
2020-06-20 19:56:00 +02:00
|
|
|
}
|
2020-06-21 01:26:04 +02:00
|
|
|
moves.push(m);
|
2020-06-20 19:56:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 01:26:04 +02:00
|
|
|
/// Accept or ignore a move from `square` to `ray_square`.
|
|
|
|
///
|
2020-06-22 21:33:23 +02:00
|
|
|
/// This function checks that the move is legal. It assumes that
|
|
|
|
/// `ray_square` is either empty or an enemy piece, but not a friend
|
|
|
|
/// piece: they should have been filtered.
|
2020-06-06 00:47:07 +02:00
|
|
|
///
|
2020-06-21 01:26:04 +02:00
|
|
|
/// This function does not set promotions for pawns reaching last rank.
|
|
|
|
fn inspect_move(
|
|
|
|
board: &Board,
|
|
|
|
game_state: &GameState,
|
|
|
|
square: Square,
|
|
|
|
ray_square: Square,
|
2020-06-22 21:33:23 +02:00
|
|
|
attacked_bb: Bitboard
|
2020-06-21 01:26:04 +02:00
|
|
|
) -> Option<Move> {
|
|
|
|
let m = Move::new(square, ray_square);
|
2020-06-22 21:33:23 +02:00
|
|
|
if !is_illegal(board, game_state, &m, attacked_bb) {
|
2020-06-21 01:26:04 +02:00
|
|
|
Some(m)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2020-06-06 00:47:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if a move is illegal.
|
2020-06-22 21:33:23 +02:00
|
|
|
fn is_illegal(
|
|
|
|
board: &Board,
|
|
|
|
game_state: &GameState,
|
|
|
|
m: &Move,
|
|
|
|
attacked_bb: Bitboard
|
|
|
|
) -> bool {
|
|
|
|
// A move is illegal if the king ends up in check.
|
|
|
|
let king_square = if board.get_piece_on(m.source) == KING {
|
|
|
|
m.dest
|
|
|
|
} else {
|
|
|
|
if let Some(king) = board.find_king(game_state.color) { king } else { return false }
|
|
|
|
};
|
|
|
|
attacked_bb & bit_pos(king_square) != 0
|
2020-05-31 19:00:56 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:33:23 +02:00
|
|
|
/// Get possible castles.
|
2020-06-05 18:46:08 +02:00
|
|
|
///
|
2020-06-22 21:33:23 +02:00
|
|
|
/// Here are the rules that should ALL be respected:
|
|
|
|
/// 1. The king and the chosen rook are on the player's first rank.
|
|
|
|
/// 2. Neither the king nor the chosen rook has previously moved.
|
|
|
|
/// 3. There are no pieces between the king and the chosen rook.
|
|
|
|
/// 4. The king is not currently in check.
|
|
|
|
/// 5. The king does not pass through a square that is attacked by an enemy piece.
|
|
|
|
/// 6. The king does not end up in check.
|
2020-06-06 00:47:07 +02:00
|
|
|
///
|
2020-06-22 21:33:23 +02:00
|
|
|
/// Rule 1 is NOT checked by this method to avoid creating empty vecs.
|
|
|
|
/// Check it in the caller.
|
|
|
|
fn get_king_castles(
|
|
|
|
board: &Board,
|
|
|
|
game_state: &GameState,
|
|
|
|
square: Square,
|
|
|
|
color: Color,
|
|
|
|
attacked_bb: Bitboard,
|
|
|
|
moves: &mut Vec<Move>
|
|
|
|
) {
|
|
|
|
let combined_bb = board.combined();
|
|
|
|
|
|
|
|
// First get the required castling rank and color mask for the player.
|
|
|
|
let castle_rank = CASTLE_RANK_BY_COLOR[color];
|
|
|
|
let castle_color_mask = CASTLE_MASK_BY_COLOR[color];
|
|
|
|
|
|
|
|
// Check for castling if the king is on its castling rank (R1)
|
|
|
|
if sq_rank(square) == castle_rank {
|
|
|
|
// Check for both castling sides.
|
|
|
|
for castle_side_id in 0..NUM_CASTLE_SIDES {
|
|
|
|
let castle_side_mask = CASTLE_SIDES[castle_side_id];
|
|
|
|
// Check for castling availability for this color and side (R2).
|
|
|
|
if (game_state.castling & castle_color_mask & castle_side_mask) != 0 {
|
|
|
|
// Check that squares in the king's path are not attacked (R4, R5, R6).
|
|
|
|
let castle_legality_path = CASTLE_LEGALITY_PATHS[color][castle_side_id];
|
|
|
|
if attacked_bb & castle_legality_path != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that squares in both the king and rook's path are empty.
|
|
|
|
let castle_move_path = CASTLE_MOVE_PATHS[color][castle_side_id];
|
|
|
|
if combined_bb & castle_move_path != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
moves.push(Move::get_castle_move(castle_side_mask & castle_color_mask));
|
|
|
|
}
|
2020-05-31 19:00:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 02:41:02 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-06-06 00:47:07 +02:00
|
|
|
|
2020-06-22 21:33:23 +02:00
|
|
|
/// Like `get_piece_moves` but generate attacked bitboard.
|
|
|
|
fn get_legal_piece_moves(
|
|
|
|
board: &Board,
|
|
|
|
game_state: &GameState,
|
|
|
|
square: Square,
|
|
|
|
color: Color
|
|
|
|
) -> Vec<Move> {
|
|
|
|
let attacked_bb = board.get_rays(opposite(game_state.color));
|
|
|
|
get_piece_moves(board, game_state, square, color, attacked_bb)
|
|
|
|
}
|
|
|
|
|
2020-05-31 02:41:02 +02:00
|
|
|
#[test]
|
2020-05-31 19:00:56 +02:00
|
|
|
fn test_get_player_moves() {
|
2020-06-19 19:29:10 +02:00
|
|
|
let b = Board::new();
|
2020-06-06 00:47:07 +02:00
|
|
|
let gs = GameState::new();
|
2020-06-05 18:46:08 +02:00
|
|
|
|
2020-05-31 02:41:02 +02:00
|
|
|
// At first move, white has 16 pawn moves and 4 knight moves.
|
2020-06-22 21:33:23 +02:00
|
|
|
let moves = get_player_moves(&b, &gs);
|
2020-05-31 02:41:02 +02:00
|
|
|
assert_eq!(moves.len(), 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-05-31 19:00:56 +02:00
|
|
|
fn test_get_pawn_moves() {
|
2020-06-19 19:29:10 +02:00
|
|
|
let mut b = Board::new_empty();
|
2020-06-06 00:47:07 +02:00
|
|
|
let gs = GameState::new();
|
2020-05-31 02:41:02 +02:00
|
|
|
|
|
|
|
// Check that a pawn (here white queen's pawn) can move forward if the road is free.
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(D3, WHITE, PAWN);
|
2020-06-22 21:33:23 +02:00
|
|
|
let moves = get_legal_piece_moves(&b, &gs, D3, WHITE);
|
2020-06-19 19:29:10 +02:00
|
|
|
assert!(moves.len() == 1 && moves.contains(&Move::new(D3, D4)));
|
2020-05-31 02:41:02 +02:00
|
|
|
|
|
|
|
// Check that a pawn (here white king's pawn) can move 2 square forward on first move.
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(E2, WHITE, PAWN);
|
2020-06-22 21:33:23 +02:00
|
|
|
let moves = get_legal_piece_moves(&b, &gs, E2, WHITE);
|
2020-05-31 02:41:02 +02:00
|
|
|
assert_eq!(moves.len(), 2);
|
2020-06-19 19:29:10 +02:00
|
|
|
assert!(moves.contains(&Move::new(E2, E3)));
|
|
|
|
assert!(moves.contains(&Move::new(E2, E4)));
|
2020-05-31 02:41:02 +02:00
|
|
|
|
|
|
|
// Check that a pawn cannot move forward if a piece is blocking its path.
|
2020-06-04 18:58:00 +02:00
|
|
|
// 1. black pawn 2 square forward; only 1 square forward available from start pos.
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(E4, BLACK, PAWN);
|
2020-06-22 21:33:23 +02:00
|
|
|
let moves = get_legal_piece_moves(&b, &gs, E2, WHITE);
|
2020-06-19 19:29:10 +02:00
|
|
|
assert!(moves.len() == 1 && moves.contains(&Move::new(E2, E3)));
|
2020-06-04 18:58:00 +02:00
|
|
|
// 2. black pawn 1 square forward; no square available.
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(E3, BLACK, PAWN);
|
2020-06-22 21:33:23 +02:00
|
|
|
let moves = get_legal_piece_moves(&b, &gs, E2, WHITE);
|
2020-05-31 02:41:02 +02:00
|
|
|
assert_eq!(moves.len(), 0);
|
2020-06-04 18:58:00 +02:00
|
|
|
// 3. remove the e4 black pawn; the white pawn should not be able to jump above e3 pawn.
|
2020-06-21 15:57:58 +02:00
|
|
|
b.clear_square(E4, BLACK, PAWN);
|
2020-06-22 21:33:23 +02:00
|
|
|
let moves = get_legal_piece_moves(&b, &gs, E2, WHITE);
|
2020-06-04 18:58:00 +02:00
|
|
|
assert_eq!(moves.len(), 0);
|
2020-05-31 02:41:02 +02:00
|
|
|
|
|
|
|
// Check that a pawn can take a piece diagonally.
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(F3, BLACK, PAWN);
|
2020-06-22 21:33:23 +02:00
|
|
|
let moves = get_legal_piece_moves(&b, &gs, E2, WHITE);
|
2020-06-19 19:29:10 +02:00
|
|
|
assert!(moves.len() == 1 && moves.contains(&Move::new(E2, F3)));
|
|
|
|
b.set_square(D3, BLACK, PAWN);
|
2020-06-22 21:33:23 +02:00
|
|
|
let moves = get_legal_piece_moves(&b, &gs, E2, WHITE);
|
2020-05-31 02:41:02 +02:00
|
|
|
assert_eq!(moves.len(), 2);
|
2020-06-19 19:29:10 +02:00
|
|
|
assert!(moves.contains( &Move::new(E2, F3) ));
|
|
|
|
assert!(moves.contains( &Move::new(E2, D3) ));
|
2020-06-04 19:19:24 +02:00
|
|
|
|
|
|
|
// Check that a pawn moving to the last rank leads to queen promotion.
|
|
|
|
// 1. by simply moving forward.
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(A7, WHITE, PAWN);
|
2020-06-22 21:33:23 +02:00
|
|
|
let moves = get_legal_piece_moves(&b, &gs, A7, WHITE);
|
2020-06-19 19:29:10 +02:00
|
|
|
assert!(moves.len() == 1 && moves.contains(&Move::new_promotion(A7, A8, QUEEN)));
|
2020-05-31 02:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-05-31 19:00:56 +02:00
|
|
|
fn test_get_bishop_moves() {
|
2020-06-19 19:29:10 +02:00
|
|
|
let mut b = Board::new_empty();
|
2020-06-06 00:47:07 +02:00
|
|
|
let gs = GameState::new();
|
2020-05-31 02:41:02 +02:00
|
|
|
|
|
|
|
// A bishop has maximum range when it's in a center square.
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(D4, WHITE, BISHOP);
|
2020-06-22 21:33:23 +02:00
|
|
|
let moves = get_legal_piece_moves(&b, &gs, D4, WHITE);
|
2020-05-31 02:41:02 +02:00
|
|
|
assert_eq!(moves.len(), 13);
|
|
|
|
// Going top-right.
|
2020-06-19 19:29:10 +02:00
|
|
|
assert!(moves.contains(&Move::new(D4, E5)));
|
|
|
|
assert!(moves.contains(&Move::new(D4, F6)));
|
|
|
|
assert!(moves.contains(&Move::new(D4, G7)));
|
|
|
|
assert!(moves.contains(&Move::new(D4, H8)));
|
2020-05-31 02:41:02 +02:00
|
|
|
// Going bottom-right.
|
2020-06-19 19:29:10 +02:00
|
|
|
assert!(moves.contains(&Move::new(D4, E3)));
|
|
|
|
assert!(moves.contains(&Move::new(D4, F2)));
|
|
|
|
assert!(moves.contains(&Move::new(D4, G1)));
|
2020-05-31 02:41:02 +02:00
|
|
|
// Going bottom-left.
|
2020-06-19 19:29:10 +02:00
|
|
|
assert!(moves.contains(&Move::new(D4, C3)));
|
|
|
|
assert!(moves.contains(&Move::new(D4, B2)));
|
|
|
|
assert!(moves.contains(&Move::new(D4, A1)));
|
2020-05-31 02:41:02 +02:00
|
|
|
// Going top-left.
|
2020-06-19 19:29:10 +02:00
|
|
|
assert!(moves.contains(&Move::new(D4, C5)));
|
|
|
|
assert!(moves.contains(&Move::new(D4, B6)));
|
|
|
|
assert!(moves.contains(&Move::new(D4, A7)));
|
2020-05-31 02:41:02 +02:00
|
|
|
|
2020-06-06 00:47:07 +02:00
|
|
|
// When blocking commit to one square with friendly piece, lose 2 moves.
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(B2, WHITE, PAWN);
|
2020-06-22 21:33:23 +02:00
|
|
|
assert_eq!(get_legal_piece_moves(&b, &gs, D4, WHITE).len(), 11);
|
2020-05-31 02:41:02 +02:00
|
|
|
|
2020-06-06 00:47:07 +02:00
|
|
|
// When blocking commit to one square with enemy piece, lose only 1 move.
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(B2, BLACK, PAWN);
|
2020-06-22 21:33:23 +02:00
|
|
|
assert_eq!(get_legal_piece_moves(&b, &gs, D4, WHITE).len(), 12);
|
2020-05-31 02:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-05-31 19:00:56 +02:00
|
|
|
fn test_get_knight_moves() {
|
2020-06-19 19:29:10 +02:00
|
|
|
let mut b = Board::new_empty();
|
2020-06-06 00:47:07 +02:00
|
|
|
let gs = GameState::new();
|
2020-05-31 02:41:02 +02:00
|
|
|
|
2020-06-06 00:47:07 +02:00
|
|
|
// A knight never has blocked commit; if it's in the center of the board, it can have up to
|
2020-05-31 02:41:02 +02:00
|
|
|
// 8 moves.
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(D4, WHITE, KNIGHT);
|
2020-06-22 21:33:23 +02:00
|
|
|
assert_eq!(get_legal_piece_moves(&b, &gs, D4, WHITE).len(), 8);
|
2020-05-31 02:41:02 +02:00
|
|
|
|
|
|
|
// If on a side if has only 4 moves.
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(A4, WHITE, KNIGHT);
|
2020-06-22 21:33:23 +02:00
|
|
|
assert_eq!(get_legal_piece_moves(&b, &gs, A4, WHITE).len(), 4);
|
2020-05-31 02:41:02 +02:00
|
|
|
|
|
|
|
// And in a corner, only 2 moves.
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(A1, WHITE, KNIGHT);
|
2020-06-22 21:33:23 +02:00
|
|
|
assert_eq!(get_legal_piece_moves(&b, &gs, A1, WHITE).len(), 2);
|
2020-05-31 02:41:02 +02:00
|
|
|
|
|
|
|
// Add 2 friendly pieces and it is totally blocked.
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(B3, WHITE, PAWN);
|
|
|
|
b.set_square(C2, WHITE, PAWN);
|
2020-06-22 21:33:23 +02:00
|
|
|
assert_eq!(get_legal_piece_moves(&b, &gs, A1, WHITE).len(), 0);
|
2020-05-31 19:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_rook_moves() {
|
2020-06-19 19:29:10 +02:00
|
|
|
let mut b = Board::new_empty();
|
2020-06-06 00:47:07 +02:00
|
|
|
let gs = GameState::new();
|
2020-05-31 19:00:56 +02:00
|
|
|
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(D4, WHITE, ROOK);
|
2020-06-22 21:33:23 +02:00
|
|
|
assert_eq!(get_legal_piece_moves(&b, &gs, D4, WHITE).len(), 14);
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(D6, BLACK, PAWN);
|
2020-06-22 21:33:23 +02:00
|
|
|
assert_eq!(get_legal_piece_moves(&b, &gs, D4, WHITE).len(), 12);
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(D6, WHITE, PAWN);
|
2020-06-22 21:33:23 +02:00
|
|
|
assert_eq!(get_legal_piece_moves(&b, &gs, D4, WHITE).len(), 11);
|
2020-05-31 19:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_queen_moves() {
|
2020-06-19 19:29:10 +02:00
|
|
|
let mut b = Board::new_empty();
|
2020-06-06 00:47:07 +02:00
|
|
|
let gs = GameState::new();
|
2020-05-31 19:00:56 +02:00
|
|
|
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(D4, WHITE, QUEEN);
|
2020-06-22 21:33:23 +02:00
|
|
|
assert_eq!(get_legal_piece_moves(&b, &gs, D4, WHITE).len(), 14 + 13);
|
2020-05-31 02:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-05-31 19:00:56 +02:00
|
|
|
fn test_get_king_moves() {
|
2020-06-06 00:47:07 +02:00
|
|
|
let mut gs = GameState::new();
|
2020-05-31 02:41:02 +02:00
|
|
|
|
2020-06-06 00:47:07 +02:00
|
|
|
// King can move 1 square in any direction.
|
2020-06-19 19:29:10 +02:00
|
|
|
let mut b = Board::new_empty();
|
|
|
|
b.set_square(D4, WHITE, KING);
|
2020-06-22 21:33:23 +02:00
|
|
|
assert_eq!(get_legal_piece_moves(&b, &gs, D4, WHITE).len(), 8);
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(E5, WHITE, PAWN);
|
2020-06-22 21:33:23 +02:00
|
|
|
assert_eq!(get_legal_piece_moves(&b, &gs, D4, WHITE).len(), 7);
|
2020-06-06 00:47:07 +02:00
|
|
|
|
|
|
|
// If castling is available, other moves are possible: 5 moves + 2 castles.
|
2020-06-19 19:29:10 +02:00
|
|
|
let mut b = Board::new_empty();
|
|
|
|
b.set_square(E1, WHITE, KING);
|
|
|
|
b.set_square(A1, WHITE, ROOK);
|
|
|
|
b.set_square(H1, WHITE, ROOK);
|
2020-06-22 21:33:23 +02:00
|
|
|
assert_eq!(get_legal_piece_moves(&b, &gs, E1, WHITE).len(), 5 + 2);
|
2020-06-06 00:47:07 +02:00
|
|
|
|
|
|
|
// Castling works as well for black.
|
2020-06-19 19:29:10 +02:00
|
|
|
gs.color = BLACK;
|
|
|
|
b.set_square(E8, BLACK, KING);
|
|
|
|
b.set_square(A8, BLACK, ROOK);
|
|
|
|
b.set_square(H8, BLACK, ROOK);
|
2020-06-22 21:33:23 +02:00
|
|
|
assert_eq!(get_legal_piece_moves(&b, &gs, E8, BLACK).len(), 5 + 2);
|
2020-05-31 02:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-05-31 19:00:56 +02:00
|
|
|
fn test_filter_illegal_moves() {
|
2020-06-19 19:29:10 +02:00
|
|
|
let mut b = Board::new_empty();
|
2020-06-06 01:27:19 +02:00
|
|
|
let mut gs = GameState::new();
|
2020-05-31 02:41:02 +02:00
|
|
|
|
2020-05-31 19:00:56 +02:00
|
|
|
// Place white's king on first rank.
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(E1, WHITE, KING);
|
2020-05-31 19:00:56 +02:00
|
|
|
// Place black rook in second rank: king can only move left or right.
|
2020-06-19 19:29:10 +02:00
|
|
|
b.set_square(H2, BLACK, ROOK);
|
2020-06-06 01:27:19 +02:00
|
|
|
// No castling available.
|
|
|
|
gs.castling = 0;
|
|
|
|
// 5 moves in absolute but only 2 are legal.
|
2020-06-22 21:33:23 +02:00
|
|
|
let all_wh_moves = get_legal_piece_moves(&b, &gs, E1, WHITE);
|
2020-06-06 01:27:19 +02:00
|
|
|
assert_eq!(all_wh_moves.len(), 2);
|
2020-05-31 02:41:02 +02:00
|
|
|
}
|
|
|
|
}
|