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
use crate::constants::*;
use crate::square::*;
pub type Bitboard = u64;
pub trait BitboardTrait {
fn pretty_print_string(&self) -> String;
fn pop_bitboard(&mut self) -> (Bitboard, bool);
fn pop_square(&mut self) -> (Square, bool);
fn variation_count(self) -> usize;
}
impl BitboardTrait for Bitboard {
fn pretty_print_string(&self) -> String {
let mut bb = *self;
let mut buff = "".to_string();
let mut bits = 0;
loop {
if bits % 8 == 0 {
buff += &format!("{}", NUM_FILES - bits / 8).to_string();
}
if bb & (1 << 63) != 0 {
buff += "1"
} else {
buff += "."
}
if bits % 8 == 7 {
buff += "*\n"
}
bb = bb << 1;
bits = bits + 1;
if bits == 64 {
break;
}
}
format! {"bitboard {:#016x}\n**********\n{}*abcdefgh*\n", &self, buff}
}
fn pop_bitboard(&mut self) -> (Bitboard, bool) {
if *self == 0 {
return (0, false);
}
let bb = 1 << (self.trailing_zeros() as usize);
*self &= !bb;
return (bb, true);
}
fn pop_square(&mut self) -> (Square, bool) {
let (bb, ok) = self.pop_bitboard();
if ok {
let tzs = bb.trailing_zeros() as usize;
(
rank_file(tzs / NUM_FILES, LAST_FILE - (tzs % NUM_FILES)),
true,
)
} else {
(0, false)
}
}
fn variation_count(self) -> usize {
1 << self.count_ones()
}
}