123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- /*!
- *
- * arranged like this:
- *
- * 63 62 61 60 59 58 57 56
- * 55 54 ...
- *
- */
- pub type Bitboard = u64;
- pub type Square = u8;
- pub const FILE_A: Bitboard = 0x_8080_8080_8080_8080;
- pub const FILE_B: Bitboard = FILE_A >> 1;
- pub const FILE_C: Bitboard = FILE_A >> 2;
- pub const FILE_D: Bitboard = FILE_A >> 3;
- pub const FILE_E: Bitboard = FILE_A >> 4;
- pub const FILE_F: Bitboard = FILE_A >> 5;
- pub const FILE_G: Bitboard = FILE_A >> 6;
- pub const FILE_H: Bitboard = FILE_A >> 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) & !FILE_H
- }
- pub fn east_one(b: Bitboard) -> Bitboard {
- (b >> 1) & !FILE_A
- }
- pub fn northeast_one(b: Bitboard) -> Bitboard {
- (b << 7) & !FILE_A
- }
- pub fn northwest_one(b: Bitboard) -> Bitboard {
- (b << 9) & !FILE_H
- }
- pub fn southwest_one(b: Bitboard) -> Bitboard {
- (b >> 7) & !FILE_H
- }
- pub fn southeast_one(b: Bitboard) -> Bitboard {
- (b >> 9) & !FILE_A
- }
- 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
- }
- ///
- /// \brief calculates the square index of a square
- ///
- /// \param file the file of the square with 0 being the a file and 7 being the h file
- /// \param row the row of the square with 0 being row 1 and 7 being row 8
- ///
- pub fn square_from_indices(file: u8, row: u8) -> Square {
- (row) * 8 + 7 - file
- }
- pub fn from_indices(file: u8, row: u8) -> Bitboard {
- 1_u64 << square_from_indices(file, row)
- }
- ///
- /// reverse of indices_from_square: (file, row) from encoded square
- ///
- pub fn indices_from_square(sq: Square) -> (u8, u8) {
- (7 - sq % 8, 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>()
- }
|