-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.rs
69 lines (59 loc) · 2.3 KB
/
mod.rs
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
use std::fs::File;
use std::io::{BufWriter, Write};
use crate::random_number_generator::generate_random_u64;
// https://www.chessprogramming.org/Zobrist_Hashing
const PIECES: [&str; 6] = ["pawn", "rook", "knight", "bishop", "king", "queen"];
const SQUARES: usize = 64;
/// Generates three tables of random u64s for Zobrist hashing and writes them to a file
/// that is included in the project's primary module.
pub fn write_zobrist_tables(out: &mut BufWriter<File>) -> std::io::Result<()> {
// Generate ZOBRIST_PIECES_TABLE
let mut zobrist_table = [[[0u64; 2]; SQUARES]; PIECES.len()];
for piece in 0..PIECES.len() {
for square in 0..SQUARES {
for color in 0..2 {
zobrist_table[piece][square][color] = generate_random_u64();
}
}
}
// Generate ZOBRIST_CASTLING_RIGHTS_TABLE
let mut zobrist_castling_rights = [0u64; 16];
for i in 0..16 {
zobrist_castling_rights[i] = generate_random_u64();
}
// Generate ZOBRIST_EN_PASSANT_TABLE
let mut zobrist_en_passant = [0u64; SQUARES];
for i in 0..SQUARES {
zobrist_en_passant[i] = generate_random_u64();
}
// Write the generated values into a format that can be used in a Rust module
writeln!(out, "#[rustfmt::skip]")?;
writeln!(out, "pub const ZOBRIST_PIECES_TABLE: [[[u64; 2]; 64]; 6] = [")?;
for piece_index in 0..PIECES.len() {
writeln!(out, " [ // {}", PIECES[piece_index])?;
for square_index in 0..SQUARES {
writeln!(
out,
" [{}, {}], // Square {}",
zobrist_table[piece_index][square_index][0],
zobrist_table[piece_index][square_index][1],
square_index
)?;
}
writeln!(out, " ],")?;
}
writeln!(out, "];")?;
writeln!(out, "\n#[rustfmt::skip]")?;
writeln!(out, "pub const ZOBRIST_CASTLING_RIGHTS_TABLE: [u64; 16] = [")?;
for rights in zobrist_castling_rights.iter() {
writeln!(out, " {},", rights)?;
}
writeln!(out, "];")?;
writeln!(out, "\n#[rustfmt::skip]")?;
writeln!(out, "pub const ZOBRIST_EN_PASSANT_TABLE: [u64; 64] = [")?;
for ep_square in zobrist_en_passant.iter() {
writeln!(out, " {},", ep_square)?;
}
writeln!(out, "];")?;
Ok(())
}