chessframe 0.4.2

A Rust library for working with chess positions, generating psudo-legal moves, and interacting with the UCI protocol.
Documentation
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use std::{
    fs::File,
    io::{self, Write},
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc,
    },
    thread,
};

use rand_chacha::{
    rand_core::{RngCore, SeedableRng},
    ChaCha8Rng,
};

use chessframe::{
    bitboard::{BitBoard, EMPTY},
    square::{Square, SQUARES},
};

#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
enum MagicPiece {
    Bishop,
    Rook,
}

#[derive(Debug, Clone, Copy, Default)]
struct Magic {
    pub mask: BitBoard,
    pub magic: u64,
    pub shift: u8,
    pub offset: u32,
}

fn rook_directions() -> [fn(Square) -> Option<Square>; 4] {
    fn north(square: Square) -> Option<Square> {
        square.up()
    }
    fn east(square: Square) -> Option<Square> {
        square.right()
    }
    fn south(square: Square) -> Option<Square> {
        square.down()
    }
    fn west(square: Square) -> Option<Square> {
        square.left()
    }
    [north, east, south, west]
}

fn bishop_directions() -> [fn(Square) -> Option<Square>; 4] {
    fn north_east(square: Square) -> Option<Square> {
        square.up().and_then(|square| square.right())
    }
    fn south_east(square: Square) -> Option<Square> {
        square.down().and_then(|square| square.right())
    }
    fn south_west(square: Square) -> Option<Square> {
        square.down().and_then(|square| square.left())
    }
    fn north_west(square: Square) -> Option<Square> {
        square.up().and_then(|square| square.left())
    }
    [north_east, south_east, south_west, north_west]
}

fn flatten_data(data: ([Magic; 64], [Vec<BitBoard>; 64])) -> ([Magic; 64], Vec<BitBoard>) {
    let (magic_array, moves_array) = data;

    let mut offset = 0;

    let updated_magic = magic_array
        .iter()
        .zip(moves_array.iter())
        .map(|(magic, moves)| {
            let mut new_magic = *magic;
            new_magic.offset = offset;
            offset += moves.len() as u32;
            new_magic
        })
        .collect::<Vec<Magic>>()
        .try_into()
        .unwrap();

    let flattened_moves = moves_array
        .iter()
        .flat_map(|moves| moves.clone())
        .collect::<Vec<BitBoard>>();

    (updated_magic, flattened_moves)
}

fn generate_magics_and_moves(
    rng: &mut ChaCha8Rng,
    piece: MagicPiece,
    search_squares: &Option<Vec<Square>>,
) -> Result<[Option<(Magic, Vec<BitBoard>)>; 64], &'static str> {
    let mut magics_and_moves = [const { None }; 64];

    if let Some(search_squares) = search_squares {
        for square in search_squares {
            magics_and_moves[square.to_index()] =
                if let Ok(magic_moves) = find_magic(rng, piece, *square) {
                    Some((magic_moves.0, magic_moves.1))
                } else {
                    None
                }
        }
    } else {
        for square in SQUARES {
            magics_and_moves[square.to_index()] =
                if let Ok(magic_moves) = find_magic(rng, piece, square) {
                    Some((magic_moves.0, magic_moves.1))
                } else {
                    None
                }
        }
    }

    Ok(magics_and_moves)
}

#[rustfmt::skip]
fn find_magic(rng: &mut ChaCha8Rng, piece: MagicPiece, square: Square) -> Result<(Magic, Vec<BitBoard>), &'static str> {
    let mask = match piece {
        MagicPiece::Bishop => generate_bishop_moves(square, BitBoard(0)) & BitBoard(0x007e7e7e7e7e7e00),
        MagicPiece::Rook => generate_rook_mask(square),
    };

    // Try magic numbers until we find one that works
    for _ in 0..1_000 {
        let magic_number = generate_magic_candidate(rng);
        let magic = Magic {
            mask,
            magic: magic_number,
            shift: 64 - mask.0.count_ones() as u8,
            offset: 0,
        };

        if let Ok(table) = try_make_table(&piece, square, magic) {
            return Ok((magic, table));
        }
    }

    Err("Failed to find magic!")
}

