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>(); pieces = std::array<Bitboard, 6>();
colors = std::array<Bitboard, 2>(); colors = std::array<Bitboard, 2>();
moveList = std::array<Move, 218>();
currentMoveIndex = 0;
} }
void Board::SetByFen(const char* fen) { void Board::SetByFen(const char* fen) {
@@ -237,18 +240,19 @@ void Board::PrintBoard() {
} }
void Board::AddMove(Move move) { void Board::AddMove(Move move) {
moveList.push_back(move); moveList[currentMoveIndex] = move;
currentMoveIndex++;
} }
void Board::ResetMoves() { void Board::ResetMoves() {
moveList.clear(); currentMoveIndex = 0;
} }
void Board::ListMoves() { void Board::ListMoves() {
int count = 1; int count = 1;
for (Move move : moveList) { for (int i = 0; i < currentMoveIndex; i++) {
std::cout << count << ". "; std::cout << count << ". ";
move.PrintMove(); moveList[i].PrintMove();
count++; count++;
} }
} }
@@ -408,7 +412,7 @@ void Board::MakeMove(Move move) {
UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor); UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor);
halfMoves++; halfMoves++;
if (halfMoves & 2 == 0) fullMoves++; if (halfMoves % 2 == 0) fullMoves++;
sideToMove = !attackerColor; sideToMove = !attackerColor;
enPassantTarget = newEpTarget; enPassantTarget = newEpTarget;
+3 -3
View File
@@ -3,7 +3,6 @@
#include "bitboards.h" #include "bitboards.h"
#include "types.h" #include "types.h"
#include "move.h" #include "move.h"
#include <vector>
class Board { class Board {
public: public:
@@ -17,7 +16,8 @@ public:
Bitboard occupied; Bitboard occupied;
std::vector<Move> moveList; std::array<Move, 218> moveList;
int currentMoveIndex = 0;
bool sideToMove = White; bool sideToMove = White;
int halfMoves = 0; int halfMoves = 0;
@@ -46,4 +46,4 @@ public:
void Promote(int square, int pieceType, int color, bool isCapture); void Promote(int square, int pieceType, int color, bool isCapture);
void MakeMove(Move move); void MakeMove(Move move);
}; };
+2 -1
View File
@@ -1,6 +1,7 @@
#include "board.h" #include "board.h"
#include "movegen.h" #include "movegen.h"
#include "uci.h" #include "uci.h"
#include "perft.h"
int main() { int main() {
initLeaperAttacks(); initLeaperAttacks();
@@ -8,7 +9,7 @@ int main() {
Board board; Board board;
UCILoop(board); Perft(board, 7);
return 0; return 0;
} }
+1 -1
View File
@@ -973,7 +973,7 @@ void GenAttackMaps(Board &board) {
} }
void GenerateMoves(Board &board, bool side) { void GenerateMoves(Board &board, bool side) {
board.moveList.clear(); board.ResetMoves();
if (board.InCheck(side)) { if (board.InCheck(side)) {
GenCheckEvasions(board, side); GenCheckEvasions(board, side);
+4 -4
View File
@@ -4,12 +4,12 @@
U64 HelperPerft(Board board, int depth) { U64 HelperPerft(Board board, int depth) {
GenerateMoves(board, board.sideToMove); GenerateMoves(board, board.sideToMove);
if (depth == 1) return board.moveList.size(); if (depth == 1) return board.currentMoveIndex;
if (depth == 0) return 1ULL; if (depth == 0) return 1ULL;
U64 nodes = 0; U64 nodes = 0;
for (int i = 0; i < board.moveList.size(); i++) { for (int i = 0; i < board.currentMoveIndex; i++) {
Board copy = board; Board copy = board;
board.MakeMove(board.moveList[i]); board.MakeMove(board.moveList[i]);
nodes += HelperPerft(board, depth - 1); nodes += HelperPerft(board, depth - 1);
@@ -24,7 +24,7 @@ void Perft(Board &board, int depth) {
U64 totalNodes = 0; U64 totalNodes = 0;
auto start = std::chrono::steady_clock::now(); 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 copy = board;
board.MakeMove(board.moveList[i]); board.MakeMove(board.moveList[i]);
U64 nodeCount = HelperPerft(board, depth - 1); U64 nodeCount = HelperPerft(board, depth - 1);
@@ -56,4 +56,4 @@ void Perft(Board &board, int depth) {
std::cout << "Total nodes: " << totalNodes << '\n'; std::cout << "Total nodes: " << totalNodes << '\n';
std::cout << "Time took: " << elapsed_seconds << "s ("; std::cout << "Time took: " << elapsed_seconds << "s (";
std::cout << int(totalNodes / elapsed_seconds) << " nodes/sec)\n"; std::cout << int(totalNodes / elapsed_seconds) << " nodes/sec)\n";
} }