bigger refactor in several files
This commit is contained in:
1 parent
8d5024a4a4
commit
0ffc2c77de
7 files changed
+111
-112
No files matched your search
+16
-16
@@ -29,8 +29,8 @@ void Board::SetByFen(std::string_view fen) {
|
||||
// Starting from top left
|
||||
int currSquare = a8;
|
||||
|
||||
std::vector<std::string> tokens = split(fen, ' ');
|
||||
std::vector<std::string> pieceTokens = split(tokens[0], '/');
|
||||
std::vector<std::string> tokens = UTILS::split(fen, ' ');
|
||||
std::vector<std::string> pieceTokens = UTILS::split(tokens[0], '/');
|
||||
|
||||
constexpr std::string_view pieceTypes = "pnbrqk";
|
||||
|
||||
@@ -61,10 +61,10 @@ void Board::SetByFen(std::string_view fen) {
|
||||
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];
|
||||
hashKey = GetHashKey(*this);
|
||||
hashKey = UTILS::GetHashKey(*this);
|
||||
MOVEGEN::GenerateMoves(*this);
|
||||
MOVEGEN::GenAttackMaps(*this);
|
||||
}
|
||||
@@ -169,7 +169,7 @@ void Board::SetPiece(int piece, int square, bool color) {
|
||||
colors[color].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) {
|
||||
@@ -177,14 +177,14 @@ void Board::RemovePiece(int piece, int square, bool color) {
|
||||
colors[color].PopBit(square);
|
||||
occupied.PopBit(square);
|
||||
|
||||
hashKey ^= zKeys[color][piece][square];
|
||||
hashKey ^= UTILS::zKeys[color][piece][square];
|
||||
}
|
||||
|
||||
// Updates castling rights
|
||||
static void UpdateCastlingRights(Board &board, int square, int type, int color) {
|
||||
if (type == Rook) {
|
||||
// Removing old rights
|
||||
board.hashKey ^= zCastle[board.castlingRights];
|
||||
board.hashKey ^= UTILS::zCastle[board.castlingRights];
|
||||
int queenSideRook = color ? a8 : a1;
|
||||
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.hashKey ^= zCastle[board.castlingRights];
|
||||
board.hashKey ^= UTILS::zCastle[board.castlingRights];
|
||||
} else if (type == King) {
|
||||
// Removing old rights
|
||||
board.hashKey ^= zCastle[board.castlingRights];
|
||||
board.hashKey ^= UTILS::zCastle[board.castlingRights];
|
||||
|
||||
board.castlingRights &= color ? ~blackKingRight : ~whiteKingRight;
|
||||
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;
|
||||
|
||||
sideToMove = !sideToMove;
|
||||
hashKey ^= zSide;
|
||||
hashKey ^= UTILS::zSide;
|
||||
|
||||
if (enPassantTarget != noEPTarget) {
|
||||
hashKey ^= zEnPassant[enPassantTarget % 8];
|
||||
hashKey ^= UTILS::zEnPassant[enPassantTarget % 8];
|
||||
}
|
||||
|
||||
if (newEpTarget != noEPTarget) {
|
||||
hashKey ^= zEnPassant[newEpTarget % 8];
|
||||
hashKey ^= UTILS::zEnPassant[newEpTarget % 8];
|
||||
}
|
||||
|
||||
enPassantTarget = newEpTarget;
|
||||
@@ -328,14 +328,14 @@ void Board::MakeMove(Move move) {
|
||||
UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor);
|
||||
|
||||
sideToMove = !attackerColor;
|
||||
hashKey ^= zSide;
|
||||
hashKey ^= UTILS::zSide;
|
||||
|
||||
if (enPassantTarget != noEPTarget) {
|
||||
hashKey ^= zEnPassant[enPassantTarget % 8];
|
||||
hashKey ^= UTILS::zEnPassant[enPassantTarget % 8];
|
||||
}
|
||||
|
||||
if (newEpTarget != noEPTarget) {
|
||||
hashKey ^= zEnPassant[newEpTarget % 8];
|
||||
hashKey ^= UTILS::zEnPassant[newEpTarget % 8];
|
||||
}
|
||||
|
||||
enPassantTarget = newEpTarget;
|
||||
|
||||
+2
-2
@@ -1,13 +1,13 @@
|
||||
#include "board.h"
|
||||
#include "movegen.h"
|
||||
#include "uci.h"
|
||||
#include "tt.h"
|
||||
#include "utils.h"
|
||||
#include "benchmark.h"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
MOVEGEN::initLeaperAttacks();
|
||||
MOVEGEN::initSliderAttacks();
|
||||
InitZobrist();
|
||||
UTILS::InitZobrist();
|
||||
|
||||
Board board;
|
||||
|
||||
|
||||
+1
-78
@@ -1,82 +1,5 @@
|
||||
#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) {
|
||||
TTEntry *current = &table[hashKey % hashSize];
|
||||
|
||||
@@ -104,4 +27,4 @@ void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move b
|
||||
current->score = score;
|
||||
current->depth = depth;
|
||||
current->bestMove = bestMove;
|
||||
}
|
||||
}
|
||||
-14
@@ -3,20 +3,6 @@
|
||||
#include "search.h"
|
||||
#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 {
|
||||
public:
|
||||
U64 hashKey;
|
||||
|
||||
+2
-2
@@ -29,10 +29,10 @@ void ParsePosition(Board &board, std::string_view command) {
|
||||
}
|
||||
|
||||
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) {
|
||||
board.MakeMove(parseMove(board, move));
|
||||
board.MakeMove(UTILS::parseMove(board, move));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,76 @@
|
||||
#include "utils.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> results;
|
||||
|
||||
@@ -76,3 +146,4 @@ Move parseMove(Board &board, std::string_view str) {
|
||||
|
||||
return Move(from, to, flag);
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,27 @@
|
||||
#include "move.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);
|
||||
|
||||
int parseSquare(std::string_view str);
|
||||
|
||||
Move parseMove(Board &board, std::string_view str);
|
||||
}
|
||||
Reference in new issue
Block a user