Skip to content

Commit

Permalink
Do not make move without need
Browse files Browse the repository at this point in the history
  • Loading branch information
bdmendes committed Dec 15, 2024
1 parent 0f4a157 commit 7820320
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 33 deletions.
18 changes: 4 additions & 14 deletions src/core/bitboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ const RANK_MASK: [Bitboard; 8] = {
arr
};

const FROM_SQUARE: [u64; 64] = {
let mut arr = [0; 64];
let mut square = 0;
while square < 64 {
arr[square] = 1 << square as u64;
square += 1;
}
arr
};

#[derive(
Default, Copy, Clone, Debug, PartialEq, Eq, BitOr, BitAnd, Shl, Shr, ShlAssign, ShrAssign, Not,
)]
Expand All @@ -55,19 +45,19 @@ impl Bitboard {
}

pub const fn from_square(square: Square) -> Self {
Bitboard(FROM_SQUARE[square as usize])
Bitboard(1 << square as usize)
}

pub const fn is_set(&self, square: Square) -> bool {
(self.0 & FROM_SQUARE[square as usize]) != 0
(self.0 & 1 << square as usize) != 0
}

pub fn set(&mut self, square: Square) {
self.0 |= FROM_SQUARE[square as usize];
self.0 |= 1 << square as usize;
}

pub fn clear(&mut self, square: Square) {
self.0 &= !FROM_SQUARE[square as usize];
self.0 &= !(1 << square as usize);
}

pub const fn count_ones(&self) -> u32 {
Expand Down
4 changes: 4 additions & 0 deletions src/core/moves/gen/magics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ pub fn rook_attacks(position: &Position, square: Square) -> Bitboard {
magic.attacks[index]
}

pub fn queen_attacks(position: &Position, square: Square) -> Bitboard {
bishop_attacks(position, square) | rook_attacks(position, square)
}

#[cfg(test)]
mod tests {
use std::str::FromStr;
Expand Down
41 changes: 31 additions & 10 deletions src/core/moves/gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::core::{
};
use castle::castle_moves;
use leapers::{king_attackers, king_regular_moves, knight_attackers, knight_moves};
use magics::queen_attacks;
use pawns::{pawn_attackers, pawn_moves};
use sliders::{bishop_moves, diagonal_attackers, file_attackers, queen_moves, rook_moves};

Expand All @@ -16,21 +17,41 @@ mod sliders;
pub fn generate_moves(position: &Position, stage: MoveStage) -> Vec<Move> {
let mut moves = Vec::with_capacity(64);

pawn_moves(position, stage, &mut moves);
knight_moves(position, stage, &mut moves);
let our_king = position.pieces_color_bb(Piece::King, position.side_to_move).next().unwrap();
let king_attackers = square_attackers(position, our_king, position.side_to_move.flipped());
let king_ray = queen_attacks(position, our_king);

king_regular_moves(position, stage, &mut moves);
bishop_moves(position, stage, &mut moves);
rook_moves(position, stage, &mut moves);
queen_moves(position, stage, &mut moves);
castle_moves(position, stage, &mut moves);

moves.retain(|m| {
match m.flag() {
if king_attackers.count_ones() <= 1 {
pawn_moves(position, stage, &mut moves);
knight_moves(position, stage, &mut moves);
bishop_moves(position, stage, &mut moves);
rook_moves(position, stage, &mut moves);
queen_moves(position, stage, &mut moves);
if king_attackers.is_empty() {
castle_moves(position, stage, &mut moves);
}
}

moves.retain(|mov| {
match mov.flag() {
MoveFlag::KingsideCastle | MoveFlag::QueensideCastle => return true,
MoveFlag::Quiet | MoveFlag::DoublePawnPush if mov.from() != our_king => {
// If in check, we must try to block the king rays.
if !king_attackers.is_empty() && !king_ray.is_set(mov.to()) {
return false;
}

// If not in check and not removing from king rays, we're sure this is legal.
if king_attackers.is_empty() && !king_ray.is_set(mov.from()) {
return true;
}
}
_ => (),
}
};

let mut new_position = make_move::<false>(position, *m);
let mut new_position = make_move::<false>(position, *mov);
!new_position.is_check()
});

Expand Down
10 changes: 2 additions & 8 deletions src/core/moves/gen/sliders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
},
};

use super::magics::{bishop_attacks, rook_attacks};
use super::magics::{bishop_attacks, queen_attacks, rook_attacks};

pub static ROOK_MOVE_DIRECTIONS: [Direction; 4] =
[Square::NORTH, Square::EAST, Square::SOUTH, Square::WEST];
Expand Down Expand Up @@ -110,13 +110,7 @@ pub fn bishop_moves(position: &Position, stage: MoveStage, moves: &mut Vec<Move>
}

pub fn queen_moves(position: &Position, stage: MoveStage, moves: &mut Vec<Move>) {
slider_moves(
Piece::Queen,
|p, sq| rook_attacks(p, sq) | bishop_attacks(p, sq),
position,
stage,
moves,
);
slider_moves(Piece::Queen, queen_attacks, position, stage, moves);
}

#[cfg(test)]
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::core::{fen::START_POSITION, Position};
use crate::core::Position;
use core::fen::START_POSITION;
use std::{str::FromStr, time::Instant};

#[allow(unused)]
Expand Down

0 comments on commit 7820320

Please sign in to comment.