BitBoard.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifndef CHESSY_BITBOARD_H
  2. #define CHESSY_BITBOARD_H
  3. #include "BitOperations.h"
  4. #include <cinttypes>
  5. #include <type_traits>
  6. #include <string>
  7. namespace chessy
  8. {
  9. union Index;
  10. struct Bitboard;
  11. }
  12. /*!
  13. * data structure to index one field on a chess board
  14. */
  15. union chessy::Index
  16. {
  17. int8_t index;
  18. Index(void) = default;
  19. inline constexpr Index(int8_t ind) : index{ ind } {}
  20. inline Index(const std::string& name) :
  21. Index{ name[0] - '1', name[1] - 'a' } {}
  22. inline constexpr Index(int row, int column) :
  23. index{int8_t(((row & 0x7) << 3) + (column & 0x7))} {}
  24. inline operator int8_t (void) const { return index; }
  25. inline int getColumn (void) const { return index & 0x7; }
  26. inline int getRow (void) const { return (index >> 3) & 0x7; }
  27. inline std::string getName(void) const
  28. {
  29. return { char('a' + getColumn()), char(getRow() + '1') };
  30. }
  31. };
  32. struct chessy::Bitboard
  33. {
  34. U64 bits;
  35. Bitboard (void) = default;
  36. Bitboard (const Bitboard&) = default;
  37. ~Bitboard (void) = default;
  38. inline constexpr Bitboard(U64 bits) : bits{ bits } {}
  39. inline static Bitboard fromIndex(Index i) { return U64(1) << i; }
  40. inline void setBit (Index i) { bits |= U64(1) << i.index; }
  41. inline void unsetBit (Index i) { bits |= ~(U64(1) << i.index); }
  42. inline void setBit (int row, int column)
  43. { setBit(row * 8 + column); }
  44. inline void unsetBit (int row, int column)
  45. { unsetBit(row * 8 + column); }
  46. static const U64 aColumn = 0x0101010101010101ULL;
  47. static const U64 hColumn = 0x8080808080808080ULL;
  48. inline void moveNorth (int dist) { bits <<= (8 * dist); }
  49. inline Bitboard north (int dist) { return bits << (8 * dist); }
  50. inline void moveSouth (int dist) { bits >>= (8 * dist); }
  51. inline Bitboard south (int dist) { return bits >> (8 * dist); }
  52. inline void moveNorthOne(void) { bits <<= 8; }
  53. inline Bitboard northOne (void) { return bits << 8; }
  54. inline void moveSouthOne(void) { bits >>= 8; }
  55. inline Bitboard southOne (void) { return bits >> 8; }
  56. inline void moveWestOne (void) { bits = (bits & ~aColumn) >> 1; }
  57. inline Bitboard westOne (void) { return (bits & ~aColumn) >> 1; }
  58. inline void moveEastOne (void) { bits = (bits & ~hColumn) << 1; }
  59. inline Bitboard eastOne (void) { return (bits & ~hColumn) << 1; }
  60. inline void moveNWOne (void) { bits = (bits << 7) & ~hColumn; }
  61. inline Bitboard nwOne (void) { return (bits << 7) & ~hColumn; }
  62. inline void moveNEOne (void) { bits = (bits << 9) & ~aColumn; }
  63. inline Bitboard neOne (void) { return (bits << 9) & ~aColumn; }
  64. inline void moveSWOne (void) { bits = (bits >> 9) & ~hColumn; }
  65. inline Bitboard swOne (void) { return (bits >> 9) & ~hColumn; }
  66. inline void moveSEOne (void) { bits = (bits >> 7) & ~aColumn; }
  67. inline Bitboard seOne (void) { return (bits >> 7) & ~aColumn; }
  68. inline void operator &= (const Bitboard& b) { bits &= b.bits; }
  69. inline void operator |= (const Bitboard& b) { bits |= b.bits; }
  70. inline void operator ^= (const Bitboard& b) { bits ^= b.bits; }
  71. inline Bitboard operator & (const Bitboard& b) const { return bits & b.bits; }
  72. inline Bitboard operator | (const Bitboard& b) const { return bits | b.bits; }
  73. inline Bitboard operator ^ (const Bitboard& b) const { return bits ^ b.bits; }
  74. inline Bitboard operator ~ (void) const { return ~bits; }
  75. inline bool operator == (const Bitboard& b) const { return bits == b.bits; }
  76. inline bool operator != (const Bitboard& b) const { return bits != b.bits; }
  77. inline operator U64(void) const { return bits; }
  78. inline explicit operator bool(void) const { return bits != 0; }
  79. inline Bitboard mirror(void) const { return byteswap(bits); }
  80. inline Index getLeastSignificantBit (void) const { return trailingZeroes(bits); }
  81. };
  82. static_assert(std::is_pod<chessy::Bitboard>::value,
  83. "chessy::Bitboard should be a POD structure.");
  84. static_assert(sizeof(chessy::Bitboard) == sizeof(uint64_t),
  85. "chessy::Bitboard should be 64 bits in size.");
  86. #endif // CHESSY_BITBOARD_H