Cleared previous method of movegen

This commit is contained in:
rektdie committed 2024-04-04 22:49:56 +02:00
1 parent cc4409ef3d
commit 09dfeb7198
6 files changed
+42 -459

No files matched your search

+1 -137
View File
@@ -243,9 +243,7 @@ void Board::ResetMoves() {
void Board::ListMoves() {
int count = 1;
for (Move move : moveList) {
std::cout << count << ". ";
move.PrintMove();
count++;
}
}
@@ -299,139 +297,5 @@ void Board::Promote(int square, int pieceType, int color, bool isCapture) {
}
void Board::MakeMove(Move move) {
int oldEPTarget = enPassantTarget;
enPassantTarget = a1; // resetting (a1 is impossible)
std::array<bool, 4> oldCastlingRights = castlingRights;
uint16_t moveType = move.moveFlags;
int attackerType = GetPieceType(move.moveFrom);
bool attackerColor = GetPieceColor(move.moveFrom);
int targetType = GetPieceType(move.moveTo);
int direction = attackerColor ? south : north;
// Removing attacker from old square
RemovePiece(attackerType, move.moveFrom, attackerColor);
if (moveType == doublePawnPush) {
enPassantTarget = move.moveFrom + direction;
} else if (moveType == capture) {
RemovePiece(targetType, move.moveTo, !attackerColor);
} else if (moveType == epCapture) {
targetType = Pawn;
RemovePiece(targetType, move.moveTo - direction, !attackerColor);
} else if (moveType == queenCastle) {
int rookSquare = attackerColor ? a8 : a1;
// Removing rook from old position
RemovePiece(Rook, rookSquare, attackerColor);
// Setting rook on new position
SetPiece(Rook, rookSquare + 3, attackerColor);
} else if (moveType == kingCastle) {
int rookSquare = attackerColor ? h8 : h1;
// Removing rook from old position
RemovePiece(Rook, rookSquare, attackerColor);
// Setting rook on new position
SetPiece(Rook, rookSquare - 2, attackerColor);
} else if (moveType == knightPromotion) {
Promote(move.moveTo, Knight, attackerColor, false);
} else if (moveType == bishopPromotion) {
Promote(move.moveTo, Bishop, attackerColor, false);
} else if (moveType == rookPromotion) {
Promote(move.moveTo, Rook, attackerColor, false);
} else if (moveType == queenPromotion) {
Promote(move.moveTo, Queen, attackerColor, false);
} else if (moveType == knightPromoCapture) {
Promote(move.moveTo, Knight, attackerColor, true);
} else if (moveType == bishopPromoCapture) {
Promote(move.moveTo, Bishop, attackerColor, true);
} else if (moveType == rookPromoCapture) {
Promote(move.moveTo, Rook, attackerColor, true);
} else if (moveType == queenPromoCapture) {
Promote(move.moveTo, Queen, attackerColor, true);
}
// Setting attacker on the target square if its not a promotion
if (moveType < knightPromotion) {
SetPiece(attackerType, move.moveTo, attackerColor);
}
// Removing the right to castle on king movement
if (attackerType == King) {
castlingRights[attackerColor * 2] = false;
castlingRights[attackerColor * 2 + 1] = false;
}
// Removing the right to castle on rook movement
if (attackerType == Rook) {
if ((Bitboard::GetSquare(move.moveFrom) & files[A] & (ranks[r_1] | ranks[r_8])).GetBoard()) {
castlingRights[attackerColor * 2 + 1] = false;
} else if ((Bitboard::GetSquare(move.moveFrom) & files[H] & (ranks[r_1] | ranks[r_8])).GetBoard()) {
castlingRights[attackerColor * 2] = false;
}
}
GenAttackMaps(*this);
halfMoves++;
if (halfMoves & 2 == 0) fullMoves++;
sideToMove = !attackerColor;
lastMove = LastMove(move, targetType, !attackerColor, oldEPTarget, oldCastlingRights, attackerType);
}
void Board::UnmakeMove() {
enPassantTarget = lastMove.enPassantTarget;
uint16_t moveType = lastMove.moveFlags;
int direction = lastMove.capturedColor ? south : north;
// Removing attacker piece from new square
RemovePiece(lastMove.attackerPiece, lastMove.moveTo, !lastMove.capturedColor);
// Setting back the attacker
SetPiece(lastMove.attackerPiece, lastMove.moveFrom, !lastMove.capturedColor);
// Setting back the target
if (moveType == quiet || moveType == doublePawnPush) {
RemovePiece(lastMove.attackerPiece, lastMove.moveTo, !lastMove.capturedColor);
} else if (moveType == capture) {
SetPiece(lastMove.capturedPiece, lastMove.moveTo, lastMove.capturedColor);
} else if (moveType == epCapture) {
SetPiece(lastMove.capturedPiece, lastMove.moveTo + direction, lastMove.capturedColor);
} else if (moveType == queenCastle) {
int rookSquare = !lastMove.capturedColor ? a8 : a1;
// Removing rook from new position
RemovePiece(Rook, rookSquare + 3, !lastMove.capturedColor);
// Setting rook back on old position
SetPiece(Rook, rookSquare, !lastMove.capturedColor);
} else if (moveType == kingCastle) {
int rookSquare = !lastMove.capturedColor ? h8 : h1;
// Removing rook from new position
RemovePiece(Rook, rookSquare - 2, !lastMove.capturedColor);
// Setting rook back to old position
SetPiece(Rook, rookSquare, !lastMove.capturedColor);
} else if (moveType >= knightPromotion && moveType <= queenPromotion) {
for (int piece = Pawn; piece <= King; piece++) {
RemovePiece(piece, lastMove.moveTo, !lastMove.capturedColor);
}
} else if (moveType >= knightPromoCapture) {
for (int piece = Pawn; piece <= King; piece++) {
RemovePiece(piece, lastMove.moveTo, !lastMove.capturedColor);
}
SetPiece(lastMove.capturedPiece, lastMove.moveTo, lastMove.capturedColor);
}
GenAttackMaps(*this);
castlingRights = lastMove.castlingRights;
if (halfMoves & 2 == 0) fullMoves--;
halfMoves--;
sideToMove = !lastMove.capturedColor;
}
-3
View File
@@ -23,8 +23,6 @@ public:
int halfMoves = 0;
int fullMoves = 0;
LastMove lastMove;
std::array<bool, 4> castlingRights = { false, false, false, false };
int enPassantTarget;
@@ -48,5 +46,4 @@ public:
void Promote(int square, int pieceType, int color, bool isCapture);
void MakeMove(Move move);
void UnmakeMove();
};
-2
View File
@@ -10,7 +10,5 @@ int main() {
Board board;
Perft(board, 2);
return 0;
}
+3 -49
View File
@@ -1,61 +1,15 @@
#pragma once
#include <array>
enum MOVE_FLAGS {
quiet, doublePawnPush,
kingCastle, queenCastle,
capture, epCapture,
knightPromotion, bishopPromotion, rookPromotion, queenPromotion,
knightPromoCapture, bishopPromoCapture, rookPromoCapture, queenPromoCapture
};
constexpr std::array<const char*, 14> moveTypes {
"quiet", "doublePawnPush",
"kingCastle", "QueenCastle",
"capture", "epCapture",
"knightPromotion", "bishopPromotion", "rookPromotion", "queenPromotion",
"knightPromoCapture", "bishopPromoCapture", "rookPromoCapture", "queenPromoCapture"
};
struct Move {
public:
int moveFrom;
int moveTo;
uint16_t moveFlags;
Move(){}
Move(const int from, const int to, const uint16_t flags)
: moveFrom(from), moveTo(to), moveFlags(flags) {}
void PrintMove() {
std::cout << squareCoords[moveFrom] << squareCoords[moveTo];
}
};
struct LastMove {
public:
int moveFrom;
int moveTo;
uint16_t moveFlags;
int attackerPiece;
int capturedPiece;
int capturedColor;
int enPassantTarget;
std::array<bool, 4> castlingRights;
LastMove(){}
LastMove(const Move move, const int capturedPiece,
const int capturedColor, const int enPassantTarget,
const std::array<bool, 4> castlingRights, int attackerPiece) {
moveFrom = move.moveFrom;
moveTo = move.moveTo;
moveFlags = move.moveFlags;
LastMove::capturedPiece = capturedPiece;
LastMove::capturedColor = capturedColor;
LastMove::enPassantTarget = enPassantTarget;
LastMove::castlingRights = castlingRights;
LastMove::attackerPiece = attackerPiece;
}
};
+3 -233
View File
@@ -270,231 +270,27 @@ Bitboard getQueenAttack(int square, U64 occupancy) {
}
static void GenPawnMoves(Board &board, bool color) {
Bitboard pawns = board.colors[color] & board.pieces[Pawn];
while (pawns.GetBoard()) {
int square = pawns.getLS1BIndex();
int direction = color ? south : north;
int lastRank = color ? r_1 : r_8;
Bitboard attacks = pawnAttacks[color][square];
// Checking for possible en passant
if (board.enPassantTarget) {
if ((attacks & Bitboard::GetSquare(board.enPassantTarget)).GetBoard()
&& board.sideToMove == color) {
board.AddMove(Move(square, board.enPassantTarget, epCapture));
}
}
attacks &= board.colors[!color];
Bitboard firstAhead = Bitboard::GetSquare(square + direction) & ~board.occupied;
Bitboard secondAhead = Bitboard::GetSquare(square + 2 * direction) & ~board.occupied;
if ((attacks & ranks[lastRank]).GetBoard()) {
while (attacks.GetBoard()) {
int targetSquare = attacks.getLS1BIndex();
for (int type = knightPromoCapture; type <= queenPromoCapture; type++) {
board.AddMove(Move(square, targetSquare, type));
}
attacks.PopBit(targetSquare);
}
} else {
while (attacks.GetBoard()) {
int targetSquare = attacks.getLS1BIndex();
board.AddMove(Move(square, targetSquare, capture));
attacks.PopBit(targetSquare);
}
}
if (firstAhead.GetBoard()) {
int secondRank = color ? r_7 : r_2;
if ((firstAhead & ranks[lastRank]).GetBoard()) {
for (int type = knightPromotion; type <= queenPromotion; type++) {
board.AddMove(Move(square, firstAhead.getLS1BIndex(), type));
}
} else {
board.AddMove(Move(square, firstAhead.getLS1BIndex(), quiet));
if (secondAhead.GetBoard() && Bitboard(ranks[secondRank]).IsSet(square)) {
board.AddMove(Move(square, secondAhead.getLS1BIndex(), doublePawnPush));
}
}
}
pawns.PopBit(square);
}
}
static void GenKnightMoves(Board &board, bool color) {
Bitboard knights = board.colors[color] & board.pieces[Knight];
while (knights.GetBoard()) {
int square = knights.getLS1BIndex();
Bitboard attacks = knightAttacks[square] & ~board.colors[color];
Bitboard captures = attacks & board.colors[!color];
attacks &= ~captures;
while (attacks.GetBoard()) {
int targetSquare = attacks.getLS1BIndex();
board.AddMove(Move(square, targetSquare, quiet));
attacks.PopBit(targetSquare);
}
while (captures.GetBoard()) {
int targetSquare = captures.getLS1BIndex();
board.AddMove(Move(square, targetSquare, capture));
captures.PopBit(targetSquare);
}
knights.PopBit(square);
}
}
static void GenRookMoves(Board &board, bool color) {
Bitboard rooks = board.colors[color] & board.pieces[Rook];
while (rooks.GetBoard()) {
int square = rooks.getLS1BIndex();
Bitboard attacks = getRookAttack(square, board.occupied.GetBoard()) & ~board.colors[color];
Bitboard captures = attacks & board.colors[!color];
attacks &= ~captures;
while (attacks.GetBoard()) {
int targetSquare = attacks.getLS1BIndex();
board.AddMove(Move(square, targetSquare, quiet));
attacks.PopBit(targetSquare);
}
while (captures.GetBoard()) {
int targetSquare = captures.getLS1BIndex();
board.AddMove(Move(square, targetSquare, capture));
captures.PopBit(targetSquare);
}
rooks.PopBit(square);
}
}
static void GenBishopMoves(Board &board, bool color) {
Bitboard bishops = board.colors[color] & board.pieces[Bishop];
while (bishops.GetBoard()) {
int square = bishops.getLS1BIndex();
Bitboard attacks = getBishopAttack(square, board.occupied.GetBoard()) & ~board.colors[color];
Bitboard captures = attacks & board.colors[!color];
attacks &= ~captures;
while (attacks.GetBoard()) {
int targetSquare = attacks.getLS1BIndex();
board.AddMove(Move(square, targetSquare, quiet));
attacks.PopBit(targetSquare);
}
while (captures.GetBoard()) {
int targetSquare = captures.getLS1BIndex();
board.AddMove(Move(square, targetSquare, capture));
captures.PopBit(targetSquare);
}
bishops.PopBit(square);
}
}
static void GenQueenMoves(Board &board, bool color) {
Bitboard queens = board.colors[color] & board.pieces[Queen];
while (queens.GetBoard()) {
int square = queens.getLS1BIndex();
Bitboard attacks = getQueenAttack(square, board.occupied.GetBoard()) & ~board.colors[color];
Bitboard captures = attacks & board.colors[!color];
attacks &= ~captures;
while (attacks.GetBoard()) {
int targetSquare = attacks.getLS1BIndex();
board.AddMove(Move(square, targetSquare, quiet));
attacks.PopBit(targetSquare);
}
while (captures.GetBoard()) {
int targetSquare = captures.getLS1BIndex();
board.AddMove(Move(square, targetSquare, capture));
captures.PopBit(targetSquare);
}
queens.PopBit(square);
}
}
static void GenKingMoves(Board &board, bool color) {
Bitboard king = board.colors[color] & board.pieces[King];
int square = king.getLS1BIndex();
Bitboard attacks = kingAttacks[square] & ~board.colors[color];
Bitboard captures = attacks & board.colors[!color];
attacks &= ~captures;
while (attacks.GetBoard()) {
int targetSquare = attacks.getLS1BIndex();
board.AddMove(Move(square, targetSquare, quiet));
attacks.PopBit(targetSquare);
}
while (captures.GetBoard()) {
int targetSquare = captures.getLS1BIndex();
board.AddMove(Move(square, targetSquare, capture));
captures.PopBit(targetSquare);
}
if (board.castlingRights[color * 2 + 1]) {
U64 QueenSide = color ? 0x1f00000000000000 : 0x1f;
U64 mask = color ? 0x1c00000000000000 : 0x1c;
int targetSquare = color ? c8 : c1;
if ((Bitboard(QueenSide) & board.occupied).popCount() == 2
&& !(mask & board.GetAttackMaps(!color))) {
board.AddMove(Move(square, targetSquare, queenCastle));
}
}
if (board.castlingRights[color * 2]) {
U64 KingSide = color ? 0xf000000000000000 : 0xf0;
U64 mask = color ? 0x7000000000000000 : 0x70;
int targetSquare = color ? g8 : g1;
if ((Bitboard(KingSide) & board.occupied).popCount() == 2
&& !(mask & board.GetAttackMaps(!color))) {
board.AddMove(Move(square, targetSquare, kingCastle));
}
}
}
void GenAttackMaps(Board &board) {
@@ -524,27 +320,9 @@ void GenAttackMaps(Board &board) {
}
}
static bool isLegalMove(Board &board, Move move) {
if (board.InCheck(board.sideToMove)) {
if (move.moveFlags == kingCastle || move.moveFlags == queenCastle) {
return false;
}
board.MakeMove(move);
if (board.InCheck(!board.sideToMove)) {
board.UnmakeMove();
return false;
}
board.UnmakeMove();
} else {
board.MakeMove(move);
if (board.InCheck(!board.sideToMove)) {
board.UnmakeMove();
return false;
}
board.UnmakeMove();
}
return true;
}
// static bool isLegalMove(Board &board, Move move) {
// }
void GenerateMoves(Board &board, bool side) {
board.moveList.clear();
@@ -560,12 +338,4 @@ void GenerateMoves(Board &board, bool side) {
GenQueenMoves(board, side);
GenKingMoves(board, side);
int listSize = board.moveList.size();
// Filtering illegal moves
for (int i = listSize - 1; i >= 0; --i) {
if (!isLegalMove(board, board.moveList[i])) {
board.moveList.erase(board.moveList.begin() + i);
}
}
}
+35 -35
View File
@@ -1,45 +1,45 @@
#include <chrono>
#include "perft.h"
// #include <chrono>
// #include "perft.h"
U64 HelperPerft(Board board, int depth) {
GenerateMoves(board, board.sideToMove);
// U64 HelperPerft(Board board, int depth) {
// GenerateMoves(board, board.sideToMove);
if (depth == 1) return board.moveList.size();
if (depth == 0) return 1ULL;
// if (depth == 1) return board.moveList.size();
// if (depth == 0) return 1ULL;
U64 nodes = 0;
// U64 nodes = 0;
for (int i = 0; i < board.moveList.size(); i++) {
board.MakeMove(board.moveList[i]);
nodes += HelperPerft(board, depth - 1);
board.UnmakeMove();
}
return nodes;
}
// for (int i = 0; i < board.moveList.size(); i++) {
// board.MakeMove(board.moveList[i]);
// nodes += HelperPerft(board, depth - 1);
// board.UnmakeMove();
// }
// return nodes;
// }
void Perft(Board &board, int depth) {
GenerateMoves(board, board.sideToMove);
// void Perft(Board &board, int depth) {
// GenerateMoves(board, board.sideToMove);
U64 totalNodes = 0;
// U64 totalNodes = 0;
for (int i = 0; i < board.moveList.size(); i++) {
board.MakeMove(board.moveList[i]);
U64 nodeCount = HelperPerft(board, depth - 1);
totalNodes += nodeCount;
board.moveList[i].PrintMove();
board.UnmakeMove();
std::cout << ": " << nodeCount << '\n';
}
// for (int i = 0; i < board.moveList.size(); i++) {
// board.MakeMove(board.moveList[i]);
// U64 nodeCount = HelperPerft(board, depth - 1);
// totalNodes += nodeCount;
// board.moveList[i].PrintMove();
// board.UnmakeMove();
// std::cout << ": " << nodeCount << '\n';
// }
auto start = std::chrono::steady_clock::now();
U64 nodes = HelperPerft(board, depth);
auto finish = std::chrono::steady_clock::now();
// auto start = std::chrono::steady_clock::now();
// U64 nodes = HelperPerft(board, depth);
// auto finish = std::chrono::steady_clock::now();
double elapsed_seconds = std::chrono::duration_cast<
std::chrono::duration<double>>(finish - start).count();
// double elapsed_seconds = std::chrono::duration_cast<
// std::chrono::duration<double>>(finish - start).count();
std::cout << "\nDepth: " << depth << '\n';
std::cout << "Total nodes: " << totalNodes << '\n';
std::cout << "Time took: " << elapsed_seconds << "s (";
std::cout << nodes / elapsed_seconds << " nodes/sec)\n";
}
// std::cout << "\nDepth: " << depth << '\n';
// std::cout << "Total nodes: " << totalNodes << '\n';
// std::cout << "Time took: " << elapsed_seconds << "s (";
// std::cout << nodes / elapsed_seconds << " nodes/sec)\n";
// }