[MoveGenerator] cleanup
This commit is contained in:
@@ -1,33 +1,29 @@
|
||||
#include <iostream>
|
||||
#include "MoveGenerator.hpp"
|
||||
#include "Board.hpp"
|
||||
#include "BoardState.hpp"
|
||||
|
||||
|
||||
void MoveGenerator::generatePawnMoves(const BoardState &bs, const Square &from, const std::optional<Square> &eps,
|
||||
PieceColor color, MoveVec &moves) {
|
||||
void MoveGenerator::generatePawnMoves(const BoardState &bs, const Square &from, MoveVec &moves) {
|
||||
|
||||
BitBoard targets = 0;
|
||||
if (eps.has_value()) {
|
||||
targets |= BitBoard::fromIndex(eps.value().index());
|
||||
if (bs.eps.has_value()) {
|
||||
targets |= BitBoard::fromIndex(bs.eps.value().index());
|
||||
}
|
||||
|
||||
generatePawnMoves(bs, from, targets, color, moves);
|
||||
}
|
||||
void MoveGenerator::generatePawnMoves(const BoardState &bs, const Square &from, PieceColor color, MoveVec &moves) {
|
||||
generatePawnMoves(bs, from, BitBoard(0), color, moves);
|
||||
generatePawnMoves(bs, from, targets, moves);
|
||||
}
|
||||
|
||||
void MoveGenerator::generatePawnMoves(const BoardState &bs, const Square &from, BitBoard targets, PieceColor color, MoveVec &moves) {
|
||||
void MoveGenerator::generatePawnMoves(const BoardState &bs, const Square &from, BitBoard targets, MoveVec &moves) {
|
||||
|
||||
auto fromBB = BitBoard::fromIndex(from.index());
|
||||
targets |= *bs.pieceBBs[Board::toIndex(!color)];
|
||||
targets |= (*bs.pieceBBs)[Board::toIndex(!bs.turn)];
|
||||
|
||||
BitBoard movesBB;
|
||||
if (color == PieceColor::White) {
|
||||
movesBB = BitBoard::pawnNorthAttacks(fromBB, targets) & ~*bs.pieceBBs[Board::toIndex(color)];
|
||||
if (bs.turn == PieceColor::White) {
|
||||
movesBB = BitBoard::pawnNorthAttacks(fromBB, targets) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
||||
movesBB |= BitBoard::pawnNorthMoves(fromBB, ~bs.occupiedBB);
|
||||
} else {
|
||||
movesBB = BitBoard::pawnSouthAttacks(fromBB, targets) & ~*bs.pieceBBs[Board::toIndex(color)];
|
||||
movesBB = BitBoard::pawnSouthAttacks(fromBB, targets) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
||||
movesBB |= BitBoard::pawnSouthMoves(fromBB, ~bs.occupiedBB);
|
||||
}
|
||||
|
||||
@@ -39,9 +35,9 @@ void MoveGenerator::generatePawnMoves(const BoardState &bs, const Square &from,
|
||||
}
|
||||
}
|
||||
|
||||
void MoveGenerator::generateBishopMoves(const BoardState &bs, const Square &from, PieceColor color, MoveVec &moves) {
|
||||
void MoveGenerator::generateBishopMoves(const BoardState &bs, const Square &from, MoveVec &moves) {
|
||||
auto fromBB = BitBoard::fromIndex(from.index());
|
||||
auto movesBB = BitBoard::bishopAttacks(fromBB, ~bs.occupiedBB) & ~*bs.pieceBBs[Board::toIndex(color)];
|
||||
auto movesBB = BitBoard::bishopAttacks(fromBB, ~bs.occupiedBB) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
||||
|
||||
generateMoves(from, movesBB, moves);
|
||||
}
|
||||
@@ -61,37 +57,34 @@ void MoveGenerator::generateMovesWithPromotion(const Square &from, BitBoard move
|
||||
}
|
||||
}
|
||||
}
|
||||
void MoveGenerator::generateKingMoves(const BoardState &bs, const Square &from,
|
||||
PieceColor color,
|
||||
CastlingRights cr,
|
||||
MoveGenerator::MoveVec &moves) {
|
||||
void MoveGenerator::generateKingMoves(const BoardState &bs, const Square &from, MoveGenerator::MoveVec &moves) {
|
||||
auto fromBB = BitBoard::fromIndex(from.index());
|
||||
auto movesBB = BitBoard::kingAttacks(fromBB) & ~*bs.pieceBBs[Board::toIndex(color)];
|
||||
auto movesBB = BitBoard::kingAttacks(fromBB) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
||||
|
||||
if ((cr & CastlingRights::All) != CastlingRights::None) {
|
||||
if ((bs.cr & CastlingRights::All) != CastlingRights::None) {
|
||||
|
||||
auto checkCR = CastlingRights::White;
|
||||
auto castlingRank = WhiteCastlingRank;
|
||||
if (color == PieceColor::Black) {
|
||||
if (bs.turn == PieceColor::Black) {
|
||||
checkCR = CastlingRights::Black;
|
||||
castlingRank = BlackCastlingRank;
|
||||
}
|
||||
|
||||
checkCR &= cr;
|
||||
checkCR &= bs.cr;
|
||||
|
||||
if (checkCR != CastlingRights::None) {
|
||||
// Generate attacked squares
|
||||
auto opponentBB = *bs.pieceBBs[Board::toIndex(!color)];
|
||||
BitBoard target = CastlingRanks | bs.occupiedBB;
|
||||
// pawns
|
||||
BitBoard attacked = 0;
|
||||
if (!color == PieceColor::White) {
|
||||
attacked |= BitBoard::pawnNorthAttacks(*bs.pieceBBs[Board::toIndex(PieceType::Pawn)] & opponentBB, target);
|
||||
} else {
|
||||
attacked |= BitBoard::pawnSouthAttacks(*bs.pieceBBs[Board::toIndex(PieceType::Pawn)] & opponentBB, target);
|
||||
}
|
||||
// auto opponentBB = (*bs.pieceBBs)[Board::toIndex(!color)];
|
||||
// BitBoard target = CastlingRanks | bs.occupiedBB;
|
||||
// // pawns
|
||||
// BitBoard attacked = 0;
|
||||
// if (!color == PieceColor::White) {
|
||||
// attacked |= BitBoard::pawnNorthAttacks((*bs.pieceBBs)[Board::toIndex(PieceType::Pawn)] & opponentBB, target);
|
||||
// } else {
|
||||
// attacked |= BitBoard::pawnSouthAttacks((*bs.pieceBBs)[Board::toIndex(PieceType::Pawn)] & opponentBB, target);
|
||||
// }
|
||||
|
||||
movesBB |= BitBoard::castlingMoves(fromBB, *bs.pieceBBs[Board::toIndex(PieceType::Rook)], ~bs.occupiedBB)
|
||||
movesBB |= BitBoard::castlingMoves(fromBB, (*bs.pieceBBs)[Board::toIndex(PieceType::Rook)], ~bs.occupiedBB)
|
||||
& castlingRank;// & ~attacked;
|
||||
|
||||
if ((checkCR & CastlingRights::KingSide) == CastlingRights::None) {
|
||||
@@ -106,17 +99,30 @@ void MoveGenerator::generateKingMoves(const BoardState &bs, const Square &from,
|
||||
generateMoves(from, movesBB, moves);
|
||||
}
|
||||
|
||||
void MoveGenerator::generateRookMoves(const BoardState &bs, const Square &from, PieceColor color, MoveVec &moves) {
|
||||
void MoveGenerator::generateRookMoves(const BoardState &bs, const Square &from, MoveVec &moves) {
|
||||
auto fromBB = BitBoard::fromIndex(from.index());
|
||||
auto movesBB = BitBoard::rookAttacks(fromBB, ~bs.occupiedBB) & ~*bs.pieceBBs[Board::toIndex(color)];
|
||||
auto movesBB = BitBoard::rookAttacks(fromBB, ~bs.occupiedBB) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
||||
|
||||
generateMoves(from, movesBB, moves);
|
||||
}
|
||||
|
||||
void MoveGenerator::generateQueenMoves(const BoardState &bs, const Square &from, PieceColor color, MoveVec &moves) {
|
||||
void MoveGenerator::generateQueenMoves(const BoardState &bs, const Square &from, MoveVec &moves) {
|
||||
auto fromBB = BitBoard::fromIndex(from.index());
|
||||
auto movesBB = BitBoard::queenAttacks(fromBB, ~bs.occupiedBB) & ~*bs.pieceBBs[Board::toIndex(color)];
|
||||
auto movesBB = BitBoard::queenAttacks(fromBB, ~bs.occupiedBB) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
||||
|
||||
generateMoves(from, movesBB, moves);
|
||||
}
|
||||
|
||||
//BitBoard MoveGenerator::generateAttackedSquares(const BoardState &bs, BitBoard target, PieceColor opColor) const {
|
||||
// auto opponentBB = (*bs.pieceBBs)[Board::toIndex(opColor)];
|
||||
//
|
||||
// BitBoard attacked = 0;
|
||||
//
|
||||
// // pawns
|
||||
// if (opColor == PieceColor::White) {
|
||||
// attacked |= BitBoard::pawnNorthAttacks((*bs.pieceBBs)[Board::toIndex(PieceType::Pawn)] & opponentBB, target);
|
||||
// } else {
|
||||
// attacked |= BitBoard::pawnSouthAttacks((*bs.pieceBBs)[Board::toIndex(PieceType::Pawn)] & opponentBB, target);
|
||||
// }
|
||||
//
|
||||
//}
|
||||
|
Reference in New Issue
Block a user