BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
MoveGen.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
19
20#pragma once
21
22#include <algorithm>
23#include <array>
24#include <beman/inplace_vector/inplace_vector.hpp>
25#include <cassert>
26#include <iterator>
39#include <optional>
40#include <ranges>
41#include <utility>
42
43namespace chess::moves {
44
45using game::Position;
47
50
57template <size_t N>
58using NMoveList = beman::inplace_vector::inplace_vector<Move, N>;
59
67inline constexpr auto MAX_MOVES = 256uz;
68
73
79template <bool CapturesOnly = false>
80void generate(
81 const Position& position,
82 std::output_iterator<Move> auto outputIt);
83
90template <bool CapturesOnly = false>
91[[nodiscard]] auto generate(const Position& position) -> MoveList;
92
100template <bool CapturesOnly = false>
101void generate_for(
102 const Position& position, PieceType piece,
103 std::output_iterator<Move> auto outputIt);
104
112template <bool CapturesOnly = false>
113[[nodiscard]] auto generate_for(
114 const Position& position, PieceType piece) -> MoveList;
115
117[[nodiscard]] auto any_legal_moves(const Position& position) -> bool;
118
124[[nodiscard, gnu::pure]] auto get_potentially_legal_en_passant_target_squares(
125 const Position& position);
126
128
129/*
130 ___ ,--,
131 ,---, ,--.'|_ ,--, ,--.'|
132 ,---.'| | | :,' ,--.'| | | :
133 | | : : : ' : | |, : : ' .--.--.
134 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
135 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
136 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
137. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
138' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
139| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
140| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
141 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
142 `----' `----' `--`---' ---`-' `--" `--" `--"
143
144 */
145
146#ifndef DOXYGEN
147namespace detail {
148
149 using board::Bitboard;
150 using board::Rank;
151 using board::Square;
152 using pieces::Color;
153
155
156 namespace stdv = std::views;
157 namespace rank_masks = board::masks::ranks;
158 namespace shifts = board::shifts;
159
160 inline constexpr auto PROMOTION_MASK = rank_masks::ONE | rank_masks::EIGHT;
161 inline constexpr auto NOT_PROMOTION_MASK = PROMOTION_MASK.inverse();
162
163 inline constexpr std::array possiblePromotedTypes {
164 PieceType::Knight, PieceType::Bishop, PieceType::Rook, PieceType::Queen
165 };
166
167 template <Color Side>
168 [[nodiscard, gnu::pure]] constexpr auto get_pawn_pushes(
169 const Position& position, const Bitboard emptySquares)
170 {
171 using Pushes = beman::inplace_vector::inplace_vector<Move, 32uz>;
172
173 const auto allPushes = pseudo_legal::pawn_pushes<Side>(
174 position.pieces_for<Side>().pawns,
175 emptySquares);
176
177 auto nonPromotingPushes = (allPushes & NOT_PROMOTION_MASK).squares()
178 | stdv::transform([](const Square target) noexcept {
179 return Move {
180 Square {
181 .file = target.file,
182 .rank = prev_pawn_rank<Side>(target.rank) },
183 target, PieceType::Pawn
184 };
185 })
186 | std::ranges::to<Pushes>();
187
188 auto promotingPushes = possiblePromotedTypes
189 | stdv::transform([pushes = (allPushes & PROMOTION_MASK).squares()](
190 const PieceType promotedType) noexcept {
191 return pushes
192 | stdv::transform([promotedType](const Square target) noexcept {
193 return Move {
194 Square {
195 .file = target.file,
196 .rank = prev_pawn_rank<Side>(target.rank) },
197 target, PieceType::Pawn, promotedType
198 };
199 });
200 })
201 | stdv::join
202 | std::ranges::to<Pushes>();
203
204 std::array moveLists {
205 std::move(nonPromotingPushes), std::move(promotingPushes)
206 };
207
208 return stdv::join(std::move(moveLists))
209 | stdv::filter([&position](const Move move) {
210 return position.is_legal(move);
211 });
212 }
213
214 template <Color Side>
215 [[nodiscard, gnu::pure]] constexpr auto get_pawn_double_pushes(
216 const Position& position, const Bitboard allOccupied)
217 {
218 static constexpr auto pawnStartingRank = Side == Color::White ? Rank::Two : Rank::Seven;
219
220 const auto pushes = pseudo_legal::pawn_double_pushes<Side>(
221 position.pieces_for<Side>().pawns,
222 allOccupied);
223
224 return pushes.squares()
225 | stdv::transform([](const Square target) noexcept {
226 return Move {
227 Square {
228 .file = target.file,
229 .rank = pawnStartingRank },
230 target, PieceType::Pawn
231 };
232 })
233 | stdv::filter([&position](const Move move) {
234 return position.is_legal(move);
235 });
236 }
237
238 [[nodiscard, gnu::const]] constexpr auto get_pawn_captures_internal(
239 const Bitboard startingBoard,
240 const Bitboard targetBoard,
241 const std::optional<PieceType> promotedType = std::nullopt)
242 {
243 return stdv::zip(
244 startingBoard.squares(), targetBoard.squares())
245 | stdv::transform([promotedType](const auto& tuple) noexcept {
246 const auto [starting, target] = tuple;
247
248 return Move { starting, target, PieceType::Pawn, promotedType };
249 });
250 }
251
252 template <Color Side>
253 [[nodiscard, gnu::pure]] constexpr auto get_pawn_captures(
254 const Position& position)
255 {
256 // We handle east & west captures separately to make set-wise operations easier.
257 // This way, there is always a 1-1 relationship between a target square and a
258 // starting square.
259
260 const auto ourPawns = position.pieces_for<Side>().pawns;
261 const auto enemyPieces = position.pieces_for<pieces::other_side<Side>()>().occupied;
262
263 const auto eastCaptures = shifts::pawn_capture_east<Side>(ourPawns) & enemyPieces;
264 const auto westCaptures = shifts::pawn_capture_west<Side>(ourPawns) & enemyPieces;
265
266 const auto eastPromotionCaptures = eastCaptures & PROMOTION_MASK;
267 const auto westPromotionCaptures = westCaptures & PROMOTION_MASK;
268
269 const auto eastRegCaptures = eastCaptures & NOT_PROMOTION_MASK;
270 const auto westRegCaptures = westCaptures & NOT_PROMOTION_MASK;
271
272 // starting positions of pawns that can make captures
273 const auto canCapturePromoteEast = shifts::pawn_inv_capture_east<Side>(eastPromotionCaptures);
274 const auto canCapturePromoteWest = shifts::pawn_inv_capture_west<Side>(westPromotionCaptures);
275
276 const auto canRegCaptureEast = shifts::pawn_inv_capture_east<Side>(eastRegCaptures);
277 const auto canRegCaptureWest = shifts::pawn_inv_capture_west<Side>(westRegCaptures);
278
279 // max number of possible pawn captures is 16 * 4 possible promoted types
280 using PawnCaptures = beman::inplace_vector::inplace_vector<Move, 64uz>;
281
282 auto get_non_promoting_captures = [](const Bitboard startingBoard, const Bitboard targetBoard) {
283 return get_pawn_captures_internal(startingBoard, targetBoard)
284 | std::ranges::to<PawnCaptures>();
285 };
286
287 auto get_promotion_captures = [](const Bitboard startingBoard, const Bitboard targetBoard) {
288 return possiblePromotedTypes
289 | stdv::transform([startingBoard, targetBoard](const PieceType type) {
290 return get_pawn_captures_internal(startingBoard, targetBoard, type);
291 })
292 | stdv::join
293 | std::ranges::to<PawnCaptures>();
294 };
295
296 std::array moveLists {
297 get_non_promoting_captures(canRegCaptureEast, eastRegCaptures),
298 get_non_promoting_captures(canRegCaptureWest, westRegCaptures),
299 get_promotion_captures(canCapturePromoteEast, eastPromotionCaptures),
300 get_promotion_captures(canCapturePromoteWest, westPromotionCaptures)
301 };
302
303 return stdv::join(std::move(moveLists))
304 | stdv::filter([&position](const Move move) {
305 return position.is_legal(move);
306 });
307 }
308
309 template <Color Side>
310 [[nodiscard, gnu::pure]] constexpr auto get_en_passant(
311 const Position& position) -> NMoveList<2uz>
312 {
313 // at most 2 captures are possible at a time
314 using EPMoves = NMoveList<2uz>;
315
316 return position.enPassantTargetSquare
317 .transform([&position](const Square targetSquare) {
318 const auto targetSquareBoard = Bitboard::from_square(targetSquare);
319
320 const auto startSquares = shifts::pawn_inv_capture_east<Side>(targetSquareBoard)
321 | shifts::pawn_inv_capture_west<Side>(targetSquareBoard);
322
323 return (position.pieces_for<Side>().pawns & startSquares).squares()
324 | stdv::transform([targetSquare](const Square square) noexcept {
325 return Move {
326 square, targetSquare, PieceType::Pawn
327 };
328 })
329 | stdv::filter([&position](const Move move) {
330 return position.is_legal(move);
331 })
332 | std::ranges::to<EPMoves>();
333 })
334 .value_or(EPMoves { });
335 }
336
337 template <Color Side, bool CapturesOnly>
338 constexpr void add_all_pawn_moves(
339 const Position& position, const Bitboard allOccupied,
340 std::output_iterator<Move> auto outputIt)
341 {
342 if constexpr (not CapturesOnly) {
343 std::ranges::copy(
344 get_pawn_pushes<Side>(position, allOccupied.inverse()),
345 outputIt);
346
347 std::ranges::copy(
348 get_pawn_double_pushes<Side>(position, allOccupied),
349 outputIt);
350 }
351
352 std::ranges::copy(
353 get_pawn_captures<Side>(position),
354 outputIt);
355
356 std::ranges::copy(
357 get_en_passant<Side>(position),
358 outputIt);
359 }
360
361 template <Color Side, bool CapturesOnly>
362 [[nodiscard, gnu::pure]] constexpr auto get_knight_moves(
363 const Position& position)
364 {
365 const auto& ourPieces = position.pieces_for<Side>();
366
367 return ourPieces.knights.subboards()
368 | stdv::transform([ourOccupied = ourPieces.occupied,
369 theirOccupied = position.pieces_for<pieces::other_side<Side>()>().occupied](
370 const Bitboard knightPos) noexcept {
371 auto knightMoves = pseudo_legal::knight(knightPos, ourOccupied);
372
373 if constexpr (CapturesOnly) {
374 knightMoves &= theirOccupied;
375 }
376
377 return knightMoves.squares()
378 | stdv::transform([knightPos](const Square targetSquare) noexcept {
379 return Move {
380 Square::from_index(knightPos.first()),
381 targetSquare, PieceType::Knight
382 };
383 });
384 })
385 | stdv::join
386 | stdv::filter([&position](const Move move) {
387 return position.is_legal(move);
388 });
389 }
390
391 template <Color Side, bool CapturesOnly>
392 [[nodiscard, gnu::pure]] auto get_bishop_moves(
393 const Position& position,
394 const Bitboard occupiedSquares)
395 {
396 const auto& ourPieces = position.pieces_for<Side>();
397
398 return ourPieces.bishops.squares()
399 | stdv::transform([occupiedSquares,
400 ourOccupied = ourPieces.occupied,
401 theirOccupied = position.pieces_for<pieces::other_side<Side>()>().occupied](
402 const Square bishopPos) noexcept {
403 auto bishopMoves = magics::bishop(bishopPos, occupiedSquares, ourOccupied);
404
405 if constexpr (CapturesOnly) {
406 bishopMoves &= theirOccupied;
407 }
408
409 return bishopMoves.squares()
410 | stdv::transform([bishopPos](const Square targetSquare) noexcept {
411 return Move {
412 bishopPos, targetSquare, PieceType::Bishop
413 };
414 });
415 })
416 | stdv::join
417 | stdv::filter([&position](const Move move) {
418 return position.is_legal(move);
419 });
420 }
421
422 template <Color Side, bool CapturesOnly>
423 [[nodiscard, gnu::pure]] auto get_rook_moves(
424 const Position& position,
425 const Bitboard occupiedSquares)
426 {
427 const auto& ourPieces = position.pieces_for<Side>();
428
429 return ourPieces.rooks.squares()
430 | stdv::transform([occupiedSquares,
431 ourOccupied = ourPieces.occupied,
432 theirOccupied = position.pieces_for<pieces::other_side<Side>()>().occupied](
433 const Square rookPos) noexcept {
434 auto rookMoves = magics::rook(rookPos, occupiedSquares, ourOccupied);
435
436 if constexpr (CapturesOnly) {
437 rookMoves &= theirOccupied;
438 }
439
440 return rookMoves.squares()
441 | stdv::transform([rookPos](const Square targetSquare) noexcept {
442 return Move {
443 rookPos, targetSquare, PieceType::Rook
444 };
445 });
446 })
447 | stdv::join
448 | stdv::filter([&position](const Move move) {
449 return position.is_legal(move);
450 });
451 }
452
453 template <Color Side, bool CapturesOnly>
454 [[nodiscard, gnu::pure]] auto get_queen_moves(
455 const Position& position,
456 const Bitboard occupiedSquares)
457 {
458 const auto& ourPieces = position.pieces_for<Side>();
459
460 return ourPieces.queens.squares()
461 | stdv::transform([occupiedSquares,
462 ourOccupied = ourPieces.occupied,
463 theirOccupied = position.pieces_for<pieces::other_side<Side>()>().occupied](
464 const Square queenPos) noexcept {
465 auto queenMoves = magics::queen(queenPos, occupiedSquares, ourOccupied);
466
467 if constexpr (CapturesOnly) {
468 queenMoves &= theirOccupied;
469 }
470
471 return queenMoves.squares()
472 | stdv::transform([queenPos](const Square targetSquare) noexcept {
473 return Move {
474 queenPos, targetSquare, PieceType::Queen
475 };
476 });
477 })
478 | stdv::join
479 | stdv::filter([&position](const Move move) {
480 return position.is_legal(move);
481 });
482 }
483
484 template <Color Side, bool CapturesOnly>
485 [[nodiscard, gnu::pure]] constexpr auto get_king_moves(
486 const Position& position)
487 {
488 const auto& ourPieces = position.pieces_for<Side>();
489
490 auto kingMoves = pseudo_legal::king(ourPieces.king, ourPieces.occupied);
491
492 if constexpr (CapturesOnly) {
493 kingMoves &= position.pieces_for<pieces::other_side<Side>()>().occupied;
494 }
495
496 return kingMoves.squares()
497 | stdv::transform([kingSquare = ourPieces.get_king_location()](
498 const Square targetSquare) noexcept {
499 return Move {
500 kingSquare, targetSquare, PieceType::King
501 };
502 })
503 | stdv::filter([&position](const Move move) {
504 return position.is_legal(move);
505 });
506 }
507
508 // the two functions below are masks containing the set of squares that
509 // must not be attacked/occupied in order for castling to be allowed
510
511 template <Color Side>
512 [[nodiscard, gnu::const]] consteval auto kingside_castle_mask() noexcept -> Bitboard
513 {
514 static constexpr auto rank = board::back_rank_for(Side);
515
516 Bitboard board;
517
518 board.set(Square { .file = File::F, .rank = rank });
519 board.set(Square { .file = File::G, .rank = rank });
520
521 return board;
522 }
523
524 // NB. with queenside castling, the set of squares that must be free/not attacked differ,
525 // since castling is possible if the B1/B8 squares are attacked, but not if they are occupied
526 template <Color Side, bool Occupied>
527 [[nodiscard, gnu::const]] consteval auto queenside_castle_mask() noexcept -> Bitboard
528 {
529 static constexpr auto rank = board::back_rank_for(Side);
530
531 Bitboard board;
532
533 board.set(Square { .file = File::C, .rank = rank });
534 board.set(Square { .file = File::D, .rank = rank });
535
536 if constexpr (Occupied) {
537 board.set(Square { .file = File::B, .rank = rank });
538 }
539
540 return board;
541 }
542
543 template <Color Side>
544 [[nodiscard, gnu::pure]] constexpr auto get_kingside_castling(
545 const Position& position, const Bitboard allOccupied)
546 -> std::optional<Move>
547 {
548 if (not position.castling_rights_for<Side>().kingside)
549 return std::nullopt;
550
551 static constexpr auto OppositeColor = pieces::other_side<Side>();
552
553 const auto& ourPieces = position.pieces_for<Side>();
554 const auto& theirPieces = position.pieces_for<OppositeColor>();
555
556 assert(ourPieces.rooks.test(Square { File::H, board::back_rank_for(Side) }));
557
558 static constexpr auto requiredSquares = kingside_castle_mask<Side>();
559
560 if ((requiredSquares & allOccupied).any()
561 or squares_attacked<OppositeColor>(theirPieces, requiredSquares, ourPieces.occupied)) {
562 return std::nullopt;
563 }
564
565 if (const auto move = castle_kingside(Side);
566 position.is_legal(move)) {
567 return move;
568 }
569
570 return std::nullopt;
571 }
572
573 template <Color Side>
574 [[nodiscard, gnu::pure]] constexpr auto get_queenside_castling(
575 const Position& position, const Bitboard allOccupied)
576 -> std::optional<Move>
577 {
578 if (not position.castling_rights_for<Side>().queenside)
579 return std::nullopt;
580
581 static constexpr auto OppositeColor = pieces::other_side<Side>();
582
583 const auto& ourPieces = position.pieces_for<Side>();
584 const auto& theirPieces = position.pieces_for<OppositeColor>();
585
586 assert(ourPieces.rooks.test(Square { File::A, board::back_rank_for(Side) }));
587
588 static constexpr auto occupiedMask = queenside_castle_mask<Side, true>();
589 static constexpr auto attackedMask = queenside_castle_mask<Side, false>();
590
591 if ((allOccupied & occupiedMask).any()
592 or squares_attacked<OppositeColor>(theirPieces, attackedMask, ourPieces.occupied)) {
593 return std::nullopt;
594 }
595
596 if (const auto move = castle_queenside(Side);
597 position.is_legal(move)) {
598 return move;
599 }
600
601 return std::nullopt;
602 }
603
604 template <Color Side>
605 [[nodiscard, gnu::pure]] constexpr auto get_castling(
606 const Position& position, const Bitboard allOccupied) -> NMoveList<2uz>
607 {
608 // castling out of check is not allowed
609 if (position.is_check())
610 return { };
611
612 NMoveList<2uz> moves;
613
614 auto add_move = [&moves](const Move move) {
615 moves.emplace_back(move);
616 return std::monostate { };
617 };
618
619 get_kingside_castling<Side>(position, allOccupied).transform(add_move);
620 get_queenside_castling<Side>(position, allOccupied).transform(add_move);
621
622 return moves;
623 }
624
625 template <Color Side, bool CapturesOnly>
626 void generate_internal(
627 const Position& position,
628 std::output_iterator<Move> auto outputIt)
629 {
630 const auto& ourPieces = position.pieces_for<Side>();
631 const auto& theirPieces = position.pieces_for<pieces::other_side<Side>()>();
632
633 const auto allOccupied = ourPieces.occupied | theirPieces.occupied;
634
635 add_all_pawn_moves<Side, CapturesOnly>(position, allOccupied, outputIt);
636
637 std::ranges::copy(
638 get_knight_moves<Side, CapturesOnly>(position),
639 outputIt);
640
641 std::ranges::copy(
642 get_bishop_moves<Side, CapturesOnly>(position, allOccupied),
643 outputIt);
644
645 std::ranges::copy(
646 get_rook_moves<Side, CapturesOnly>(position, allOccupied),
647 outputIt);
648
649 std::ranges::copy(
650 get_queen_moves<Side, CapturesOnly>(position, allOccupied),
651 outputIt);
652
653 std::ranges::copy(
654 get_king_moves<Side, CapturesOnly>(position),
655 outputIt);
656
657 if constexpr (not CapturesOnly) {
658 std::ranges::copy(
659 get_castling<Side>(position, allOccupied),
660 outputIt);
661 }
662 }
663
664 template <Color Side, bool CapturesOnly>
665 void generate_for_internal(
666 const Position& position, const PieceType piece,
667 std::output_iterator<Move> auto outputIt)
668 {
669 const auto& ourPieces = position.pieces_for<Side>();
670 const auto& theirPieces = position.pieces_for<pieces::other_side<Side>()>();
671
672 const auto allOccupied = ourPieces.occupied | theirPieces.occupied;
673
674 switch (piece) {
675 case PieceType::Pawn: {
676 add_all_pawn_moves<Side, CapturesOnly>(position, allOccupied, outputIt);
677 return;
678 }
679
680 case PieceType::Knight: {
681 std::ranges::copy(
682 get_knight_moves<Side, CapturesOnly>(position),
683 outputIt);
684 return;
685 }
686
687 case PieceType::Bishop: {
688 std::ranges::copy(
689 get_bishop_moves<Side, CapturesOnly>(position, allOccupied),
690 outputIt);
691 return;
692 }
693
694 case PieceType::Rook: {
695 std::ranges::copy(
696 get_rook_moves<Side, CapturesOnly>(position, allOccupied),
697 outputIt);
698 return;
699 }
700
701 case PieceType::Queen: {
702 std::ranges::copy(
703 get_queen_moves<Side, CapturesOnly>(position, allOccupied),
704 outputIt);
705 return;
706 }
707
708 default : [[fallthrough]];
709 case PieceType::King: {
710 std::ranges::copy(
711 get_king_moves<Side, CapturesOnly>(position),
712 outputIt);
713
714 if constexpr (not CapturesOnly) {
715 // castling is considered a King move
716 std::ranges::copy(
717 get_castling<Side>(position, allOccupied),
718 outputIt);
719 }
720
721 return;
722 }
723 }
724 }
725
726 template <Color Side>
727 [[nodiscard]] auto any_legal_moves_internal(const Position& position) -> bool
728 {
729 MoveList moves;
730
731 // as an optimization, check for king moves first, because in a double check,
732 // a king move would be the only valid response
733 for (const auto piece : { PieceType::King, PieceType::Pawn, PieceType::Knight, PieceType::Queen, PieceType::Rook, PieceType::Bishop }) {
734 generate_for_internal<Side, false>(position, piece, std::back_inserter(moves));
735
736 if (not moves.empty())
737 return true;
738
739 moves.clear();
740 }
741
742 return false;
743 }
744
745} // namespace detail
746#endif // DOXYGEN
747
748template <bool CapturesOnly>
750 const Position& position,
751 std::output_iterator<Move> auto outputIt)
752{
753 if (position.is_white_to_move())
754 detail::generate_internal<Color::White, CapturesOnly>(position, outputIt);
755 else
756 detail::generate_internal<Color::Black, CapturesOnly>(position, outputIt);
757}
758
759template <bool CapturesOnly>
760auto generate(const Position& position) -> MoveList
761{
763
764 generate<CapturesOnly>(position, std::back_inserter(moves));
765
766 return moves;
767}
768
769template <bool CapturesOnly>
771 const Position& position, const PieceType piece,
772 std::output_iterator<Move> auto outputIt)
773{
774 if (position.is_white_to_move())
775 detail::generate_for_internal<Color::White, CapturesOnly>(position, piece, outputIt);
776 else
777 detail::generate_for_internal<Color::Black, CapturesOnly>(position, piece, outputIt);
778}
779
780template <bool CapturesOnly>
782 const Position& position, const PieceType piece)
783 -> MoveList
784{
786
787 generate_for<CapturesOnly>(position, piece, std::back_inserter(moves));
788
789 return moves;
790}
791
792inline auto any_legal_moves(const Position& position) -> bool
793{
794 if (position.is_white_to_move())
795 return detail::any_legal_moves_internal<Color::White>(position);
796
797 return detail::any_legal_moves_internal<Color::Black>(position);
798}
799
800namespace detail {
801 template <Color Side>
802 [[nodiscard, gnu::pure]] auto potentially_legal_ep_squares(
803 const Position& position) noexcept
804 {
805 using beman::inplace_vector::inplace_vector;
806
807 static constexpr auto OppositeColor = pieces::other_side<Side>();
808
809 static constexpr auto epRankMask = Side == Color::White
812
813 const auto& ourPieces = position.pieces_for<Side>();
814 const auto& theirPieces = position.pieces_for<OppositeColor>();
815
816 const auto possibleTakers = ourPieces.pawns & epRankMask;
817 const auto posibleTaken = theirPieces.pawns & epRankMask;
818
819 return possibleTakers.subboards()
820 | stdv::transform([posibleTaken](const Bitboard startSquare) noexcept {
821 const auto attacks = patterns::pawn_attacks<Side>(startSquare);
822 const auto takenMask = patterns::pawn_pushes<Side>(posibleTaken);
823
824 return (attacks & takenMask).squares()
825 | std::ranges::to<inplace_vector<Square, 2uz>>();
826 })
827 | stdv::join
828 | std::ranges::to<inplace_vector<Square, 8uz>>();
829 }
830} // namespace detail
831
833 const Position& position)
834{
835 auto squares = position.is_white_to_move()
838
839 // sort & filter duplicates
840
841 std::ranges::sort(squares);
842
843 const auto toErase = std::ranges::unique(squares);
844
845 squares.erase(toErase.begin(), toErase.end());
846
847 return squares;
848}
849
850} // namespace chess::moves
constexpr auto FIVE
Definition Masks.hpp:201
constexpr auto FOUR
Definition Masks.hpp:198
constexpr auto prev_pawn_rank(Rank rank) noexcept -> Rank
Definition Rank.hpp:155
constexpr auto rank(Bitboard starting) noexcept -> Bitboard
Definition Fills.hpp:218
beman::inplace_vector::inplace_vector< Move, N > NMoveList
Definition MoveGen.hpp:58
constexpr auto pawn_pushes(Bitboard startingPawns, Bitboard emptySquares) noexcept -> Bitboard
constexpr auto pawn_attacks(Bitboard starting) noexcept -> Bitboard
Definition Patterns.hpp:120
auto get_potentially_legal_en_passant_target_squares(const Position &position)
Definition MoveGen.hpp:832
constexpr auto MAX_MOVES
Definition MoveGen.hpp:67
NMoveList< MAX_MOVES > MoveList
Definition MoveGen.hpp:72
void generate_for(const Position &position, PieceType piece, std::output_iterator< Move > auto outputIt)
Definition MoveGen.hpp:770
constexpr auto pawn_double_pushes(Bitboard startingPawns, Bitboard occupiedSquares) noexcept -> Bitboard
auto any_legal_moves(const Position &position) -> bool
Definition MoveGen.hpp:792
void generate(const Position &position, std::output_iterator< Move > auto outputIt)
Definition MoveGen.hpp:749
constexpr auto pawn_pushes(Bitboard starting) noexcept -> Bitboard
Definition Patterns.hpp:98
constexpr auto other_side() noexcept -> Color
Definition Colors.hpp:40
auto potentially_legal_ep_squares(const Position &position) noexcept
Definition MoveGen.hpp:802
constexpr auto castle_queenside(const Color color) noexcept -> Move
Definition Move.hpp:315
constexpr auto castle_kingside(const Color color) noexcept -> Move
Definition Move.hpp:304
pieces::Type PieceType
Definition Move.hpp:54
auto is_white_to_move() const noexcept -> bool
Definition Position.hpp:225
static constexpr auto from_square(Square square) noexcept -> Bitboard
Definition Bitboard.hpp:279