bitboard.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*!
  2. *
  3. * arranged like this:
  4. *
  5. * 63 62 61 60 59 58 57 56
  6. * 55 54 ...
  7. *
  8. */
  9. pub type Bitboard = u64;
  10. pub type Square = u8;
  11. pub const A_FILE: Bitboard = 0x_8080_8080_8080_8080;
  12. pub const B_FILE: Bitboard = A_FILE >> 1;
  13. pub const C_FILE: Bitboard = A_FILE >> 2;
  14. pub const D_FILE: Bitboard = A_FILE >> 3;
  15. pub const E_FILE: Bitboard = A_FILE >> 4;
  16. pub const F_FILE: Bitboard = A_FILE >> 5;
  17. pub const G_FILE: Bitboard = A_FILE >> 6;
  18. pub const H_FILE: Bitboard = A_FILE >> 7;
  19. pub const ROW_1: Bitboard = 0x_0000_0000_0000_00FF;
  20. pub const ROW_2: Bitboard = 0x_0000_0000_0000_FF00;
  21. pub const ROW_3: Bitboard = 0x_0000_0000_00FF_0000;
  22. pub const ROW_4: Bitboard = 0x_0000_0000_FF00_0000;
  23. pub const ROW_5: Bitboard = 0x_0000_00FF_0000_0000;
  24. pub const ROW_6: Bitboard = 0x_0000_FF00_0000_0000;
  25. pub const ROW_7: Bitboard = 0x_00FF_0000_0000_0000;
  26. pub const ROW_8: Bitboard = 0x_FF00_0000_0000_0000;
  27. pub fn north_one(b: Bitboard) -> Bitboard {
  28. b << 8
  29. }
  30. pub fn south_one(b: Bitboard) -> Bitboard {
  31. b >> 8
  32. }
  33. pub fn west_one(b: Bitboard) -> Bitboard {
  34. (b << 1) & !H_FILE
  35. }
  36. pub fn east_one(b: Bitboard) -> Bitboard {
  37. (b >> 1) & !A_FILE
  38. }
  39. pub fn northeast_one(b: Bitboard) -> Bitboard {
  40. (b << 7) & !A_FILE
  41. }
  42. pub fn northwest_one(b: Bitboard) -> Bitboard {
  43. (b << 9) & !H_FILE
  44. }
  45. pub fn southwest_one(b: Bitboard) -> Bitboard {
  46. (b >> 7) & !H_FILE
  47. }
  48. pub fn southeast_one(b: Bitboard) -> Bitboard {
  49. (b >> 9) & !A_FILE
  50. }
  51. pub fn bit_at_index(b: Bitboard, index: u32) -> bool {
  52. ((b >> index) & 1) != 0
  53. }
  54. pub fn bit_at(b: Bitboard, i: i32, j: i32) -> bool {
  55. ((b >> (7 - i) * 8 + 7 - j) & 1) == 1
  56. }
  57. pub fn set_bit(b: &mut Bitboard, i: i32, j: i32) {
  58. *b |= 1_u64 << ((7 - i) * 8 + 7 - j);
  59. }
  60. pub fn set_bit_index(b: &mut Bitboard, index: u32) {
  61. *b |= 1_u64 << index;
  62. }
  63. pub fn from_square(s: Square) -> Bitboard {
  64. return 1_u64 << s;
  65. }
  66. pub fn square(b: Bitboard) -> Square {
  67. b.trailing_zeros() as Square
  68. }
  69. pub fn square_from_indices(file: u8, row: u8) -> Square {
  70. (7 - row) * 8 + 7 - file
  71. }
  72. pub fn from_indices(file: u8, row: u8) -> Bitboard {
  73. 1_u64 << square_from_indices(file, row)
  74. }
  75. pub fn indices_from_square(sq: Square) -> (u8, u8) {
  76. (7 - sq % 8, 7 - sq / 8)
  77. }
  78. pub fn print_board(b: Bitboard) -> String {
  79. (0..8).map(
  80. |i| (0..8).map(
  81. |j| if bit_at(b, i, j) { "x " }
  82. else { ". " }
  83. ).collect::<String>() + "\n"
  84. ).collect::<String>()
  85. }