123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- /*!
- *
- * arranged like this:
- *
- * 63 62 61 60 59 58 57 56
- * 55 54 ...
- *
- */
- pub type Bitboard = u64;
- pub type Square = u8;
- pub const A_FILE: Bitboard = 0x_8080_8080_8080_8080;
- pub const B_FILE: Bitboard = A_FILE >> 1;
- pub const C_FILE: Bitboard = A_FILE >> 2;
- pub const D_FILE: Bitboard = A_FILE >> 3;
- pub const E_FILE: Bitboard = A_FILE >> 4;
- pub const F_FILE: Bitboard = A_FILE >> 5;
- pub const G_FILE: Bitboard = A_FILE >> 6;
- pub const H_FILE: Bitboard = A_FILE >> 7;
- pub const ROW_1: Bitboard = 0x_0000_0000_0000_00FF;
- pub const ROW_2: Bitboard = 0x_0000_0000_0000_FF00;
- pub const ROW_3: Bitboard = 0x_0000_0000_00FF_0000;
- pub const ROW_4: Bitboard = 0x_0000_0000_FF00_0000;
- pub const ROW_5: Bitboard = 0x_0000_00FF_0000_0000;
- pub const ROW_6: Bitboard = 0x_0000_FF00_0000_0000;
- pub const ROW_7: Bitboard = 0x_00FF_0000_0000_0000;
- pub const ROW_8: Bitboard = 0x_FF00_0000_0000_0000;
- 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_u64 << ((7 - i) * 8 + 7 - j);
- }
- pub fn set_bit_index(b: &mut Bitboard, index: u32) {
- *b |= 1_u64 << index;
- }
- pub fn from_square(s: Square) -> Bitboard {
- return 1_u64 << s;
- }
- pub fn square(b: Bitboard) -> Square {
- b.trailing_zeros() as Square
- }
- pub fn square_from_indices(file: u8, row: u8) -> Square {
- (7 - row) * 8 + 7 - file
- }
- pub fn from_indices(file: u8, row: u8) -> Bitboard {
- 1_u64 << square_from_indices(file, row)
- }
- pub fn indices_from_square(sq: Square) -> (u8, u8) {
- (7 - sq % 8, 7 - sq / 8)
- }
- 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>()
- }
|