123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #ifndef CHESSY_BITBOARD_H
- #define CHESSY_BITBOARD_H
- #include "BitOperations.h"
- #include <cinttypes>
- #include <type_traits>
- #include <string>
- namespace chessy
- {
- union Index;
- struct Bitboard;
- }
- /*!
- * data structure to index one field on a chess board
- */
- union chessy::Index
- {
- int8_t index;
- Index(void) = default;
- inline constexpr Index(int8_t ind) : index{ ind } {}
- inline Index(const std::string& name) :
- Index{ name[0] - '1', name[1] - 'a' } {}
- inline constexpr Index(int row, int column) :
- index{int8_t(((row & 0x7) << 3) + (column & 0x7))} {}
- inline operator int8_t (void) const { return index; }
- inline int getColumn (void) const { return index & 0x7; }
- inline int getRow (void) const { return (index >> 3) & 0x7; }
- inline std::string getName(void) const
- {
- return { char('a' + getColumn()), char(getRow() + '1') };
- }
- };
- struct chessy::Bitboard
- {
- U64 bits;
- Bitboard (void) = default;
- Bitboard (const Bitboard&) = default;
- ~Bitboard (void) = default;
- inline constexpr Bitboard(U64 bits) : bits{ bits } {}
- inline static Bitboard fromIndex(Index i) { return U64(1) << i; }
- inline void setBit (Index i) { bits |= 1ULL << i.index; }
- inline void unsetBit (Index i) { bits |= ~(1ULL << i.index); }
- inline void setBit (int row, int column)
- { setBit(row * 8 + column); }
- inline void unsetBit (int row, int column)
- { unsetBit(row * 8 + column); }
- static const U64 aColumn = 0x0101010101010101;
- static const U64 hColumn = 0x8080808080808080;
- inline void moveNorth (int dist) { bits <<= (8 * dist); }
- inline void moveSouth (int dist) { bits >>= (8 * dist); }
- inline void moveNorthOne(void) { bits <<= 8; }
- inline void moveSouthOne(void) { bits >>= 8; }
- inline void moveEastOne (void) { bits = (bits >> 1) & ~aColumn; }
- inline void moveWestOne (void) { bits = (bits << 1) & ~hColumn; }
- inline void moveNEOne (void) { bits = (bits << 7) & ~aColumn; }
- inline void moveNWOne (void) { bits = (bits << 9) & ~hColumn; }
- inline void moveSEOne (void) { bits = (bits >> 9) & ~aColumn; }
- inline void moveSWOne (void) { bits = (bits >> 7) & ~hColumn; }
- inline void operator &= (const Bitboard& b) { bits &= b.bits; }
- inline void operator |= (const Bitboard& b) { bits |= b.bits; }
- inline void operator ^= (const Bitboard& b) { bits ^= b.bits; }
- inline Bitboard operator & (const Bitboard& b) const { return bits & b.bits; }
- inline Bitboard operator | (const Bitboard& b) const { return bits | b.bits; }
- inline Bitboard operator ^ (const Bitboard& b) const { return bits ^ b.bits; }
- inline Bitboard operator ~ (void) const { return ~bits; }
- inline bool operator == (const Bitboard& b) const { return bits == b.bits; }
- inline bool operator != (const Bitboard& b) const { return bits != b.bits; }
- inline operator U64(void) const { return bits; }
- inline explicit operator bool(void) const { return bits != 0; }
- inline Bitboard mirror(void) const { return byteswap(bits); }
- };
- static_assert(std::is_pod<chessy::Bitboard>().value,
- "chessy::Bitboard should be a POD structure.");
- static_assert(sizeof(chessy::Bitboard) == sizeof(uint64_t),
- "chessy::Bitboard should be 64 bits in size.");
- #endif // CHESSY_BITBOARD_H
|