From 5962c37546d85f06544cb7dcea95993d50cbe44a Mon Sep 17 00:00:00 2001 From: "rektdie@gmail.com" Date: Mon, 1 Apr 2024 22:04:43 +0200 Subject: [PATCH] Added perft function + fixed some move generation bugs(there are still some) --- source/board.cpp | 6 ++--- source/main.cpp | 5 ++-- source/movegen.cpp | 66 +++++++++++++++++++++------------------------- source/movegen.h | 2 +- source/perft.cpp | 57 +++++++++++++++++++++++++++++++++++++++ source/perft.h | 5 ++++ 6 files changed, 98 insertions(+), 43 deletions(-) create mode 100644 source/perft.cpp create mode 100644 source/perft.h diff --git a/source/board.cpp b/source/board.cpp index bd4d450..fea3ce7 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -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--; diff --git a/source/main.cpp b/source/main.cpp index 6d9c1c1..d8b68ef 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -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; } \ No newline at end of file diff --git a/source/movegen.cpp b/source/movegen.cpp index 6a3444b..3365100 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -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); } - } + } } \ No newline at end of file diff --git a/source/movegen.h b/source/movegen.h index 38de265..f1e0999 100644 --- a/source/movegen.h +++ b/source/movegen.h @@ -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); \ No newline at end of file +void GenerateMoves(Board &board, bool side); \ No newline at end of file diff --git a/source/perft.cpp b/source/perft.cpp new file mode 100644 index 0000000..04dd4c4 --- /dev/null +++ b/source/perft.cpp @@ -0,0 +1,57 @@ +#include +#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(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"; +} \ No newline at end of file diff --git a/source/perft.h b/source/perft.h new file mode 100644 index 0000000..fe787c6 --- /dev/null +++ b/source/perft.h @@ -0,0 +1,5 @@ +#pragma once +#include "board.h" +#include "movegen.h" + +void Perft(Board &board, int depth); \ No newline at end of file