using fixed size array instead of vector to store moves

This commit is contained in:
rektdie committed 2024-06-19 02:30:28 +02:00
1 parent d566c6d0e4
commit 6e8f7e3d86
5 files changed
+19 -14

No files matched your search

+9 -5
View File
@@ -16,6 +16,9 @@ void Board::Reset() {
pieces = std::array<Bitboard, 6>();
colors = std::array<Bitboard, 2>();
moveList = std::array<Move, 218>();
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;
+3 -3
View File
@@ -3,7 +3,6 @@
#include "bitboards.h"
#include "types.h"
#include "move.h"
#include <vector>
class Board {
public:
@@ -17,7 +16,8 @@ public:
Bitboard occupied;
std::vector<Move> moveList;
std::array<Move, 218> 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);
};
};
+2 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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);
+4 -4
View File
@@ -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";
}
}