fn try_make_table(piece: &MagicPiece, square: Square, magic: Magic) -> Result<Vec<BitBoard>, &str> {
    let mut table: Vec<BitBoard> =
        vec![BitBoard::default(); 1 << magic.mask.0.count_ones() as usize];

    for blockers in subsets(magic.mask) {
        let moves = match piece {
            MagicPiece::Bishop => generate_bishop_moves(square, blockers),
            MagicPiece::Rook => generate_rook_moves(square, blockers),
        };
        let table_entry = &mut table[magic_index(magic, blockers)];

        if *table_entry == EMPTY {
            *table_entry = moves;
        } else if *table_entry != moves {
            return Err("Hash collision!");
        }
    }

    Ok(table)
}

fn generate_magic_candidate(rng: &mut ChaCha8Rng) -> u64 {
    rng.next_u64() & rng.next_u64() & rng.next_u64()
}

fn magic_index(magic: Magic, blockers: BitBoard) -> usize {
    let blockers = blockers & magic.mask;
    let hash = blockers.0.wrapping_mul(magic.magic);
    let index = (hash >> magic.shift) as usize;
    magic.offset as usize + index
}

fn generate_bishop_moves(square: Square, blockers: BitBoard) -> BitBoard {
    let mut moves = BitBoard(0);

    for mv in bishop_directions() {
        let mut next = mv(square);
        while let Some(current) = next {
            moves |= BitBoard::from_square(current);
            next = mv(current);

            if BitBoard::from_square(current) & blockers != EMPTY {
                break;
            }
        }
    }

    moves
}

fn generate_rook_mask(square: Square) -> BitBoard {
    let mut mask = BitBoard(0);

    let (file, rank) = (square.to_index() % 8, square.to_index() / 8);

    let vertical_mask = BitBoard(0x0001010101010100);
    let horizontal_mask = BitBoard(0x000000000000007E);

    mask |= vertical_mask << file;
    mask |= horizontal_mask << (rank * 8);

    mask.clear_bit(square);

    mask
}

fn generate_rook_moves(square: Square, blockers: BitBoard) -> BitBoard {
    let mut moves = BitBoard(0);

    for mv in rook_directions() {
        let mut next = mv(square);
        while let Some(current) = next {
            moves |= BitBoard::from_square(current);
            next = mv(current);

            if BitBoard::from_square(current) & blockers != EMPTY {
                break;
            }
        }
    }

    moves
}

fn subsets(mask: BitBoard) -> Vec<BitBoard> {
    let mut subsets = Vec::new();

    let mut subset: BitBoard = BitBoard(0);
    loop {
        subsets.push(subset);

        subset = BitBoard(subset.0.wrapping_sub(mask.0)) & mask;
        if subset == EMPTY {
            break;
        }
    }

    subsets
}

fn extract_moves_and_magics(
    moves_and_magics: &[Option<(Magic, Vec<BitBoard>)>; 64],
) -> ([Magic; 64], [Vec<BitBoard>; 64]) {
    let magics = moves_and_magics
        .iter()
        .map(|x| x.as_ref().unwrap().0)
        .collect::<Vec<Magic>>()
        .try_into()
        .unwrap();
    let moves = moves_and_magics
        .iter()
        .map(|x| x.as_ref().unwrap().1.clone())
        .collect::<Vec<Vec<BitBoard>>>()
        .try_into()
        .unwrap();
    (magics, moves)
}

fn write_bishop_moves(f: &mut File, bishop_magics_and_moves: ([Magic; 64], Vec<BitBoard>)) {
    writeln!(
        f,
        "pub const BISHOP_MAGICS: [Magic; 64] = {:?};",
        bishop_magics_and_moves.0,
    )
    .unwrap();

    writeln!(
        f,
        "pub static BISHOP_MOVES_TABLE: &[BitBoard] = &{:?};",
        bishop_magics_and_moves.1,
    )
    .unwrap();
}

fn write_rook_moves(f: &mut File, rook_magics_and_moves: ([Magic; 64], Vec<BitBoard>)) {
    writeln!(
        f,
        "pub const ROOK_MAGICS: [Magic; 64] = {:?};",
        rook_magics_and_moves.0,
    )
    .unwrap();

    writeln!(
        f,
        "pub static ROOK_MOVES_TABLE: &[BitBoard] = &{:?};",
        rook_magics_and_moves.1,
    )
    .unwrap();
}

