BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Patterns.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
20
21#pragma once
22
28
38
39using board::Bitboard;
40using pieces::Color;
41
44
46template <Color Side>
47[[nodiscard, gnu::const]] constexpr auto pawn_pushes(Bitboard starting) noexcept -> Bitboard;
48
50template <Color Side>
51[[nodiscard, gnu::const]] constexpr auto pawn_double_pushes(Bitboard starting) noexcept -> Bitboard;
52
56template <Color Side>
57[[nodiscard, gnu::const]] constexpr auto pawn_attacks(Bitboard starting) noexcept -> Bitboard;
58
60[[nodiscard, gnu::const]] constexpr auto knight(Bitboard starting) noexcept -> Bitboard;
61
63[[nodiscard, gnu::const]] constexpr auto bishop(Bitboard starting) noexcept -> Bitboard;
64
66[[nodiscard, gnu::const]] constexpr auto rook(Bitboard starting) noexcept -> Bitboard;
67
69[[nodiscard, gnu::const]] constexpr auto queen(Bitboard starting) noexcept -> Bitboard;
70
76[[nodiscard, gnu::const]] constexpr auto king(Bitboard starting) noexcept -> Bitboard;
77
79
80/*
81 ___ ,--,
82 ,---, ,--.'|_ ,--, ,--.'|
83 ,---.'| | | :,' ,--.'| | | :
84 | | : : : ' : | |, : : ' .--.--.
85 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
86 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
87 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
88. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
89' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
90| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
91| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
92 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
93 `----' `----' `--`---' ---`-' `--" `--" `--"
94
95 */
96
97template <Color Side>
98constexpr auto pawn_pushes(const Bitboard starting) noexcept
99 -> Bitboard
100{
101 return board::shifts::pawn_forward<Side>(starting);
102}
103
104template <Color Side>
105constexpr auto pawn_double_pushes(const Bitboard starting) noexcept
106 -> Bitboard
107{
108 namespace rank_masks = board::masks::ranks;
109
110 if constexpr (Side == Color::White) {
111 return (starting << 16uz) // north 2 ranks
112 & rank_masks::FOUR;
113 } else {
114 return (starting >> 16uz) // south 2 ranks
115 & rank_masks::FIVE;
116 }
117}
118
119template <Color Side>
120constexpr auto pawn_attacks(const Bitboard starting) noexcept
121 -> Bitboard
122{
123 namespace shifts = board::shifts;
124
125 return shifts::pawn_capture_east<Side>(starting) | shifts::pawn_capture_west<Side>(starting);
126}
127
128constexpr auto knight(const Bitboard starting) noexcept
129 -> Bitboard
130{
131 namespace file_masks = board::masks::files;
132
133 constexpr auto notAFile = file_masks::A.inverse();
134 constexpr auto notHFile = file_masks::H.inverse();
135 constexpr auto notABFile = (file_masks::A | file_masks::B).inverse();
136 constexpr auto notGHFile = (file_masks::G | file_masks::H).inverse();
137
139
140 moves |= (starting & notHFile) << 17uz; // noNoEa
141 moves |= (starting & notGHFile) << 10uz; // noEaEa
142 moves |= (starting & notGHFile) >> 6uz; // soEaEa
143 moves |= (starting & notHFile) >> 15uz; // soSoEa
144 moves |= (starting & notAFile) << 15uz; // noNoWe
145 moves |= (starting & notABFile) << 6uz; // noWeWe
146 moves |= (starting & notABFile) >> 10uz; // soWeWe
147 moves |= (starting & notAFile) >> 17uz; // soSoWe
148
149 return moves;
150}
151
152constexpr auto bishop(const Bitboard starting) noexcept
153 -> Bitboard
154{
155 const auto diags = board::fills::diagonal(starting);
156 const auto antiDiags = board::fills::antidiagonal(starting);
157
158 return (diags | antiDiags) & starting.inverse();
159}
160
161constexpr auto rook(const Bitboard starting) noexcept
162 -> Bitboard
163{
164 const auto ranks = board::fills::rank(starting);
165 const auto files = board::fills::file(starting);
166
167 return (ranks | files) & starting.inverse();
168}
169
170constexpr auto queen(const Bitboard starting) noexcept
171 -> Bitboard
172{
173 const auto ranks = board::fills::rank(starting);
174 const auto files = board::fills::file(starting);
175 const auto diags = board::fills::diagonal(starting);
176 const auto antiDiags = board::fills::antidiagonal(starting);
177
178 return (ranks | files | diags | antiDiags) & starting.inverse();
179}
180
181constexpr auto king(Bitboard starting) noexcept
182 -> Bitboard
183{
184 namespace shifts = board::shifts;
185
186 auto moves = shifts::east(starting) | shifts::west(starting);
187
188 starting |= moves;
189
190 moves |= shifts::north(starting) | shifts::south(starting);
191
192 return moves;
193}
194
195} // namespace chess::moves::patterns
constexpr auto file(Bitboard starting) noexcept -> Bitboard
Definition Fills.hpp:213
constexpr auto antidiagonal(Bitboard starting) noexcept -> Bitboard
Definition Fills.hpp:228
constexpr auto pawn_forward(Bitboard board) noexcept -> Bitboard
Definition Shifts.hpp:162
constexpr auto diagonal(Bitboard starting) noexcept -> Bitboard
Definition Fills.hpp:223
constexpr auto rank(Bitboard starting) noexcept -> Bitboard
Definition Fills.hpp:218
constexpr auto pawn_double_pushes(Bitboard starting) noexcept -> Bitboard
Definition Patterns.hpp:105
constexpr auto queen(Bitboard starting) noexcept -> Bitboard
Definition Patterns.hpp:170
constexpr auto pawn_attacks(Bitboard starting) noexcept -> Bitboard
Definition Patterns.hpp:120
constexpr auto knight(Bitboard starting) noexcept -> Bitboard
Definition Patterns.hpp:128
constexpr auto king(Bitboard starting) noexcept -> Bitboard
Definition Patterns.hpp:181
constexpr auto rook(Bitboard starting) noexcept -> Bitboard
Definition Patterns.hpp:161
constexpr auto bishop(Bitboard starting) noexcept -> Bitboard
Definition Patterns.hpp:152
constexpr auto pawn_pushes(Bitboard starting) noexcept -> Bitboard
Definition Patterns.hpp:98
constexpr auto inverse() const noexcept -> Bitboard
Definition Bitboard.hpp:357