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
use std::prelude::v1::*;
use common::*;
use square::*;
use board;
pub type Bitboard = u64;
pub trait BitboardExt {
fn from_square(sq: Square) -> Bitboard;
fn count(self) -> u32;
fn scan(self) -> u32;
fn shift(self, s: Shift) -> Bitboard;
fn get(self, sq: Square) -> bool;
fn set(&mut self, sq: Square);
fn toggle(&mut self, sq: Square);
fn reset(&mut self, sq: Square);
fn debug(&self);
fn to_debug_string(&self) -> String;
}
impl BitboardExt for Bitboard {
fn from_square(sq: Square) -> Bitboard {
1 << sq
}
#[inline]
fn count(self) -> u32 {
self.count_ones()
}
#[inline]
fn scan(self) -> u32 {
self.trailing_zeros()
}
#[inline]
fn shift(self, s: Shift) -> Bitboard {
if s > 0 {
self << s
} else {
self >> -s
}
}
#[inline]
fn get(self, sq: Square) -> bool {
(self & Bitboard::from_square(sq)) > 0
}
#[inline]
fn set(&mut self, sq: Square) {
*self |= Bitboard::from_square(sq)
}
#[inline]
fn toggle(&mut self, sq: Square) {
*self ^= Bitboard::from_square(sq)
}
#[inline]
fn reset(&mut self, sq: Square) {
*self &= !Bitboard::from_square(sq)
}
fn debug(&self) {
println!("{}", self.to_debug_string());
}
fn to_debug_string(&self) -> String {
let mut out = String::new();
out.push_str(&format!(" Bitboard (0x{:016X})\n", *self));
let squares = (0..64).map(|i| {
if self.get(i) {
bold_green("1")
} else {
"0".to_string()
}
}).collect();
out.push_str(&board::draw_compact_with_coordinates(squares));
out
}
}
pub fn upfill(mut pieces: Bitboard) -> Bitboard {
pieces |= pieces << 8;
pieces |= pieces << 16;
pieces |= pieces << 32;
pieces
}
pub fn downfill(mut pieces: Bitboard) -> Bitboard {
pieces |= pieces >> 8;
pieces |= pieces >> 16;
pieces |= pieces >> 32;
pieces
}
pub fn filefill(pieces: Bitboard) -> Bitboard {
upfill(pieces) | downfill(pieces)
}
pub trait BitboardIterator {
type Item;
fn next(&mut self) -> Option<Self::Item>;
}
impl BitboardIterator for Bitboard {
type Item = Square;
fn next(&mut self) -> Option<Square> {
if *self > 0 {
let sq = self.scan() as Square;
self.reset(sq);
Some(sq)
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_shift() {
let bb1: Bitboard = 0b0011000000000000000000000000000000110000000000000000000000000000;
let bb2: Bitboard = 0b1100000000000000000000000000000011000000000000000000000000000011;
let bb3: Bitboard = 0b0000000000000000000000000000001100000000000000000000000000001100;
assert_eq!(bb2.shift(0), bb2, "{:b} == {:b}", bb2, bb2);
assert_eq!(bb2 << 2, bb3, "{:b} == {:b}", bb2, bb3);
assert_eq!(bb2.shift(2), bb3, "{:b} == {:b}", bb2, bb3);
assert_eq!(bb2 >> 2, bb1, "{:b} == {:b}", bb2, bb1);
assert_eq!(bb2.shift(-2), bb1, "{:b} == {:b}", bb2, bb1);
}
#[test]
fn test_iterator() {
let mut bb: Bitboard = 0;
bb.set(A2);
bb.set(B2);
bb.set(C2);
bb.set(D2);
assert_eq!(bb.next(), Some(A2));
assert_eq!(bb.next(), Some(B2));
assert_eq!(bb.next(), Some(C2));
assert_eq!(bb.next(), Some(D2));
assert_eq!(bb.next(), None);
}
}