Added perft function + fixed some move generation bugs(there are still some)

This commit is contained in:
rektdie committed 2024-04-01 22:04:43 +02:00
1 parent 915b93ad5a
commit 5962c37546
6 files changed
+98 -43

No files matched your search

+3 -3
View File
@@ -394,6 +394,9 @@ void Board::UnmakeMove() {
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);
@@ -431,9 +434,6 @@ void Board::UnmakeMove() {
SetPiece(lastMove.capturedPiece, lastMove.moveTo, lastMove.capturedColor);
}
// Removing attacker piece from new square
RemovePiece(lastMove.attackerPiece, lastMove.moveTo, !lastMove.capturedColor);
castlingRights = lastMove.castlingRights;
if (halfMoves & 2 == 0) fullMoves--;
halfMoves--;
+2 -3
View File
@@ -2,15 +2,14 @@
#include "types.h"
#include "board.h"
#include "movegen.h"
#include "perft.h"
int main() {
initLeaperAttacks();
initSliderAttacks();
Board board;
GenerateMoves(board);
board.ListMoves();
board.PrintBoard();
Perft(board, 4);
return 0;
}
+30 -36
View File
@@ -314,24 +314,14 @@ static void GenPawnMoves(Board &board, bool color) {
}
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 (color == White) {
if (Bitboard::GetSquare(firstAhead.getLS1BIndex() & RANKS::r_2).GetBoard()) {
if ((secondAhead).GetBoard()) {
board.AddMove(Move(square, secondAhead.getLS1BIndex(), doublePawnPush));
}
}
} else {
if (Bitboard::GetSquare(firstAhead.getLS1BIndex() & RANKS::r_7).GetBoard()) {
if ((secondAhead).GetBoard()) {
if (secondAhead.GetBoard() && Bitboard(ranks[secondRank]).IsSet(square)) {
board.AddMove(Move(square, secondAhead.getLS1BIndex(), doublePawnPush));
}
}
@@ -503,44 +493,48 @@ static void GenKingMoves(Board &board, bool color) {
}
}
bool isLegalMove(Board &board, Move move) {
board.MakeMove(move);
if (board.InCheck(!board.sideToMove)) {
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 false;
}
board.UnmakeMove();
return true;
}
void GenerateMoves(Board &board) {
void GenerateMoves(Board &board, bool side) {
board.moveList.clear();
GenPawnMoves(board, White);
GenPawnMoves(board, Black);
GenPawnMoves(board, side);
GenKnightMoves(board, White);
GenKnightMoves(board, Black);
GenKnightMoves(board, side);
GenRookMoves(board, White);
GenRookMoves(board, Black);
GenRookMoves(board, side);
GenBishopMoves(board, White);
GenBishopMoves(board, Black);
GenBishopMoves(board, side);
GenQueenMoves(board, White);
GenQueenMoves(board, Black);
GenQueenMoves(board, side);
GenKingMoves(board, White);
GenKingMoves(board, Black);
GenKingMoves(board, side);
int listSize = sizeof(board.moveList);
int listSize = board.moveList.size();
// Filtering illegal moves
for (int i = 0; i < listSize; i++) {
int removedCount = 0;
for (int i = listSize - 1; i >= 0; --i) {
if (!isLegalMove(board, board.moveList[i])) {
board.moveList.erase(board.moveList.begin() + i - removedCount);
removedCount++;
board.moveList.erase(board.moveList.begin() + i);
}
}
}
}
+1 -1
View File
@@ -204,4 +204,4 @@ Bitboard getBishopAttack(int square, U64 occupancy);
Bitboard getRookAttack(int square, U64 occupancy);
Bitboard getQueenAttack(int square, U64 occupancy);
void GenerateMoves(Board &board);
void GenerateMoves(Board &board, bool side);
+57
View File
@@ -0,0 +1,57 @@
#include <chrono>
#include "perft.h"
static int captures = 0;
static int epCaptures = 0;
static int castles = 0;
static int promotions = 0;
static int checks = 0;
U64 HelperPerft(Board board, int depth) {
if (depth == 0) return 1ULL;
GenerateMoves(board, board.sideToMove);
U64 nodes = 0;
for (int i = 0; i < board.moveList.size(); i++) {
if (board.moveList[i].moveFlags == capture) captures++;
if (board.moveList[i].moveFlags == epCapture) epCaptures++;
if (board.moveList[i].moveFlags == kingCastle || board.moveList[i].moveFlags == queenCastle) castles++;
if (board.moveList[i].moveFlags >= knightPromotion) promotions++;
board.MakeMove(board.moveList[i]);
if (board.InCheck(board.sideToMove)) checks++;
nodes += HelperPerft(board, depth - 1);
board.UnmakeMove();
}
return nodes;
}
void Perft(Board &board, int depth) {
using std::chrono::high_resolution_clock;
using std::chrono::milliseconds;
using std::chrono::duration_cast;
captures = 0;
epCaptures = 0;
castles = 0;
promotions = 0;
checks = 0;
auto start = high_resolution_clock::now();
U64 nodes = HelperPerft(board, depth);
auto end = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(end - start);
std::cout << "Depth: " << depth << '\n';
std::cout << "Nodes searched: " << nodes << '\n';
std::cout << "Captures: " << captures << '\n';
std::cout << "epCaptures: " << epCaptures << '\n';
std::cout << "Castles: " << castles << '\n';
std::cout << "Promotions: " << promotions << '\n';
std::cout << "Checks: " << checks << '\n' << '\n';
std::cout << "Total time taken: " << duration.count() << " ms";
std::cout << " (" << (nodes / duration.count()) * 1000 << " nodes/sec)\n";
}
+5
View File
@@ -0,0 +1,5 @@
#pragma once
#include "board.h"
#include "movegen.h"
void Perft(Board &board, int depth);