[MoveGenerator] cleanup

This commit is contained in:
2022-12-22 23:14:19 +01:00
parent 8d028ba087
commit 79b5dd4a86
4 changed files with 59 additions and 54 deletions

View File

@@ -1,3 +1,4 @@
#include <iostream>
#include "BitBoard.hpp" #include "BitBoard.hpp"
BitBoard::BitBoard(uint64_t v) { BitBoard::BitBoard(uint64_t v) {
@@ -57,10 +58,13 @@ BitBoard BitBoard::castlingMoves(BitBoard kings, BitBoard rooks, BitBoard empty)
kings |= (kings.east() | kings.west()) & empty; kings |= (kings.east() | kings.west()) & empty;
kings |= (kings.east() | kings.west()) & empty; kings |= (kings.east() | kings.west()) & empty;
kings |= kings.west() & empty; kings |= kings.west() & empty;
std::cout << "rooks1" << std::endl << rooks;
rooks |= (rooks.east() | rooks.west()) & empty; rooks |= (rooks.east() | rooks.west()) & empty;
rooks |= (rooks.east() | rooks.west()) & empty; rooks |= (rooks.east() | rooks.west()) & empty;
rooks |= (rooks.east()) & empty; rooks |= (rooks.east()) & empty;
std::cout << "rooks2" << std::endl << rooks;
return kings & rooks & CastlingSquares; return kings & rooks & CastlingSquares;
} }

View File

