|
@@ -0,0 +1,92 @@
|
|
|
+/*!
|
|
|
+ *
|
|
|
+ * arranged like this:
|
|
|
+ *
|
|
|
+ * 63 62 61 60 59 58 57 56
|
|
|
+ * 55 54 ...
|
|
|
+ *
|
|
|
+ */
|
|
|
+pub type Bitboard = u64;
|
|
|
+
|
|
|
+
|
|
|
+pub struct BitboardIterator { pub board: Bitboard }
|
|
|
+
|
|
|
+impl Iterator for BitboardIterator {
|
|
|
+ type Item = Bitboard;
|
|
|
+
|
|
|
+ fn next(&mut self) -> Option<Bitboard> {
|
|
|
+ if self.board != 0 {
|
|
|
+ let lsb = self.board & (0_u64.wrapping_sub(self.board));
|
|
|
+ self.board &= !lsb;
|
|
|
+ //Some(lsb.trailing_zeros())
|
|
|
+ Some(lsb)
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ None
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+pub const A_FILE: Bitboard = 0x_8080_8080_8080_8080;
|
|
|
+pub const H_FILE: Bitboard = 0x_0101_0101_0101_0101;
|
|
|
+
|
|
|
+
|
|
|
+pub fn north_one(b: Bitboard) -> Bitboard {
|
|
|
+ b << 8
|
|
|
+}
|
|
|
+
|
|
|
+pub fn south_one(b: Bitboard) -> Bitboard {
|
|
|
+ b >> 8
|
|
|
+}
|
|
|
+
|
|
|
+pub fn west_one(b: Bitboard) -> Bitboard {
|
|
|
+ (b << 1) & !H_FILE
|
|
|
+}
|
|
|
+
|
|
|
+pub fn east_one(b: Bitboard) -> Bitboard {
|
|
|
+ (b >> 1) & !A_FILE
|
|
|
+}
|
|
|
+
|
|
|
+pub fn northeast_one(b: Bitboard) -> Bitboard {
|
|
|
+ (b << 7) & !A_FILE
|
|
|
+}
|
|
|
+
|
|
|
+pub fn northwest_one(b: Bitboard) -> Bitboard {
|
|
|
+ (b << 9) & !H_FILE
|
|
|
+}
|
|
|
+
|
|
|
+pub fn southwest_one(b: Bitboard) -> Bitboard {
|
|
|
+ (b >> 7) & !H_FILE
|
|
|
+}
|
|
|
+
|
|
|
+pub fn southeast_one(b: Bitboard) -> Bitboard {
|
|
|
+ (b >> 9) & !A_FILE
|
|
|
+}
|
|
|
+
|
|
|
+pub fn bit_at_index(b: Bitboard, index: u32) -> bool {
|
|
|
+ ((b >> index) & 1) != 0
|
|
|
+}
|
|
|
+
|
|
|
+pub fn bit_at(b: Bitboard, i: i32, j: i32) -> bool {
|
|
|
+ ((b >> (7 - i) * 8 + 7 - j) & 1) == 1
|
|
|
+}
|
|
|
+
|
|
|
+pub fn set_bit(b: &mut Bitboard, i: i32, j: i32) {
|
|
|
+ *b |= 1 << ((7 - i) * 8 + 7 - j);
|
|
|
+}
|
|
|
+
|
|
|
+pub fn set_bit_index(b: &mut Bitboard, index: u32) {
|
|
|
+ *b |= 1 << index;
|
|
|
+}
|
|
|
+
|
|
|
+pub fn print_board(b: Bitboard) -> String {
|
|
|
+ (0..8).map(
|
|
|
+ |i| (0..8).map(
|
|
|
+ |j| if bit_at(b, i, j) { "x " }
|
|
|
+ else { ". " }
|
|
|
+ ).collect::<String>() + "\n"
|
|
|
+ ).collect::<String>()
|
|
|
+}
|
|
|
+
|
|
|
+
|