Skip to content

Commit

Permalink
Detect threefold repetition (#15)
Browse files Browse the repository at this point in the history
* Refactor alpha-beta search a bit
* Zobrist hashing
* Add GameState::to_fen()
* Small performance improvements to Zobrist hashes
* v0.11
  • Loading branch information
oriolarcas committed Jun 2, 2024
1 parent 71f3715 commit aeca696
Show file tree
Hide file tree
Showing 20 changed files with 1,174 additions and 313 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
run: npm install
if: startsWith(github.ref, 'refs/tags/')
- name: Install Rust dependencies
run: cargo install tauri-cli
run: cargo install tauri-cli@^2.0.0-beta
if: startsWith(github.ref, 'refs/tags/')
- name: Build
working-directory: ./src-tauri
Expand Down Expand Up @@ -69,7 +69,7 @@ jobs:
- name: Install React dependencies
run: npm install
- name: Install Rust dependencies
run: cargo install tauri-cli
run: cargo install tauri-cli@^2.0.0-beta
- name: Build
working-directory: ./src-tauri
run: cargo tauri build -b dmg
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions bump_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ echo Updating package.json
npm version --allow-same-version --commit-hooks false --git-tag-version false "$new_ver"

echo Updating Cargo.toml
cd src-tauri
cargo bump "$new_ver"
cargo set-version -p chusst-gen "$new_ver"

echo Updating tauri.conf.json
sed -E -i'' "s/\"version\": \"[0-9.]+\"/\"version\": \"$new_ver\"/" tauri.conf.json
sed -E -i'' "s/\"version\": \"[0-9.]+\"/\"version\": \"$new_ver\"/" src-tauri/tauri.conf.json
2 changes: 1 addition & 1 deletion chusst-gen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chusst-gen"
version = "0.10.0"
version = "0.11.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
15 changes: 9 additions & 6 deletions chusst-gen/benches/search.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use divan::Bencher;

use chusst_gen::eval::{Game, SilentSearchFeedback};
use chusst_gen::eval::{Game, GameHistory, SilentSearchFeedback};
use chusst_gen::game::BitboardGame;

#[divan::bench]
fn search(bench: Bencher) {
bench.bench_local(|| {
let game = BitboardGame::new();
let history = GameHistory::new();

let best_branch = game
.get_best_move_recursive(4, &mut (), &mut SilentSearchFeedback::default())
.get_best_move_recursive(&history, 4, &mut (), &mut SilentSearchFeedback::default())
.unwrap();

best_branch.searched
Expand Down Expand Up @@ -123,9 +124,11 @@ fn game_benchmark() -> u64 {
// use std::io::Write;

let mut game = BitboardGame::new();
let get_best_move_helper = |game: &mut BitboardGame| {
let mut history = GameHistory::new();

let get_best_move_helper = |game: &mut BitboardGame, history: &mut GameHistory| {
let best_branch = game
.get_best_move_recursive(3, &mut (), &mut SilentSearchFeedback::default())
.get_best_move_recursive(history, 3, &mut (), &mut SilentSearchFeedback::default())
.unwrap();

(best_branch.searched, best_branch.moves.first().unwrap().mv)
Expand All @@ -147,14 +150,14 @@ fn game_benchmark() -> u64 {
// print!("{}. ", turn);
// std::io::stdout().flush().unwrap();

let (white_searched, white_move) = get_best_move_helper(&mut game);
let (white_searched, white_move) = get_best_move_helper(&mut game, &mut history);
// let white_move_name = move_name(&game.board, &game.last_move, &game.player, &white_move);
game.do_move(&white_move);

// print!("{} ", white_move_name);
// std::io::stdout().flush().unwrap();

let (black_searched, black_move) = get_best_move_helper(&mut game);
let (black_searched, black_move) = get_best_move_helper(&mut game, &mut history);
// let black_move_name = move_name(&game.board, &game.last_move, &game.player, &black_move);
game.do_move(&black_move);

Expand Down
45 changes: 45 additions & 0 deletions chusst-gen/src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,51 @@ pub trait Board:

Some(board)
}

fn to_fen(&self) -> String {
let mut fen = String::new();

for rank in (0..8).rev() {
let mut empty = 0;
for file in 0..8 {
let piece = self.at(&pos!(rank, file));
match piece {
Some(Piece { piece, player }) => {
if empty > 0 {
fen.push_str(&empty.to_string());
empty = 0;
}
let piece_char = match (player, piece) {
(Player::White, PieceType::Pawn) => 'P',
(Player::White, PieceType::Knight) => 'N',
(Player::White, PieceType::Bishop) => 'B',
(Player::White, PieceType::Rook) => 'R',
(Player::White, PieceType::Queen) => 'Q',
(Player::White, PieceType::King) => 'K',
(Player::Black, PieceType::Pawn) => 'p',
(Player::Black, PieceType::Knight) => 'n',
(Player::Black, PieceType::Bishop) => 'b',
(Player::Black, PieceType::Rook) => 'r',
(Player::Black, PieceType::Queen) => 'q',
(Player::Black, PieceType::King) => 'k',
};
fen.push(piece_char);
}
None => {
empty += 1;
}
}
}
if empty > 0 {
fen.push_str(&empty.to_string());
}
if rank > 0 {
fen.push('/');
}
}

fen
}
}

fn get_unicode_piece(piece: PieceType, player: Player) -> char {
Expand Down
Loading

0 comments on commit aeca696

Please sign in to comment.