[MoveGenerator] Add MoveGenerator

This commit is contained in:
2022-12-22 18:31:13 +01:00
parent e1d216f06b
commit fadfab165a
3 changed files with 99 additions and 0 deletions

62
MoveGenerator.cpp Normal file
View File

@@ -0,0 +1,62 @@
#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));
}
}
}