Chess Engine
A Chess Engine project written in C++.
Loading...
Searching...
No Matches
bitboard.hpp
1#pragma once
2
3#include <array>
4#include <cassert>
5#include <cstdint>
6
7#include "navigation.hpp"
8
9struct Bitboard {
10 public:
11 constexpr Bitboard(uint64_t value = zero()) : m_value(value) {};
12
13 constexpr void set(uint64_t value) { m_value = value; }
14 [[nodiscard]] constexpr uint64_t value() const { return m_value; }
15
16 constexpr Bitboard& operator|=(const Bitboard& other) {
17 m_value |= other.m_value;
18 return *this;
19 }
20 constexpr Bitboard& operator&=(const Bitboard& other) {
21 m_value &= other.m_value;
22 return *this;
23 }
24 constexpr Bitboard& operator^=(const Bitboard& other) {
25 m_value ^= other.m_value;
26 return *this;
27 }
28
29 constexpr operator uint64_t() const { return m_value; }
30
31 constexpr bool operator==(const Bitboard& other) const { return m_value == other.m_value; }
32 constexpr bool operator!=(const Bitboard& other) const { return m_value != other.m_value; }
33
34 static constexpr Bitboard zero() { return 0ULL; }
35
36 static constexpr Bitboard squareBB(Square square) { return (1ULL << square); }
37 static constexpr Bitboard destinationBB(Square square, Direction direction) {
38 Square destination = square + direction;
39 return (destination.ok() && Square::distance(square, destination) <= 2) ? squareBB(destination) : zero();
40 };
41
42 template <int8_t D>
43 [[nodiscard]] constexpr Bitboard pushed() const {
44 switch (D) {
45 case Direction::N:
46 case Direction::NN:
47 return m_value << D;
48 case Direction::S:
49 case Direction::SS:
50 return m_value >> -D;
51 case Direction::NE:
52 case Direction::SE:
53 return (m_value & ~fileBB(File::FH)) << D;
54 case Direction::NW:
55 case Direction::SW:
56 return (m_value & ~fileBB(File::FA)) >> -D;
57 default:
58 return zero();
59 }
60 }
61
62 static constexpr Bitboard rankBB(Rank rank) {
63 static constexpr auto table = []() constexpr {
64 std::array<Bitboard, Rank::number()> t{};
65
66 constexpr Bitboard RANK_A = 0xFFULL;
67
68 for (auto rank : Rank::all()) {
69 t[rank] = (RANK_A << (rank * File::number()));
70 }
71 return t;
72 }();
73
74 return table[rank];
75 }
76
77 static constexpr Bitboard fileBB(File file) {
78 static constexpr auto table = []() constexpr {
79 std::array<Bitboard, File::NB> t{};
80
81 constexpr Bitboard FILE_A = 0x0101010101010101ULL;
82
83 for (auto file : File::all()) {
84 t[file] = (FILE_A << file);
85 }
86 return t;
87 }();
88
89 return table[file];
90 }
91
92 static constexpr Bitboard diagBB(Square square) {
93 static constexpr auto table = []() constexpr {
94 std::array<Bitboard, Square::NB> t{};
95 for (auto square : Square::all()) {
96 Bitboard diag = zero();
97 File squareFile = square.file();
98 Rank squareRank = square.rank();
99
100 for (auto rank : Rank::all()) {
101 for (auto file : File::all()) {
102 Square possibleSquare = Square(file, rank);
103 if (file - rank == squareFile - squareRank) {
104 diag = diag | squareBB(possibleSquare);
105 }
106 }
107 }
108 t[square] = diag;
109 }
110 return t;
111 }();
112 return table[square];
113 }
114
115 static constexpr Bitboard antiDiagBB(Square square) {
116 static constexpr auto table = []() constexpr {
117 std::array<Bitboard, Square::NB> t{};
118 for (auto square : Square::all()) {
119 Bitboard antiDiag = zero();
120 File squareFile = square.file();
121 Rank squareRank = square.rank();
122
123 for (auto rank : Rank::all()) {
124 for (auto file : File::all()) {
125 Square possibleSquare = Square(file, rank);
126 if (file + rank == squareFile + squareRank) {
127 antiDiag = antiDiag | squareBB(possibleSquare);
128 }
129 }
130 }
131 t[square] = antiDiag;
132 }
133 return t;
134 }();
135 return table[square];
136 }
137
138 private:
139 uint64_t m_value;
140};
Definition navigation.hpp:176
Definition navigation.hpp:42
Definition navigation.hpp:79
Definition navigation.hpp:118