Skip to content

Commit

Permalink
fix: Add forgotten files
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillbobyrev committed Jun 17, 2024
1 parent b6fa456 commit 43d9133
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/chess/zobrist.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//! Implements Zobrist hashing and [Transposition Table] functionality.
//!
//! [Transposition Table](https://www.chessprogramming.org/Transposition_Table

Check warning on line 3 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Build docs

this URL is not a hyperlink
// TODO: Migrate to RawTable instead for better performance?
use std::collections::HashMap;
use std::hash::{Hash, Hasher};

Check warning on line 7 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, nightly, true)

unused imports: `Hash` and `Hasher`

Check warning on line 7 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, nightly, true)

unused imports: `Hash` and `Hasher`

Check warning on line 7 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable, false)

unused imports: `Hash`, `Hasher`

Check warning on line 7 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, 1.79.0, false)

unused imports: `Hash`, `Hasher`

Check warning on line 7 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable, true)

unused imports: `Hash`, `Hasher`

Check warning on line 7 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, nightly, true)

unused imports: `Hash` and `Hasher`

Check warning on line 7 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable, true)

unused imports: `Hash`, `Hasher`

use super::position::Position;

Check warning on line 9 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, nightly, true)

unused import: `super::position::Position`

Check warning on line 9 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, nightly, true)

unused import: `super::position::Position`

Check warning on line 9 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable, false)

unused import: `super::position::Position`

Check warning on line 9 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, 1.79.0, false)

unused import: `super::position::Position`

Check warning on line 9 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable, true)

unused import: `super::position::Position`

Check warning on line 9 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, nightly, true)

unused import: `super::position::Position`

Check warning on line 9 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable, true)

unused import: `super::position::Position`

/// Zobrist key is a 64-bit integer.
pub type Key = u64;

pub(crate) struct RepetitionTable {
table: HashMap<Key, u8>,
}

impl RepetitionTable {
pub(crate) fn new() -> Self {
Self {
table: HashMap::new(),
}
}

pub(crate) fn clear(&mut self) {
self.table.clear();
}

/// Returns true if the position has occurred 3 times.
pub(crate) fn record(&mut self, key: Key) -> bool {
let count = self.table.entry(key).or_insert(0);
*count += 1;
*count == 3
}

/// Reduces the number of times a position has occurred by one.
pub(crate) fn remove(&mut self, key: Key) {
let count = self.table.entry(key).or_insert(0);
match count {
1 => {
let _ = self.table.remove_entry(&key);
},
2 | 3 => {
*count -= 1;
},
_ => {
unreachable!("can not call remove on position that has not occurred yet")
},
}
}
}

mod test {
use super::*;

Check warning on line 54 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, nightly, true)

unused import: `super::*`

Check warning on line 54 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, nightly, true)

unused import: `super::*`

Check warning on line 54 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable, false)

unused import: `super::*`

Check warning on line 54 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, 1.79.0, false)

unused import: `super::*`

Check warning on line 54 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable, true)

unused import: `super::*`

Check warning on line 54 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, nightly, true)

unused import: `super::*`

Check warning on line 54 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable, true)

unused import: `super::*`
use crate::chess::core::Move;

Check warning on line 55 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, nightly, true)

unused import: `crate::chess::core::Move`

Check warning on line 55 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, nightly, true)

unused import: `crate::chess::core::Move`

Check warning on line 55 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable, false)

unused import: `crate::chess::core::Move`

Check warning on line 55 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, 1.79.0, false)

unused import: `crate::chess::core::Move`

Check warning on line 55 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable, true)

unused import: `crate::chess::core::Move`

Check warning on line 55 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, nightly, true)

unused import: `crate::chess::core::Move`

Check warning on line 55 in src/chess/zobrist.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable, true)

unused import: `crate::chess::core::Move`

#[test]
fn repetition_table() {
let mut table = RepetitionTable::new();

let mut position = Position::starting();
assert!(!table.record(position.compute_hash()));

position.make_move(&Move::from_uci("g1f3").expect("valid move"));
assert!(!table.record(position.compute_hash()));
position.make_move(&Move::from_uci("g8f6").expect("valid move"));
assert!(!table.record(position.compute_hash()));

position.make_move(&Move::from_uci("f3g1").expect("valid move"));
assert!(!table.record(position.compute_hash()));
// Two-fold repetition.
position.make_move(&Move::from_uci("f6g8").expect("valid move"));
assert!(!table.record(position.compute_hash()));

position.make_move(&Move::from_uci("g1f3").expect("valid move"));
assert!(!table.record(position.compute_hash()));
position.make_move(&Move::from_uci("g8f6").expect("valid move"));
assert!(!table.record(position.compute_hash()));

position.make_move(&Move::from_uci("f3g1").expect("valid move"));
assert!(!table.record(position.compute_hash()));
// Three-fold repetition.
position.make_move(&Move::from_uci("f6g8").expect("valid move"));
assert!(table.record(position.compute_hash()));
}
}
33 changes: 33 additions & 0 deletions src/search/transposition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! Implements Zobrist hashing and [Transposition Table] functionality.
//!
//! [Transposition Table](https://www.chessprogramming.org/Transposition_Table

Check warning on line 3 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Build docs

this URL is not a hyperlink
use crate::chess::position::Position;

Check warning on line 5 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, nightly, true)

unused import: `crate::chess::position::Position`

Check warning on line 5 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, nightly, true)

unused import: `crate::chess::position::Position`

Check warning on line 5 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable, false)

unused import: `crate::chess::position::Position`

Check warning on line 5 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, 1.79.0, false)

unused import: `crate::chess::position::Position`

Check warning on line 5 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable, true)

unused import: `crate::chess::position::Position`

Check warning on line 5 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, nightly, true)

unused import: `crate::chess::position::Position`

Check warning on line 5 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable, true)

unused import: `crate::chess::position::Position`
use core::hash;

Check warning on line 6 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, nightly, true)

unused import: `core::hash`

Check warning on line 6 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, nightly, true)

unused import: `core::hash`

Check warning on line 6 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable, false)

unused import: `core::hash`

Check warning on line 6 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, 1.79.0, false)

unused import: `core::hash`

Check warning on line 6 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable, true)

unused import: `core::hash`

Check warning on line 6 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, nightly, true)

unused import: `core::hash`

Check warning on line 6 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable, true)

unused import: `core::hash`
use std::hash::{Hash, Hasher};

Check warning on line 7 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, nightly, true)

unused imports: `Hash` and `Hasher`

Check warning on line 7 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, nightly, true)

unused imports: `Hash` and `Hasher`

Check warning on line 7 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable, false)

unused imports: `Hash`, `Hasher`

Check warning on line 7 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, 1.79.0, false)

unused imports: `Hash`, `Hasher`

Check warning on line 7 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable, true)

unused imports: `Hash`, `Hasher`

Check warning on line 7 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, nightly, true)

unused imports: `Hash` and `Hasher`

Check warning on line 7 in src/search/transposition.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable, true)

unused imports: `Hash`, `Hasher`

pub type Key = u64;

pub(super) struct Entry {}

pub(super) struct TranspositionTable {}

impl TranspositionTable {
#[must_use]
pub(super) fn new() -> Self {
todo!()
}

pub(super) fn clear(&mut self) {
todo!()
}

#[must_use]
pub(super) fn probe(&self, key: u64) -> Option<&Entry> {
todo!()
}

pub(super) fn store(&mut self, key: u64, entry: Entry) {
todo!()
}
}
Empty file added src/search/worker.rs
Empty file.

0 comments on commit 43d9133

Please sign in to comment.