Skip to content

Commit

Permalink
Implement Magic BitBoard move generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ParthPant committed Mar 16, 2023
1 parent 158cac3 commit 6528bb9
Show file tree
Hide file tree
Showing 5 changed files with 376 additions and 3 deletions.
37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ egui = "0.20.0"
egui-winit = "0.20.1"
egui-wgpu = "0.20.0"
fontdue = "0.7.2"
rand = "0.8.5"
14 changes: 14 additions & 0 deletions src/data/bitboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ impl BitBoard {
self.0 &= !((1 as u64) << Self::bit_from_xy(x, y) as u64);
}

pub fn set(&mut self, sq: usize) {
self.0 |= (1 as u64) << sq;
}

pub fn unset(&mut self, sq: usize) {
self.0 &= !((1 as u64) << sq);
}

pub fn move_xy_to_xy(&mut self, prev: (usize, usize), new: (usize, usize)) {
self.remove(prev.0, prev.1);
self.add(new.0, new.1);
Expand All @@ -31,6 +39,12 @@ impl BitBoard {
}
}

impl From<BitBoard> for u64 {
fn from(value: BitBoard) -> Self {
value.0
}
}

impl From<u64> for BitBoard {
fn from(value: u64) -> Self {
Self(value)
Expand Down
80 changes: 78 additions & 2 deletions src/generator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,81 @@
pub mod tables;

pub struct MoveGenerator {}
use tables::*;

impl MoveGenerator {}
use crate::data::{bitboard::BitBoard, piece::BoardPiece};

pub struct MoveGenerator {
rook_magics: [MagicEntry; 64],
bishop_magics: [MagicEntry; 64],

rook_moves: Vec<Vec<u64>>,
bishop_moves: Vec<Vec<u64>>,
}

impl Default for MoveGenerator {
fn default() -> Self {
let mut rook_magics = [MagicEntry::default(); 64];
let mut bishop_magics = [MagicEntry::default(); 64];

let mut rook_moves: Vec<Vec<u64>> = vec![vec![]; 64];
let mut bishop_moves: Vec<Vec<u64>> = vec![vec![]; 64];

log::info!("Generating Magic Entries");
for i in 0..64 {
let (bishop_magic, bishop_move_tbl) = find_magic(i, BoardPiece::WhiteBishop);
bishop_magics[i] = bishop_magic;
bishop_moves[i] = bishop_move_tbl;
log::trace!(
"Bishop Magic Entry for square {i}\nMagic: {:?}",
bishop_magic
);

let (rook_magic, rook_move_tbl) = find_magic(i, BoardPiece::WhiteRook);
rook_magics[i] = rook_magic;
rook_moves[i] = rook_move_tbl;
log::trace!("Rook Magic Entry for square {i}\nMagic: {:?}", rook_magic);
}

MoveGenerator {
rook_magics,
bishop_magics,

rook_moves,
bishop_moves,
}
}
}

impl MoveGenerator {
pub fn get_rook_atk(&self, sq: usize, blockers: u64) -> u64 {
let magic = self.rook_magics[sq];
let moves = &self.rook_moves[sq];
moves[magic_index(&magic, blockers)]
}

pub fn get_bishop_atk(&self, sq: usize, blockers: u64) -> u64 {
let magic = self.bishop_magics[sq];
let moves = &self.bishop_moves[sq];
moves[magic_index(&magic, blockers)]
}

pub fn get_queen_atk(&self, sq: usize, blockers: u64) -> u64 {
self.get_rook_atk(sq, blockers) | self.get_bishop_atk(sq, blockers)
}

pub fn get_white_pawn_atk(&self, sq: usize) -> u64 {
WP_ATK_TBL[sq]
}

pub fn get_black_pawn_atk(&self, sq: usize) -> u64 {
BP_ATK_TBL[sq]
}

pub fn get_knight_atk(&self, sq: usize) -> u64 {
N_ATK_TBL[sq]
}

pub fn get_king_atk(&self, sq: usize) -> u64 {
K_ATK_TBL[sq]
}
}
Loading

0 comments on commit 6528bb9

Please sign in to comment.