Refactor #26

Merged
rektdie merged 11 commits from refactor into main 2025-03-21 10:52:24 +01:00
7 changed files with 110 additions and 111 deletions
Showing only changes of commit 0ffc2c77de - Show all commits

No files matched your search

+16 -16
View File
@@ -29,8 +29,8 @@ void Board::SetByFen(std::string_view fen) {
// Starting from top left // Starting from top left
int currSquare = a8; int currSquare = a8;
std::vector<std::string> tokens = split(fen, ' '); std::vector<std::string> tokens = UTILS::split(fen, ' ');
std::vector<std::string> pieceTokens = split(tokens[0], '/'); std::vector<std::string> pieceTokens = UTILS::split(tokens[0], '/');
constexpr std::string_view pieceTypes = "pnbrqk"; constexpr std::string_view pieceTypes = "pnbrqk";
@@ -61,10 +61,10 @@ void Board::SetByFen(std::string_view fen) {
else if (piece == 'q') castlingRights |= blackQueenRight; else if (piece == 'q') castlingRights |= blackQueenRight;
} }
if (tokens[3] != "-") enPassantTarget = parseSquare(tokens[3]); if (tokens[3] != "-") enPassantTarget = UTILS::parseSquare(tokens[3]);
occupied = colors[White] | colors[Black]; occupied = colors[White] | colors[Black];
hashKey = GetHashKey(*this); hashKey = UTILS::GetHashKey(*this);
MOVEGEN::GenerateMoves(*this); MOVEGEN::GenerateMoves(*this);
MOVEGEN::GenAttackMaps(*this); MOVEGEN::GenAttackMaps(*this);
} }
@@ -169,7 +169,7 @@ void Board::SetPiece(int piece, int square, bool color) {
colors[color].SetBit(square); colors[color].SetBit(square);
occupied.SetBit(square); occupied.SetBit(square);
hashKey ^= zKeys[color][piece][square]; hashKey ^= UTILS::zKeys[color][piece][square];
} }
void Board::RemovePiece(int piece, int square, bool color) { void Board::RemovePiece(int piece, int square, bool color) {
@@ -177,14 +177,14 @@ void Board::RemovePiece(int piece, int square, bool color) {
colors[color].PopBit(square); colors[color].PopBit(square);
occupied.PopBit(square); occupied.PopBit(square);
hashKey ^= zKeys[color][piece][square]; hashKey ^= UTILS::zKeys[color][piece][square];
} }
// Updates castling rights // Updates castling rights
static void UpdateCastlingRights(Board &board, int square, int type, int color) { static void UpdateCastlingRights(Board &board, int square, int type, int color) {
if (type == Rook) { if (type == Rook) {
// Removing old rights // Removing old rights
board.hashKey ^= zCastle[board.castlingRights]; board.hashKey ^= UTILS::zCastle[board.castlingRights];
int queenSideRook = color ? a8 : a1; int queenSideRook = color ? a8 : a1;
int kingSideRook = color ? h8 : h1; int kingSideRook = color ? h8 : h1;
@@ -194,15 +194,15 @@ static void UpdateCastlingRights(Board &board, int square, int type, int color)
board.castlingRights &= color ? ~blackKingRight : ~whiteKingRight; board.castlingRights &= color ? ~blackKingRight : ~whiteKingRight;
} }
board.hashKey ^= zCastle[board.castlingRights]; board.hashKey ^= UTILS::zCastle[board.castlingRights];
} else if (type == King) { } else if (type == King) {
// Removing old rights // Removing old rights
board.hashKey ^= zCastle[board.castlingRights]; board.hashKey ^= UTILS::zCastle[board.castlingRights];
board.castlingRights &= color ? ~blackKingRight : ~whiteKingRight; board.castlingRights &= color ? ~blackKingRight : ~whiteKingRight;
board.castlingRights &= color ? ~blackQueenRight : ~whiteQueenRight; board.castlingRights &= color ? ~blackQueenRight : ~whiteQueenRight;
board.hashKey ^= zCastle[board.castlingRights]; board.hashKey ^= UTILS::zCastle[board.castlingRights];
} }
} }
@@ -224,14 +224,14 @@ void Board::MakeMove(Move move) {
int newEpTarget = noEPTarget; int newEpTarget = noEPTarget;
sideToMove = !sideToMove; sideToMove = !sideToMove;
hashKey ^= zSide; hashKey ^= UTILS::zSide;
if (enPassantTarget != noEPTarget) { if (enPassantTarget != noEPTarget) {
hashKey ^= zEnPassant[enPassantTarget % 8]; hashKey ^= UTILS::zEnPassant[enPassantTarget % 8];
} }
if (newEpTarget != noEPTarget) { if (newEpTarget != noEPTarget) {
hashKey ^= zEnPassant[newEpTarget % 8]; hashKey ^= UTILS::zEnPassant[newEpTarget % 8];
} }
enPassantTarget = newEpTarget; enPassantTarget = newEpTarget;
@@ -328,14 +328,14 @@ void Board::MakeMove(Move move) {
UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor); UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor);
sideToMove = !attackerColor; sideToMove = !attackerColor;
hashKey ^= zSide; hashKey ^= UTILS::zSide;
if (enPassantTarget != noEPTarget) { if (enPassantTarget != noEPTarget) {
hashKey ^= zEnPassant[enPassantTarget % 8]; hashKey ^= UTILS::zEnPassant[enPassantTarget % 8];
} }
if (newEpTarget != noEPTarget) { if (newEpTarget != noEPTarget) {
hashKey ^= zEnPassant[newEpTarget % 8]; hashKey ^= UTILS::zEnPassant[newEpTarget % 8];
} }
enPassantTarget = newEpTarget; enPassantTarget = newEpTarget;
+2 -2
View File
@@ -1,13 +1,13 @@
#include "board.h" #include "board.h"
#include "movegen.h" #include "movegen.h"
#include "uci.h" #include "uci.h"
#include "tt.h" #include "utils.h"
#include "benchmark.h" #include "benchmark.h"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
MOVEGEN::initLeaperAttacks(); MOVEGEN::initLeaperAttacks();
MOVEGEN::initSliderAttacks(); MOVEGEN::initSliderAttacks();
InitZobrist(); UTILS::InitZobrist();
Board board; Board board;
-77
View File
@@ -1,82 +1,5 @@
#include "tt.h" #include "tt.h"
static U64 initialSeed = 0x60919C48E57863B9;
// 2[colors] * 6[pieces] * 64[squares]
U64 zKeys[2][6][64];
// There are 8 files
U64 zEnPassant[8];
// 16 possible castling right variations
U64 zCastle[16];
U64 zSide;
// PRNG using xorshift
static U64 RandomNum() {
initialSeed ^= initialSeed << 13;
initialSeed ^= initialSeed >> 7;
initialSeed ^= initialSeed << 17;
return initialSeed;
}
void InitZobrist() {
// init piece square keys
for (int i = White; i <= Black; i++) {
for (int j = Pawn; j <= King; j++) {
for (int k = a1; k <= h8; k++) {
zKeys[i][j][k] = RandomNum();
}
}
}
// init en passant file keys
for (int i = 0; i < 8; i++) {
zEnPassant[i] = RandomNum();
}
// init castling rights keys
for (int i = 0; i < 16; i++) {
zCastle[i] = RandomNum();
}
// init side key
zSide = RandomNum();
}
U64 GetHashKey(Board &board) {
U64 key = 0ULL;
// XORing pieces
for (int color = White; color <= Black; color++) {
for (int piece = Pawn; piece <= King; piece++) {
Bitboard pieceSet = board.pieces[piece] & board.colors[color];
while (pieceSet) {
int square = pieceSet.getLS1BIndex();
key ^= zKeys[color][piece][square];
pieceSet.PopBit(square);
}
}
}
// XORing en passant
if (board.enPassantTarget != -1) {
key ^= zEnPassant[board.enPassantTarget % 8];
}
// XORing castling rights
key ^= zCastle[board.castlingRights];
// XORing side to move
if (board.sideToMove) key ^= zSide;
return key;
}
SearchResults TTable::ReadEntry(U64 &hashKey, int depth, int alpha, int beta) { SearchResults TTable::ReadEntry(U64 &hashKey, int depth, int alpha, int beta) {
TTEntry *current = &table[hashKey % hashSize]; TTEntry *current = &table[hashKey % hashSize];
-14
View File
@@ -3,20 +3,6 @@
#include "search.h" #include "search.h"
#include <vector> #include <vector>
// 2[colors] * 6[pieces] * 64[squares]
extern U64 zKeys[2][6][64];
// There are 8 files
extern U64 zEnPassant[8];
// 16 possible castling right variations
extern U64 zCastle[16];
extern U64 zSide;
void InitZobrist();
U64 GetHashKey(Board &board);
class TTEntry { class TTEntry {
public: public:
U64 hashKey; U64 hashKey;
+2 -2
View File
@@ -29,10 +29,10 @@ void ParsePosition(Board &board, std::string_view command) {
} }
if (movesIndex != std::string::npos) { if (movesIndex != std::string::npos) {
std::vector<std::string> moves = split(command.substr(movesIndex + 6, command.length() - (movesIndex + 5)), ' '); std::vector<std::string> moves = UTILS::split(command.substr(movesIndex + 6, command.length() - (movesIndex + 5)), ' ');
for (std::string_view move : moves) { for (std::string_view move : moves) {
board.MakeMove(parseMove(board, move)); board.MakeMove(UTILS::parseMove(board, move));
} }
} }
+71
View File
@@ -1,6 +1,76 @@
#include "utils.h" #include "utils.h"
#include "movegen.h" #include "movegen.h"
namespace UTILS {
// Zobrist //
U64 initialSeed = 0x60919C48E57863B9;
// PRNG using xorshift
static U64 RandomNum() {
initialSeed ^= initialSeed << 13;
initialSeed ^= initialSeed >> 7;
initialSeed ^= initialSeed << 17;
return initialSeed;
}
void InitZobrist() {
// init piece square keys
for (int i = White; i <= Black; i++) {
for (int j = Pawn; j <= King; j++) {
for (int k = a1; k <= h8; k++) {
zKeys[i][j][k] = RandomNum();
}
}
}
// init en passant file keys
for (int i = 0; i < 8; i++) {
zEnPassant[i] = RandomNum();
}
// init castling rights keys
for (int i = 0; i < 16; i++) {
zCastle[i] = RandomNum();
}
// init side key
zSide = RandomNum();
}
U64 GetHashKey(Board &board) {
U64 key = 0ULL;
// XORing pieces
for (int color = White; color <= Black; color++) {
for (int piece = Pawn; piece <= King; piece++) {
Bitboard pieceSet = board.pieces[piece] & board.colors[color];
while (pieceSet) {
int square = pieceSet.getLS1BIndex();
key ^= zKeys[color][piece][square];
pieceSet.PopBit(square);
}
}
}
// XORing en passant
if (board.enPassantTarget != -1) {
key ^= zEnPassant[board.enPassantTarget % 8];
}
// XORing castling rights
key ^= zCastle[board.castlingRights];
// XORing side to move
if (board.sideToMove) key ^= zSide;
return key;
}
std::vector<std::string> split(std::string_view str, char delim) { std::vector<std::string> split(std::string_view str, char delim) {
std::vector<std::string> results; std::vector<std::string> results;
@@ -76,3 +146,4 @@ Move parseMove(Board &board, std::string_view str) {
return Move(from, to, flag); return Move(from, to, flag);
} }
}
+19
View File
@@ -6,8 +6,27 @@
#include "move.h" #include "move.h"
#include "board.h" #include "board.h"
namespace UTILS {
// Zobrist //
// 2[colors] * 6[pieces] * 64[squares]
inline U64 zKeys[2][6][64];
// There are 8 files
inline U64 zEnPassant[8];
// 16 possible castling right variations
inline U64 zCastle[16];
inline U64 zSide;
void InitZobrist();
U64 GetHashKey(Board &board);
std::vector<std::string> split(std::string_view str, char delim); std::vector<std::string> split(std::string_view str, char delim);
int parseSquare(std::string_view str); int parseSquare(std::string_view str);
Move parseMove(Board &board, std::string_view str); Move parseMove(Board &board, std::string_view str);
}