fn main() {
    let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(123456789);

    let mut missing_bishop_squares = Some(Vec::from(SQUARES));
    let mut bishop_moves_and_magics: [Option<(Magic, Vec<BitBoard>)>; 64] = [const { None }; 64];

    let mut missing_rook_squares = Some(Vec::from(SQUARES));
    let mut rook_moves_and_magics: [Option<(Magic, Vec<BitBoard>)>; 64] = [const { None }; 64];

    let stop_flag = Arc::new(AtomicBool::new(false));
    let stop_flag_clone = stop_flag.clone();

    thread::spawn(move || {
        let mut line = String::new();
        while io::stdin().read_line(&mut line).is_ok() {
            if line.trim_end() == "stop" {
                stop_flag_clone.store(true, Ordering::Relaxed);
                break;
            }
            line.clear();
        }
    });

    let mut iterations = 0;

    loop {
        iterations += 1;

        if let Some(missing_squares) = &missing_bishop_squares {
            if missing_squares.is_empty() {
                missing_bishop_squares = None;
            }
        }

        if let Ok(latest_bishop_magics_and_moves) =
            generate_magics_and_moves(&mut rng, MagicPiece::Bishop, &missing_bishop_squares)
        {
            for (i, latest_bishop_magics_and_moves) in latest_bishop_magics_and_moves
                .iter()
                .enumerate()
                .filter(|(_, x)| x.is_some())
            {
                let latest_bishop_magics_and_moves =
                    latest_bishop_magics_and_moves.as_ref().unwrap();
                if let Some(bishop_moves_and_magics) = &mut bishop_moves_and_magics[i] {
                    if latest_bishop_magics_and_moves.1.len() < bishop_moves_and_magics.1.len() {
                        *bishop_moves_and_magics = latest_bishop_magics_and_moves.clone();
                    }
                } else {
                    bishop_moves_and_magics[i] = Some(latest_bishop_magics_and_moves.clone());
                    // println!("Found bishop moves and magics for square {}!", i);
                }
            }
        }

        if let Some(missing_squares) = &missing_rook_squares {
            if missing_squares.is_empty() {
                missing_rook_squares = None;
            }
        }

        if let Ok(latest_rook_magics_and_moves) =
            generate_magics_and_moves(&mut rng, MagicPiece::Rook, &missing_rook_squares)
        {
            for (i, latest_rook_magics_and_moves) in latest_rook_magics_and_moves
                .iter()
                .enumerate()
                .filter(|(_, x)| x.is_some())
            {
                let latest_rook_magics_and_moves = latest_rook_magics_and_moves.as_ref().unwrap();
                if let Some(rook_moves_and_magics) = &mut rook_moves_and_magics[i] {
                    if latest_rook_magics_and_moves.1.len() < rook_moves_and_magics.1.len() {
                        *rook_moves_and_magics = latest_rook_magics_and_moves.clone();
                    }
                } else {
                    rook_moves_and_magics[i] = Some(latest_rook_magics_and_moves.clone());
                    // println!("Found rook moves and magics for square {}!", i);
                }
            }
        }

        let bishop_moves_kb = bishop_moves_and_magics
            .iter()
            .filter_map(|x| x.as_ref())
            .fold(0, |acc, (_, moves)| acc + moves.len() * 8 / 1024);
        let rook_moves_kb = rook_moves_and_magics
            .iter()
            .filter_map(|x| x.as_ref())
            .fold(0, |acc, (_, moves)| acc + moves.len() * 8 / 1024);
        let found_bishop_squares = bishop_moves_and_magics
            .iter()
            .filter(|x| x.is_some())
            .count();
        let found_rook_squares = rook_moves_and_magics.iter().filter(|x| x.is_some()).count();

        print!(
            "\rIteration: {}, Bishop moves size: {} KB, Rook moves size: {} KB | Progress: Bishops {}/64, Rooks {}/64",
            iterations, bishop_moves_kb, rook_moves_kb, found_bishop_squares, found_rook_squares
        );
        io::stdout().flush().unwrap();

        if stop_flag.load(Ordering::Relaxed) {
            break;
        }
    }

    if bishop_moves_and_magics
        .iter()
        .filter(|x| x.is_none())
        .count()
        > 0
    {
        eprintln!("\nFailed to generate bishop moves and magics!");
        return;
    } else if rook_moves_and_magics.iter().filter(|x| x.is_none()).count() > 0 {
        eprintln!("\nFailed to generate rook moves and magics!");
        return;
    }

    let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR should be set");
    let mut file = File::create(format!("{}/magic_tables.rs", out_dir)).unwrap();

    println!("\nWriting magic tables to {}/magic_tables.rs...", out_dir);

    write_bishop_moves(
        &mut file,
        flatten_data(extract_moves_and_magics(&bishop_moves_and_magics)),
    );
    write_rook_moves(
        &mut file,
        flatten_data(extract_moves_and_magics(&rook_moves_and_magics)),
    );

    println!("Done!");
}