From 494cd13f7ec054c7c05aa29117bbd4f07db9f07e Mon Sep 17 00:00:00 2001 From: "rektdie@gmail.com" Date: Tue, 9 Apr 2024 21:44:33 +0200 Subject: [PATCH] added perft back + fixed some bugs with movegen --- source/board.cpp | 8 +++-- source/main.cpp | 4 +-- source/movegen.cpp | 27 ++++++++++------- source/perft.cpp | 73 +++++++++++++++++++++++----------------------- source/types.h | 2 +- 5 files changed, 61 insertions(+), 53 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index bf8714c..a87c441 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -113,10 +113,14 @@ void Board::SetByFen(const char* fen) { section++; continue; case 'w': - sideToMove = White; + if (section == 0) { + sideToMove = White; + } break; case 'b': - sideToMove = Black; + if (section == 0) { + sideToMove = Black; + } break; case 'K': castlingRights[White] = true; diff --git a/source/main.cpp b/source/main.cpp index f0ac42d..d8b68ef 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -9,9 +9,7 @@ int main() { initSliderAttacks(); Board board; - board.PrintBoard(); - GenerateMoves(board, board.sideToMove); - board.ListMoves(); + Perft(board, 4); return 0; } \ No newline at end of file diff --git a/source/movegen.cpp b/source/movegen.cpp index 0e5a3b1..e0732db 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -318,21 +318,21 @@ static Bitboard Checkers(Board &board, bool color) { static int GetDirection(int square, int target) { int difference = target - square; - if (difference % north == 0) { + if (difference - north == 0) { return north; - } else if (difference % south == 0) { + } else if (difference - south == 0) { return south; - } else if (difference % noWe == 0) { + } else if (difference - noWe == 0) { return noWe; - } else if (difference % noEa == 0) { + } else if (difference - noEa == 0) { return noEa; - } else if (difference % soWe == 0) { + } else if (difference - soWe == 0) { return soWe; - } else if (difference % soEa == 0) { + } else if (difference - soEa == 0) { return soEa; - } else if (difference < 0 && difference > -8) { + } else if (ranks[square / 8] == ranks[target / 8] && target < square) { return west; - } else if (difference > 0 && difference < 8) { + } else if (ranks[square / 8] == ranks[target / 8] && target > square) { return east; } @@ -344,6 +344,7 @@ static Bitboard SliderRaysToSquare(int attacker, int target) { Bitboard ray; int direction = GetDirection(attacker, target); + attacker += direction; if (direction) { while (attacker != target) { @@ -358,7 +359,13 @@ static Bitboard GetPinningRay(Board &board, int square, int direction) { Bitboard ray; if (direction) { + int previousRank = (square - direction) / 7; while (square < 64 && square >= 0) { + int currentRank = square / 7; + + if (abs(currentRank - previousRank) > 1) break; + previousRank = currentRank; + ray.SetBit(square); if (board.occupied.IsSet(square)) { int attackerColor = board.GetPieceColor(square); @@ -386,7 +393,6 @@ static bool IsPinned(Board &board, int square, int color) { } int currentSquare = square - directionToKing; - return GetPinningRay(board, currentSquare, -directionToKing).GetBoard(); } @@ -811,8 +817,7 @@ static void GenCheckEvasions(Board &board, bool color) { int checkerType = board.GetPieceType(checkerSquare); if (checkerType == Rook || checkerType == Bishop || checkerType == Queen) { - pushMask = SliderRaysToSquare(checkerSquare, kingSquare) - & ~(Bitboard::GetSquare(kingSquare) | Bitboard::GetSquare(checkerSquare)); + pushMask = SliderRaysToSquare(checkerSquare, kingSquare); } // looping through own pieces to add captures and blocks diff --git a/source/perft.cpp b/source/perft.cpp index 398dea3..c32fe53 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -1,45 +1,46 @@ -// #include -// #include "perft.h" +#include +#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 copy = board; + board.MakeMove(board.moveList[i]); + nodes += HelperPerft(board, depth - 1); + board = copy; + } + 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'; -// } + auto start = std::chrono::steady_clock::now(); + for (int i = 0; i < board.moveList.size(); i++) { + Board copy = board; + board.MakeMove(board.moveList[i]); + U64 nodeCount = HelperPerft(board, depth - 1); + board = copy; + totalNodes += nodeCount; + std::cout << squareCoords[board.moveList[i].MoveFrom()] + << squareCoords[board.moveList[i].MoveTo()]; + std::cout << ": " << nodeCount << '\n'; + } + 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>(finish - start).count(); -// double elapsed_seconds = std::chrono::duration_cast< -// std::chrono::duration>(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"; -// } \ No newline at end of file + std::cout << "\nDepth: " << depth << '\n'; + std::cout << "Total nodes: " << totalNodes << '\n'; + std::cout << "Time took: " << elapsed_seconds << "s ("; + std::cout << int(totalNodes / elapsed_seconds * 1000) << " nodes/sec)\n"; +} \ No newline at end of file diff --git a/source/types.h b/source/types.h index a728283..7b1cd65 100644 --- a/source/types.h +++ b/source/types.h @@ -4,7 +4,7 @@ using U64 = uint64_t; -constexpr char StartingFen[] = "8/8/8/8/k2Pp2Q/8/8/3K4 b - d3 0 1"; +constexpr char StartingFen[] = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; enum COLOR { White,