Skip to content

Commit 8a3ea99

Browse files
feat: add initial implementation of killers table
1 parent 28115e3 commit 8a3ea99

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

engine/src/defs.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
#[rustfmt::skip]
1616
const BANNER: &str = r#"
17-
_ _ _ _ _ _
18-
| |__ _ _| |_ ___ ___| |___ _ (_)__ _| |_| |_
17+
_ _ _ _ _ _
18+
| |__ _ _| |_ ___ ___| |___ _ (_)__ _| |_| |_
1919
| '_ \ || | _/ -_)___| / / ' \| / _` | ' \ _|
2020
|_.__/\_, |\__\___| |_\_\_||_|_\__, |_||_\__|
21-
|__/ |___/
21+
|__/ |___/
2222
"#;
2323

2424
pub struct About;
@@ -32,3 +32,4 @@ impl About {
3232
}
3333

3434
pub(crate) const MAX_DEPTH: u8 = 128;
35+
pub(crate) const MAX_KILLERS_PER_PLY: usize = 2;

engine/src/killer_moves_table.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use chess::moves::Move;
2+
3+
use crate::defs::{MAX_DEPTH, MAX_KILLERS_PER_PLY};
4+
5+
pub struct KillerMovesTable {
6+
table: [[Option<Move>; MAX_KILLERS_PER_PLY]; MAX_DEPTH as usize],
7+
}
8+
9+
impl KillerMovesTable {
10+
pub(crate) fn new() -> Self {
11+
let table = [[None; MAX_KILLERS_PER_PLY]; MAX_DEPTH as usize];
12+
13+
Self { table }
14+
}
15+
16+
pub(crate) fn get(&self, ply: u8) -> &[Option<Move>] {
17+
assert!(ply < MAX_DEPTH, "Depth is out of bounds");
18+
19+
&self.table[ply as usize][..]
20+
}
21+
22+
fn get_mut(&mut self, ply: u8) -> &mut [Option<Move>] {
23+
assert!(ply < MAX_DEPTH, "Depth is out of bounds");
24+
25+
&mut self.table[ply as usize][..]
26+
}
27+
28+
pub(crate) fn update(&mut self, ply: u8, mv: Move) {
29+
assert!(ply < MAX_DEPTH, "Depth is out of bounds");
30+
31+
let current_killers = self.get_mut(ply);
32+
if !current_killers[0].is_some_and(|killer_mv| killer_mv == mv) {
33+
current_killers.swap(0, 1);
34+
current_killers[0] = Some(mv);
35+
}
36+
}
37+
38+
pub(crate) fn clear(&mut self) {
39+
for item in self.table.as_flattened_mut() {
40+
*item = None;
41+
}
42+
}
43+
}
44+
45+
impl Default for KillerMovesTable {
46+
fn default() -> Self {
47+
Self::new()
48+
}
49+
}
50+
51+
#[cfg(test)]
52+
mod tests {
53+
54+
use crate::defs::{MAX_DEPTH, MAX_KILLERS_PER_PLY};
55+
56+
use super::KillerMovesTable;
57+
58+
#[test]
59+
60+
fn initialize_killers_table() {
61+
let killers_table: KillerMovesTable = Default::default();
62+
for i in 0..MAX_DEPTH {
63+
let killers = killers_table.get(i);
64+
assert_eq!(killers, &[None, None]);
65+
assert_eq!(killers.len(), MAX_KILLERS_PER_PLY);
66+
}
67+
}
68+
}

engine/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod hce_values;
1010
pub mod history_table;
1111
mod inplace_incremental_sort;
1212
pub mod input_handler;
13+
pub mod killer_moves_table;
1314
mod lmr;
1415
pub mod log_level;
1516
mod move_order;

0 commit comments

Comments
 (0)