1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
use std::cmp::Ordering;
use crate::moves::Move;
use super::Grossmeister;
/// https://www.chessprogramming.org/Node_Types
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum NodeType {
/// Principal variation node - exact score
PV,
/// Fail-high
Cut,
/// Fail-low
All,
}
impl NodeType {
fn cmp_order(&self) -> u8 {
match self {
NodeType::PV => 2,
NodeType::Cut => 1,
NodeType::All => 0,
}
}
}
impl PartialOrd for NodeType {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp_order().cmp(&other.cmp_order()))
}
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct TranspositionTableItem {
/// Zobrist hash of this position
pub hash: u64,
pub mov: Option<Move>,
pub depth: u8,
pub score: f32,
pub node_type: NodeType,
}
impl PartialOrd for TranspositionTableItem {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
// Order by depth first, then node type
let depth_ordering = self.depth.partial_cmp(&other.depth);
if depth_ordering != Some(Ordering::Equal) {
return depth_ordering
}
self.node_type.partial_cmp(&other.node_type)
}
}
pub const TTABLE_SIZE: u64 = 2u64.pow(24);
pub type TranspositionTable = Vec<Option<TranspositionTableItem>>;
impl Grossmeister {
fn transposition_ref(&mut self) -> &mut Option<TranspositionTableItem> {
&mut self.transposition_table[(self.board.hash % TTABLE_SIZE) as usize]
}
/// Find current position in Transposition Table
/// This operation is safe from collisions since it compares the *full* hash
pub fn transposition(&mut self) -> Option<TranspositionTableItem> {
let hash = self.board.hash;
match self.transposition_ref() {
Some(item) => {
if item.hash == hash {
return Some(*item)
}
None
}
None => None
}
}
pub fn store_transposition(&mut self, transposition: TranspositionTableItem) {
let transposition_ref = self.transposition_ref();
match transposition_ref {
Some(existing_transposition) => {
if transposition >= *existing_transposition {
*transposition_ref = Some(transposition)
}
}
None => *transposition_ref = Some(transposition)
}
}
}
|