Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
fix: fixed some bugs with zobrist hashing and ep/castling
Browse files Browse the repository at this point in the history
refactor: adjusted API for zobrist hashing
  • Loading branch information
dannyhammer committed Aug 13, 2024
1 parent bb96270 commit 0ed49ba
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 131 deletions.
24 changes: 22 additions & 2 deletions brogle/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use threadpool::ThreadPool;

use super::{
protocols::{UciCommand, UciEngine, UciOption, UciResponse, UciSearchOptions},
search::{Search, SearchResult},
search::{SearchResult, Searcher},
Evaluator,
};

Expand All @@ -29,6 +29,26 @@ enum EngineProtocol {
Uci,
}

/*
pub enum ScoreBound {
// The score is exact
Exact,
/// The score is less than alpha
Upper,
/// The score is greater than or equal to beta
Lower,
}
pub struct TranspositionTableEntry {
key: u64,
bestmove: Move,
depth: usize,
score: i32,
score_bound: ScoreBound,
age: usize,
}
*/

/// A chess engine responds to inputs (such as from a GUI or terminal) and
/// responds with computed outputs. The most common modern protocol is UCI.
///
Expand Down Expand Up @@ -597,7 +617,7 @@ impl UciEngine for Engine {
let cloned_result = Arc::clone(&result);

// Start the search
let search = Search::new(
let search = Searcher::new(
&game,
timeout,
cloned_stopper,
Expand Down
4 changes: 2 additions & 2 deletions brogle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ pub mod engine;
pub use engine::*;

pub mod search {
pub mod search_utils;
pub use search_utils::*;
pub mod searcher;
pub use searcher::*;
}

pub mod eval {
Expand Down
26 changes: 8 additions & 18 deletions brogle/src/protocols/uci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,19 +830,15 @@ pub trait UciEngine {
/// score [cp <x> | mate <y> | lowerbound | upperbound]
/// ```
///
/// - `cp <x>`
/// The score from the engine's point of view in centipawns.
/// - `cp <x>` - The score from the engine's point of view in centipawns.
///
/// - `mate <y>`
/// Mate in `y` moves, not plies.
/// - `mate <y>` - Mate in `y` moves, not plies.
///
/// If the engine is getting mated, use negative values for `y`.
///
/// - `lowerbound`
/// The score is just a lower bound.
/// - `lowerbound` - The score is just a lower bound.
///
/// - `upperbound`
/// The score is just an upper bound.
/// - `upperbound` - The score is just an upper bound.
///
/// ```text
/// currmove <move>
Expand Down Expand Up @@ -1597,19 +1593,13 @@ pub struct UciInfo {
/// score [cp <x> | mate <y> | lowerbound | upperbound]
/// ```
///
/// - `cp <x>`
/// The score from the engine's point of view in centipawns.
///
/// - `mate <y>`
/// Mate in `y` moves, not plies.
/// - `cp <x>` - The score from the engine's point of view in centipawns.
/// - `mate <y>` - Mate in `y` moves, not plies.
///
/// If the engine is getting mated, use negative values for `y`.
///
/// - `lowerbound`
/// The score is just a lower bound.
///
/// - `upperbound`
/// The score is just an upper bound.
/// - `lowerbound` - The score is just a lower bound.
/// - `upperbound` - The score is just an upper bound.
pub score: Option<String>,

/// ```text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::Sender;
use std::sync::{Arc, RwLock};
use std::time::Duration;
use std::usize;
use std::{ops::Neg, time::Instant};

use anyhow::{bail, Result};
Expand Down Expand Up @@ -84,7 +83,7 @@ impl Default for SearchData {
}

/// A struct to encapsulate the logic of searching through moves for a given a chess position.
pub struct Search<'a> {
pub struct Searcher<'a> {
game: &'a Game,
timeout: Duration,
stopper: Arc<AtomicBool>,
Expand All @@ -101,7 +100,7 @@ pub struct Search<'a> {
*/
}

impl<'a> Search<'a> {
impl<'a> Searcher<'a> {
/// Create a new search that will search the provided position at a depth of 1.
pub fn new(
game: &'a Game,
Expand Down
7 changes: 4 additions & 3 deletions brogle_core/brogle_types/src/magicgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ struct MagicEntry {
fn magic_index(entry: &MagicEntry, blockers: Bitboard) -> usize {
let blockers = blockers.0 & entry.mask;
let hash = blockers.wrapping_mul(entry.magic);
let index = (hash >> entry.shift) as usize;
// let index = (hash >> entry.shift) as usize;
// entry.offset + index
index
// index
(hash >> entry.shift) as usize
}

// #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -111,7 +112,7 @@ fn find_and_print_all_magics(slider: &SlidingPiece, piece_name: &str) {
let mut table_size = 0;

for tile in Tile::iter() {
let index_bits = slider.blockers(tile).population() as u8;
let index_bits = slider.blockers(tile).population();
let (entry, table) = find_magic(slider, tile, index_bits);

println!(
Expand Down
16 changes: 13 additions & 3 deletions brogle_core/brogle_types/src/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use super::{Bitboard, Color};
///
/// This bit pattern is also known as [Least Significant File Mapping](https://www.chessprogramming.org/Square_Mapping_Considerations#Deduction_on_Files_and_Ranks),
/// so `tile = file + rank * 8`.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[repr(transparent)]
pub struct Tile(pub(crate) u8);

Expand Down Expand Up @@ -695,7 +695,7 @@ impl fmt::Debug for Tile {
}
}

#[derive(Clone, Copy, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Eq, PartialOrd, Ord, Hash, Default)]
#[repr(transparent)]
pub struct Rank(pub(crate) u8);

Expand Down Expand Up @@ -836,6 +836,11 @@ impl Rank {
}
}

/// `const` analog of `==`.
pub const fn is(&self, other: &Self) -> bool {
self.0 == other.0
}

// Index in Little Endian (default)
pub const fn index_le(&self) -> usize {
self.0 as usize
Expand Down Expand Up @@ -1053,7 +1058,7 @@ impl fmt::Debug for Rank {
}
}

#[derive(Clone, Copy, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Eq, PartialOrd, Ord, Hash, Default)]
pub struct File(pub(crate) u8);

impl File {
Expand Down Expand Up @@ -1131,6 +1136,11 @@ impl File {
Self::new(file_int)
}

/// `const` analog of `==`.
pub const fn is(&self, other: &Self) -> bool {
self.0 == other.0
}

pub const fn inner(&self) -> u8 {
self.0
}
Expand Down
31 changes: 22 additions & 9 deletions brogle_core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,26 @@ fn main() {
println!("pos: {}\nkey: {}", game.position(), game.key());
*/

let mut game = Game::default();
game.make_move(Move::from_uci(&game, "b1a3").unwrap());
println!("repetition? {}", game.is_repetition());
game.make_move(Move::from_uci(&game, "b8a6").unwrap());
println!("repetition? {}", game.is_repetition());
game.make_move(Move::from_uci(&game, "a3b1").unwrap());
println!("repetition? {}", game.is_repetition());
game.make_move(Move::from_uci(&game, "a6b8").unwrap());
println!("repetition? {}", game.is_repetition());
// let fen = FEN_STARTPOS;
// let moves = ["b1a3", "b8a6", "a3b1", "a6b8"];
// let fen = "k7/8/8/8/3p4/8/4P3/K7 w - - 0 1"; // Testing hash keys with en passant
// let moves = ["e2e4", "d4e3"];
let fen = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1";
let moves = ["e1g1"];
let mut game = Game::from_fen(fen).unwrap();

for mv in moves {
game.make_move(Move::from_uci(&game, mv).unwrap());
// println!("repetition? {}", game.is_repetition());
}

// let mut game = Game::default();
// game.make_move(Move::from_uci(&game, "b1a3").unwrap());
// println!("repetition? {}", game.is_repetition());
// game.make_move(Move::from_uci(&game, "b8a6").unwrap());
// println!("repetition? {}", game.is_repetition());
// game.make_move(Move::from_uci(&game, "a3b1").unwrap());
// println!("repetition? {}", game.is_repetition());
// game.make_move(Move::from_uci(&game, "a6b8").unwrap());
// println!("repetition? {}", game.is_repetition());
}
Loading

0 comments on commit 0ed49ba

Please sign in to comment.