Skip to content

Commit 596e177

Browse files
committed
implemented queen legal moves generation with testing
1 parent 509992a commit 596e177

File tree

2 files changed

+583
-0
lines changed

2 files changed

+583
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#pragma once
2+
3+
#include <bitbishop/attacks/queen_attacks.hpp>
4+
#include <bitbishop/bitboard.hpp>
5+
#include <bitbishop/board.hpp>
6+
#include <bitbishop/color.hpp>
7+
#include <bitbishop/move.hpp>
8+
#include <bitbishop/movegen/pins.hpp>
9+
#include <vector>
10+
11+
/**
12+
* @brief Generate all legal queen moves for the side to move.
13+
*
14+
* This function enumerates every queen belonging to the given side and
15+
* generates all legal destination squares, taking into account:
16+
*
17+
* - Sliding attacks along ranks, files, and diagonals
18+
* - Friendly piece blocking
19+
* - Enemy captures
20+
* - Active check constraints via @p check_mask
21+
* - Absolute pins via @p pins
22+
*
23+
* The resulting moves are appended to @p moves. Existing contents are
24+
* preserved.
25+
*
26+
* @param[out] moves
27+
* Vector to which generated queen moves will be appended.
28+
*
29+
* @param[in] board
30+
* Current board position.
31+
*
32+
* @param[in] us
33+
* Side to generate queen moves for.
34+
*
35+
* @param[in] check_mask
36+
* Bitboard restricting legal move destinations when the king is in check.
37+
* Only moves intersecting this mask are generated.
38+
*
39+
* @param[in] pins
40+
* Precomputed pin information. If a queen is absolutely pinned, its legal
41+
* moves are restricted to the corresponding pin ray.
42+
*
43+
* @note
44+
* - This function assumes that @p check_mask and @p pins have already been
45+
* computed for the current position.
46+
* - Promotions, en passant, and castling are not applicable to queen moves.
47+
*/
48+
void generate_queen_legal_moves(std::vector<Move>& moves, const Board& board, Color us, const Bitboard& check_mask,
49+
const PinResult& pins) {
50+
const Bitboard own = board.friendly(us);
51+
const Bitboard enemy = board.enemy(us);
52+
const Bitboard occupied = board.occupied();
53+
Bitboard queens = board.queens(us);
54+
55+
// warning: this loop is destructive on bitboard queens
56+
while (auto from_opt = queens.pop_lsb()) {
57+
Square from = from_opt.value();
58+
59+
Bitboard candidates = queen_attacks(from, occupied);
60+
candidates &= ~own;
61+
candidates &= check_mask;
62+
if (pins.pinned.test(from)) {
63+
candidates &= pins.pin_ray[from.value()];
64+
}
65+
66+
for (Square to : candidates) {
67+
const bool is_capture = enemy.test(to);
68+
moves.emplace_back(from, to, std::nullopt, is_capture, false, false);
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)