Skip to content

Commit

Permalink
cleanup and add rand
Browse files Browse the repository at this point in the history
  • Loading branch information
Heiaha committed Nov 21, 2024
1 parent b8da503 commit 0f53130
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 64 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rust-version = "1.80.0"
[dependencies]
arrayvec = "0.7.6"
regex = "1.11.1"
rand="0.8.5"

[profile.release]
opt-level = 3
Expand Down
11 changes: 0 additions & 11 deletions src/move_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,7 @@ use super::types::*;

use arrayvec::ArrayVec;

// Cache size consideration idea originally found in Pleco

// Make sure the move lists are aligned into lengths such that the memory is
// in an integer number of cache chunks. The is for a 32 bit Move.
// https://www.youtube.com/watch?v=WDIkqP4JbkE

#[cfg(target_pointer_width = "64")]
pub const MAX_MOVES: usize = 252;
#[cfg(target_pointer_width = "32")]
const MAX_MOVES: usize = 254;
#[cfg(target_pointer_width = "16")]
const MAX_MOVES: usize = 255;

pub struct MoveListEntry {
pub m: Move,
Expand Down
2 changes: 1 addition & 1 deletion src/piece.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl fmt::Display for Piece {

impl Piece {
pub const N_PIECES: usize = 12;
const PIECE_STR: &'static str = "PNBRQK pnbrqk ";
const PIECE_STR: &'static str = "PNBRQK pnbrqk";
}

#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Debug)]
Expand Down
80 changes: 42 additions & 38 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl TimeControl {
m.map(|m| {
m.as_str()
.parse::<u64>()
.map_err(|_| "Unable to parse wtime.")
.map_err(|_| "Unable to parse time.")
.map(Duration::from_millis)
})
.transpose()
Expand All @@ -46,16 +46,11 @@ impl TryFrom<&str> for TimeControl {

let re_captures = GO_RE.captures(line).ok_or("Invalid go format.")?;

if re_captures.name("ponder").is_some() {
return Err("Ponder is not implemented.");
}

if re_captures.name("searchmoves").is_some() {
return Err("Searchmoves is not implemented.");
}

if re_captures.name("mate").is_some() {
return Err("Mate is not implemented.");
if re_captures.name("ponder").is_some()
|| re_captures.name("searchmoves").is_some()
|| re_captures.name("mate").is_some()
{
return Err("Feature is not implemented.");
}

let mut count = 0;
Expand Down Expand Up @@ -162,33 +157,11 @@ impl Timer {
stop: Arc<AtomicBool>,
overhead: Duration,
) -> Self {
let mut time_target = Duration::ZERO;
let mut time_maximum = Duration::ZERO;

if let TimeControl::Variable {
wtime,
btime,
winc,
binc,
moves_to_go,
} = control
{
let (time, inc) = match board.ctm() {
Color::White => (wtime, winc),
Color::Black => (btime, binc),
};

let mtg = moves_to_go.unwrap_or_else(|| {
(Self::MTG_INTERCEPT
+ Self::MTG_EVAL_WEIGHT * (board.simple_eval().abs() as f32)
+ Self::MTG_MOVE_WEIGHT * (board.fullmove_number() as f32))
.ceil()
.max(1.0) as u32
});

time_target = time.min(time / mtg + inc.unwrap_or(Duration::ZERO));
time_maximum = time_target + (time - time_target) / 4;
}
let (time_target, time_maximum) = if let TimeControl::Variable { .. } = control {
Self::calculate_time(board, control)
} else {
(Duration::ZERO, Duration::ZERO)
};

Self {
start_time: Instant::now(),
Expand All @@ -202,6 +175,37 @@ impl Timer {
}
}

fn calculate_time(board: &Board, control: TimeControl) -> (Duration, Duration) {
let TimeControl::Variable {
wtime,
btime,
winc,
binc,
moves_to_go,
} = control
else {
unreachable!()
};

let (time, inc) = match board.ctm() {
Color::White => (wtime, winc),
Color::Black => (btime, binc),
};

let mtg = moves_to_go.unwrap_or_else(|| {
(Self::MTG_INTERCEPT
+ Self::MTG_EVAL_WEIGHT * (board.simple_eval().abs() as f32)
+ Self::MTG_MOVE_WEIGHT * (board.fullmove_number() as f32))
.ceil()
.max(1.0) as u32
});

let time_target = time.min(time / mtg + inc.unwrap_or(Duration::ZERO));
let time_maximum = time_target + (time - time_target) / 4;

(time_target, time_maximum)
}

pub fn start_check(&self, depth: Depth) -> bool {
if self.stop.load(sync::atomic::Ordering::Relaxed) {
return false;
Expand Down
25 changes: 11 additions & 14 deletions src/zobrist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@ use super::piece::*;
use super::square::*;
use super::types::*;

struct Rng(Hash);

impl Rng {
fn gen(&mut self) -> Hash {
self.0 ^= self.0 >> 12;
self.0 ^= self.0 << 25;
self.0 ^= self.0 >> 27;
self.0.wrapping_mul(2685821657736338717)
}
}
use rand::rngs::StdRng;
use rand::{RngCore, SeedableRng};

#[derive(Clone)]
pub struct Hasher {
Expand All @@ -25,15 +17,20 @@ pub struct Hasher {

impl Hasher {
pub fn new() -> Self {
let mut rng = Rng(1070372);
let mut rng = StdRng::seed_from_u64(1070372);

let mut zobrist_table = [0; SQ::N_SQUARES * Piece::N_PIECES];
let mut zobrist_ep = [0; File::N_FILES];
let zobrist_color = rng.gen();

zobrist_table.iter_mut().for_each(|hash| *hash = rng.gen());
let zobrist_color = rng.next_u64();

zobrist_table
.iter_mut()
.for_each(|hash| *hash = rng.next_u64());

zobrist_ep.iter_mut().for_each(|hash| *hash = rng.gen());
zobrist_ep
.iter_mut()
.for_each(|hash| *hash = rng.next_u64());

Self {
hash: 0,
Expand Down

0 comments on commit 0f53130

Please sign in to comment.