63 lines
2.1 KiB
C++
63 lines
2.1 KiB
C++
#include <iostream>
|
|
#include "MoveGenerator.hpp"
|
|
#include "Board.hpp"
|
|
|
|
MoveGenerator::MoveGenerator(const std::shared_ptr<BitBoard[]> &pieceBB,
|
|
const std::shared_ptr<BitBoard> &occupiedBB)
|
|
: mPieceBBs(pieceBB), mOccupiedBB(occupiedBB) {
|
|
|
|
}
|
|
|
|
void MoveGenerator::generatePawnMoves(const Square &from, const std::optional<Square> &eps,
|
|
PieceColor color, MoveVec &moves) const {
|
|
|
|
BitBoard targets = 0;
|
|
if (eps.has_value()) {
|
|
targets |= BitBoard::fromIndex(eps.value().index());
|
|
}
|
|
|
|
generatePawnMoves(from, targets, color, moves);
|
|
}
|
|
void MoveGenerator::generatePawnMoves(const Square &from, PieceColor color, MoveVec &moves) {
|
|
generatePawnMoves(from, BitBoard(0), color, moves);
|
|
}
|
|
|
|
void MoveGenerator::generatePawnMoves(const Square &from, BitBoard targets, PieceColor color, MoveVec &moves) const {
|
|
|
|
auto fromBB = BitBoard::fromIndex(from.index());
|
|
targets |= mPieceBBs[Board::toIndex(!color)];
|
|
|
|
BitBoard movesBB;
|
|
if (color == PieceColor::White) {
|
|
movesBB = BitBoard::pawnNorthAttacks(fromBB, targets) & ~mPieceBBs[Board::toIndex(color)];
|
|
movesBB |= BitBoard::pawnNorthMoves(fromBB, ~*mOccupiedBB);
|
|
} else {
|
|
movesBB = BitBoard::pawnSouthAttacks(fromBB, targets) & ~mPieceBBs[Board::toIndex(color)];
|
|
movesBB |= BitBoard::pawnSouthMoves(fromBB, ~*mOccupiedBB);
|
|
}
|
|
|
|
bool isPromotion = movesBB & (Rank1 | Rank8);
|
|
if (isPromotion) {
|
|
generateMovesWithPromotion(from, movesBB, moves);
|
|
} else {
|
|
generateMoves(from, movesBB, moves);
|
|
}
|
|
}
|
|
|
|
void MoveGenerator::generateMoves(const Square &from, BitBoard movesBB, MoveGenerator::MoveVec &moves) {
|
|
while (movesBB) {
|
|
auto to = Square(movesBB.pop());
|
|
moves.emplace_back(from, to, std::nullopt);
|
|
}
|
|
}
|
|
|
|
void MoveGenerator::generateMovesWithPromotion(const Square &from, BitBoard movesBB, MoveGenerator::MoveVec &moves) {
|
|
while (movesBB) {
|
|
auto to = Square(movesBB.pop());
|
|
for (const auto &kItem : Piece::PromotionTypes) {
|
|
moves.emplace_back(from, to, static_cast<PieceType>(kItem));
|
|
}
|
|
}
|
|
}
|
|
|