@@ -6,7 +6,6 @@
#include <cmath> #include <cmath>
#include <bitset> #include <bitset>
#include <algorithm> #include <algorithm>
#include <iostream>
void Board::setPiece(const Square &square, const Piece::Optional &piece) { void Board::setPiece(const Square &square, const Piece::Optional &piece) {
if (!piece.has_value()) if (!piece.has_value())
@@ -159,17 +158,17 @@ void Board::pseudoLegalMovesFrom(const Square &from, Board::MoveVec &moves) cons
BitBoard movesBB; BitBoard movesBB;
switch (p.type()) { switch (p.type()) {
case PieceType::Pawn: MoveGenerator::generatePawnMoves(bs, from, mEPS, mTurn, moves); case PieceType::Pawn: MoveGenerator::generatePawnMoves(bs, from, moves);
return; return;
case PieceType::Knight: break; case PieceType::Knight: break;
case PieceType::Bishop: MoveGenerator::generateBishopMoves(bs, from, mTurn, moves); case PieceType::Bishop: MoveGenerator::generateBishopMoves(bs, from, moves);
return; return;
case PieceType::Rook: MoveGenerator::generateRookMoves(bs, from, mTurn, moves); case PieceType::Rook: MoveGenerator::generateRookMoves(bs, from, moves);
return; return;
case PieceType::Queen: MoveGenerator::generateQueenMoves(bs, from, mTurn, moves); case PieceType::Queen: MoveGenerator::generateQueenMoves(bs, from, moves);
return; return;
case PieceType::King: MoveGenerator::generateKingMoves(bs, from, mTurn, mCR, moves); case PieceType::King: MoveGenerator::generateKingMoves(bs, from, moves);
break; break;
} }

View File

@@ -1,33 +1,29 @@
#include <iostream>
#include "MoveGenerator.hpp" #include "MoveGenerator.hpp"
#include "Board.hpp" #include "Board.hpp"
#include "BoardState.hpp" #include "BoardState.hpp"
void MoveGenerator::generatePawnMoves(const BoardState &bs, const Square &from, MoveVec &moves) {
void MoveGenerator::generatePawnMoves(const BoardState &bs, const Square &from, const std::optional<Square> &eps,
PieceColor color, MoveVec &moves) {
BitBoard targets = 0; BitBoard targets = 0;
if (eps.has_value()) { if (bs.eps.has_value()) {
targets |= BitBoard::fromIndex(eps.value().index()); targets |= BitBoard::fromIndex(bs.eps.value().index());
} }
generatePawnMoves(bs, from, targets, color, moves); generatePawnMoves(bs, from, targets, moves);
}
void MoveGenerator::generatePawnMoves(const BoardState &bs, const Square &from, PieceColor color, MoveVec &moves) {
generatePawnMoves(bs, from, BitBoard(0), color, 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()); auto fromBB = BitBoard::fromIndex(from.index());
targets |= *bs.pieceBBs[Board::toIndex(!color)]; targets |= (*bs.pieceBBs)[Board::toIndex(!bs.turn)];
BitBoard movesBB; BitBoard movesBB;
if (color == PieceColor::White) { if (bs.turn == PieceColor::White) {
movesBB = BitBoard::pawnNorthAttacks(fromBB, targets) & ~*bs.pieceBBs[Board::toIndex(color)]; movesBB = BitBoard::pawnNorthAttacks(fromBB, targets) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
movesBB |= BitBoard::pawnNorthMoves(fromBB, ~bs.occupiedBB); movesBB |= BitBoard::pawnNorthMoves(fromBB, ~bs.occupiedBB);
} else { } 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); 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 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); 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, void MoveGenerator::generateKingMoves(const BoardState &bs, const Square &from, MoveGenerator::MoveVec &moves) {
PieceColor color,
CastlingRights cr,
MoveGenerator::MoveVec &moves) {
auto fromBB = BitBoard::fromIndex(from.index()); 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 checkCR = CastlingRights::White;
auto castlingRank = WhiteCastlingRank; auto castlingRank = WhiteCastlingRank;
if (color == PieceColor::Black) { if (bs.turn == PieceColor::Black) {
checkCR = CastlingRights::Black; checkCR = CastlingRights::Black;
castlingRank = BlackCastlingRank; castlingRank = BlackCastlingRank;
} }
checkCR &= cr; checkCR &= bs.cr;
if (checkCR != CastlingRights::None) { if (checkCR != CastlingRights::None) {
// Generate attacked squares // Generate attacked squares
auto opponentBB = *bs.pieceBBs[Board::toIndex(!color)]; // auto opponentBB = (*bs.pieceBBs)[Board::toIndex(!color)];
BitBoard target = CastlingRanks | bs.occupiedBB; // BitBoard target = CastlingRanks | bs.occupiedBB;
// pawns // // pawns
BitBoard attacked = 0; // BitBoard attacked = 0;
if (!color == PieceColor::White) { // if (!color == PieceColor::White) {
attacked |= BitBoard::pawnNorthAttacks(*bs.pieceBBs[Board::toIndex(PieceType::Pawn)] & opponentBB, target); // attacked |= BitBoard::pawnNorthAttacks((*bs.pieceBBs)[Board::toIndex(PieceType::Pawn)] & opponentBB, target);
} else { // } else {
attacked |= BitBoard::pawnSouthAttacks(*bs.pieceBBs[Board::toIndex(PieceType::Pawn)] & opponentBB, target); // 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; & castlingRank;// & ~attacked;
if ((checkCR & CastlingRights::KingSide) == CastlingRights::None) { if ((checkCR & CastlingRights::KingSide) == CastlingRights::None) {
@@ -106,17 +99,30 @@ void MoveGenerator::generateKingMoves(const BoardState &bs, const Square &from,
generateMoves(from, movesBB, moves); 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 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); 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 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); 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);
// }
//
//}

View File

@@ -17,18 +17,14 @@ class MoveGenerator {
public: public:
using MoveVec = std::vector<Move>; using MoveVec = std::vector<Move>;
static void generatePawnMoves(const BoardState &bs, const Square &from, static void generatePawnMoves(const BoardState &bs, const Square &from, MoveVec &moves);
const std::optional<Square> &eps, static void generateBishopMoves(const BoardState &bs, const Square &from, MoveVec &moves);
PieceColor color, static void generateKingMoves(const BoardState &bs, const Square &from, MoveVec &moves);
MoveVec &moves); static void generateRookMoves(const BoardState &bs, const Square &from, MoveVec &moves);
static void generatePawnMoves(const BoardState &bs, const Square &from, PieceColor color, MoveVec &moves); static void generateQueenMoves(const BoardState &bs, const Square &from, MoveVec &moves);
static void generateBishopMoves(const BoardState &bs, const Square &from, PieceColor color, MoveVec &moves);
static void generateKingMoves(const BoardState &bs, const Square &from, PieceColor color, CastlingRights cr, MoveVec &moves);
static void generateRookMoves(const BoardState &bs, const Square &from, PieceColor color, MoveVec &moves);
static void generateQueenMoves(const BoardState &bs, const Square &from, PieceColor color, MoveVec &moves);
private: private:
static void generatePawnMoves(const BoardState &bs, const Square &from, BitBoard targets, PieceColor color, MoveVec &moves); static void generatePawnMoves(const BoardState &bs, const Square &from, BitBoard targets, MoveVec &moves);
static void generateMoves(const Square &from, BitBoard movesBB, MoveVec &moves); static void generateMoves(const Square &from, BitBoard movesBB, MoveVec &moves);
static void generateMovesWithPromotion(const Square &from, BitBoard movesBB, MoveVec &moves); static void generateMovesWithPromotion(const Square &from, BitBoard movesBB, MoveVec &moves);