From 6e8f7e3d863d61e09677c3edebc2c5a75dc50bc5 Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 19 Jun 2024 02:30:28 +0200 Subject: [PATCH] using fixed size array instead of vector to store moves --- source/board.cpp | 14 +++++++++----- source/board.h | 6 +++--- source/main.cpp | 3 ++- source/movegen.cpp | 2 +- source/perft.cpp | 8 ++++---- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index 84c9025..15ef005 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -16,6 +16,9 @@ void Board::Reset() { pieces = std::array(); colors = std::array(); + + moveList = std::array(); + currentMoveIndex = 0; } void Board::SetByFen(const char* fen) { @@ -237,18 +240,19 @@ void Board::PrintBoard() { } void Board::AddMove(Move move) { - moveList.push_back(move); + moveList[currentMoveIndex] = move; + currentMoveIndex++; } void Board::ResetMoves() { - moveList.clear(); + currentMoveIndex = 0; } void Board::ListMoves() { int count = 1; - for (Move move : moveList) { + for (int i = 0; i < currentMoveIndex; i++) { std::cout << count << ". "; - move.PrintMove(); + moveList[i].PrintMove(); count++; } } @@ -408,7 +412,7 @@ void Board::MakeMove(Move move) { UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor); halfMoves++; - if (halfMoves & 2 == 0) fullMoves++; + if (halfMoves % 2 == 0) fullMoves++; sideToMove = !attackerColor; enPassantTarget = newEpTarget; diff --git a/source/board.h b/source/board.h index 12af5c9..aecf557 100644 --- a/source/board.h +++ b/source/board.h @@ -3,7 +3,6 @@ #include "bitboards.h" #include "types.h" #include "move.h" -#include class Board { public: @@ -17,7 +16,8 @@ public: Bitboard occupied; - std::vector moveList; + std::array moveList; + int currentMoveIndex = 0; bool sideToMove = White; int halfMoves = 0; @@ -46,4 +46,4 @@ public: void Promote(int square, int pieceType, int color, bool isCapture); void MakeMove(Move move); -}; \ No newline at end of file +}; diff --git a/source/main.cpp b/source/main.cpp index 4505175..245fe21 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,6 +1,7 @@ #include "board.h" #include "movegen.h" #include "uci.h" +#include "perft.h" int main() { initLeaperAttacks(); @@ -8,7 +9,7 @@ int main() { Board board; - UCILoop(board); + Perft(board, 7); return 0; } diff --git a/source/movegen.cpp b/source/movegen.cpp index 10fdae7..3a9f4dc 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -973,7 +973,7 @@ void GenAttackMaps(Board &board) { } void GenerateMoves(Board &board, bool side) { - board.moveList.clear(); + board.ResetMoves(); if (board.InCheck(side)) { GenCheckEvasions(board, side); diff --git a/source/perft.cpp b/source/perft.cpp index 4cd6265..bf0f3e1 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -4,12 +4,12 @@ U64 HelperPerft(Board board, int depth) { GenerateMoves(board, board.sideToMove); - if (depth == 1) return board.moveList.size(); + if (depth == 1) return board.currentMoveIndex; if (depth == 0) return 1ULL; U64 nodes = 0; - for (int i = 0; i < board.moveList.size(); i++) { + for (int i = 0; i < board.currentMoveIndex; i++) { Board copy = board; board.MakeMove(board.moveList[i]); nodes += HelperPerft(board, depth - 1); @@ -24,7 +24,7 @@ void Perft(Board &board, int depth) { U64 totalNodes = 0; auto start = std::chrono::steady_clock::now(); - for (int i = 0; i < board.moveList.size(); i++) { + for (int i = 0; i < board.currentMoveIndex; i++) { Board copy = board; board.MakeMove(board.moveList[i]); U64 nodeCount = HelperPerft(board, depth - 1); @@ -56,4 +56,4 @@ void Perft(Board &board, int depth) { std::cout << "Total nodes: " << totalNodes << '\n'; std::cout << "Time took: " << elapsed_seconds << "s ("; std::cout << int(totalNodes / elapsed_seconds) << " nodes/sec)\n"; -} \ No newline at end of file +}