[MoveGenerator] Add MoveGenerator
This commit is contained in:
@@ -36,6 +36,7 @@ add_library(cplchess_lib OBJECT
|
||||
EngineFactory.cpp
|
||||
Uci.cpp
|
||||
BitBoard.cpp
|
||||
MoveGenerator.cpp
|
||||
)
|
||||
|
||||
target_include_directories(cplchess_lib PUBLIC .)
|
||||
|
62
MoveGenerator.cpp
Normal file
62
MoveGenerator.cpp
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
36
MoveGenerator.hpp
Normal file
36
MoveGenerator.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef CHESS_ENGINE_MOVEGENERATOR_HPP
|
||||
#define CHESS_ENGINE_MOVEGENERATOR_HPP
|
||||
|
||||
#include "Piece.hpp"
|
||||
#include "Square.hpp"
|
||||
#include "Move.hpp"
|
||||
#include "CastlingRights.hpp"
|
||||
#include "BitBoard.hpp"
|
||||
|
||||
#include <optional>
|
||||
#include <iosfwd>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
class MoveGenerator {
|
||||
public:
|
||||
using MoveVec = std::vector<Move>;
|
||||
|
||||
MoveGenerator(const std::shared_ptr<BitBoard[]>& pieceBB, const std::shared_ptr<BitBoard>& occupiedBB);
|
||||
MoveGenerator(MoveGenerator &) = delete;
|
||||
MoveGenerator &operator=(MoveGenerator const &) = delete;
|
||||
|
||||
void generatePawnMoves(const Square &from, const std::optional<Square> &eps, PieceColor color, MoveVec &moves) const;
|
||||
void generatePawnMoves(const Square &from, PieceColor color, MoveVec &moves);
|
||||
|
||||
private:
|
||||
const std::shared_ptr<const BitBoard[]> mPieceBBs;
|
||||
const std::shared_ptr<const BitBoard> mOccupiedBB;
|
||||
|
||||
void generatePawnMoves(const Square &from, BitBoard targets, PieceColor color, MoveVec &moves) const;
|
||||
|
||||
static void generateMoves(const Square &from, BitBoard movesBB, MoveVec &moves);
|
||||
static void generateMovesWithPromotion(const Square &from, BitBoard movesBB, MoveVec &moves);
|
||||
};
|
||||
|
||||
#endif //CHESS_ENGINE_MOVEGENERATOR_HPP
|
Reference in New Issue
Block a user