refactor in several areas

This commit is contained in:
rektdie committed 2025-03-20 20:20:24 +01:00
1 parent 23e1f8a165
commit 20ab2f476a
9 files changed
+62 -100

No files matched your search

+8 -8
View File
@@ -65,8 +65,8 @@ void Board::SetByFen(std::string_view fen) {
occupied = colors[White] | colors[Black];
hashKey = GetHashKey(*this);
GenerateMoves(*this, sideToMove);
GenAttackMaps(*this);
MOVEGEN::GenerateMoves(*this);
MOVEGEN::GenAttackMaps(*this);
}
void Board::PrintBoard() {
@@ -158,10 +158,10 @@ U64 Board::GetAttackMaps(bool side) {
return combined;
}
bool Board::InCheck(bool side) {
Bitboard myKingSquare = colors[side] & pieces[King];
bool Board::InCheck() {
Bitboard myKingSquare = colors[sideToMove] & pieces[King];
return GetAttackMaps(!side) & myKingSquare;
return GetAttackMaps(!sideToMove) & myKingSquare;
}
void Board::SetPiece(int piece, int square, bool color) {
@@ -340,18 +340,18 @@ void Board::MakeMove(Move move) {
enPassantTarget = newEpTarget;
GenAttackMaps(*this);
MOVEGEN::GenAttackMaps(*this);
positionIndex++;
positionHistory[positionIndex] = hashKey;
}
bool Board::InPossibleZug(bool side) {
bool Board::InPossibleZug() {
Bitboard toCheck;
// For all pieces other than pawns and king
for (int piece = Knight; piece <= Queen; piece++) {
toCheck |= (pieces[piece] & colors[side]);
toCheck |= (pieces[piece] & colors[sideToMove]);
}
return !toCheck;
+2 -2
View File
@@ -46,7 +46,7 @@ public:
int GetPieceColor(int square);
U64 GetAttackMaps(bool side);
bool InCheck(bool side);
bool InCheck();
void SetPiece(int piece, int square, bool color);
void RemovePiece(int piece, int square, bool color);
@@ -54,5 +54,5 @@ public:
void Promote(int square, int pieceType, int color, bool isCapture);
void MakeMove(Move move);
bool InPossibleZug(bool side);
bool InPossibleZug();
};
+2 -2
View File
@@ -5,8 +5,8 @@
#include "benchmark.h"
int main(int argc, char* argv[]) {
initLeaperAttacks();
initSliderAttacks();
MOVEGEN::initLeaperAttacks();
MOVEGEN::initSliderAttacks();
InitZobrist();
Board board;
+30 -27
View File
@@ -1,22 +1,24 @@
#include "movegen.h"
Bitboard pawnAttacks[2][64];
Bitboard knightAttacks[64];
Bitboard kingAttacks[64];
namespace MOVEGEN {
static Bitboard pawnAttacks[2][64];
static Bitboard knightAttacks[64];
static Bitboard kingAttacks[64];
Bitboard bishopMasks[64];
Bitboard rookMasks[64];
static Bitboard bishopMasks[64];
static Bitboard rookMasks[64];
Bitboard bishopAttacks[64][512];
Bitboard rookAttacks[64][4096];
static Bitboard bishopAttacks[64][512];
static Bitboard rookAttacks[64][4096];
static Bitboard notAfile = ~files[A];
static Bitboard notHfile = ~files[H];
static Bitboard notABfile = notAfile & ~files[B];
static Bitboard notHGfile = notHfile & ~files[G];
Bitboard maskPawnAttacks(int side, int square) {
static Bitboard maskPawnAttacks(int side, int square) {
Bitboard attacks;
Bitboard attacker = Bitboard::GetSquare(square);
@@ -33,7 +35,7 @@ Bitboard maskPawnAttacks(int side, int square) {
return attacks;
}
Bitboard maskKnightAttacks(int square) {
static Bitboard maskKnightAttacks(int square) {
Bitboard attacks;
Bitboard attacker = Bitboard::GetSquare(square);
@@ -50,7 +52,7 @@ Bitboard maskKnightAttacks(int square) {
return attacks;
}
Bitboard maskKingAttacks(int square) {
static Bitboard maskKingAttacks(int square) {
Bitboard attacks;
Bitboard attacker = Bitboard::GetSquare(square);
@@ -67,7 +69,7 @@ Bitboard maskKingAttacks(int square) {
return attacks;
}
Bitboard maskBishopAttacks(int square) {
static Bitboard maskBishopAttacks(int square) {
Bitboard attacks;
// target rank and files
@@ -96,7 +98,7 @@ Bitboard maskBishopAttacks(int square) {
return attacks;
}
Bitboard maskRookAttacks(int square) {
static Bitboard maskRookAttacks(int square) {
Bitboard attacks;
// target rank and files
@@ -125,7 +127,7 @@ Bitboard maskRookAttacks(int square) {
return attacks;
}
Bitboard bishopAttacksOnTheFly(int square, Bitboard block) {
static Bitboard bishopAttacksOnTheFly(int square, Bitboard block) {
Bitboard attacks;
// target rank and files
@@ -164,7 +166,7 @@ Bitboard bishopAttacksOnTheFly(int square, Bitboard block) {
return attacks;
}
Bitboard rookAttacksOnTheFly(int square, Bitboard block) {
static Bitboard rookAttacksOnTheFly(int square, Bitboard block) {
Bitboard attacks;
// target rank and files
@@ -238,7 +240,7 @@ void initSliderAttacks() {
}
}
Bitboard getBishopAttack(int square, U64 occupancy) {
static Bitboard getBishopAttack(int square, U64 occupancy) {
occupancy &= bishopMasks[square];
occupancy *= bishopMagicNumbers[square];
occupancy >>= (64 - bishopRelevantBits[square]);
@@ -246,7 +248,7 @@ Bitboard getBishopAttack(int square, U64 occupancy) {
return bishopAttacks[square][occupancy];
}
Bitboard getRookAttack(int square, U64 occupancy) {
static Bitboard getRookAttack(int square, U64 occupancy) {
occupancy &= rookMasks[square];
occupancy *= rookMagicNumbers[square];
occupancy >>= (64 - rookRelevantBits[square]);
@@ -254,7 +256,7 @@ Bitboard getRookAttack(int square, U64 occupancy) {
return rookAttacks[square][occupancy];
}
Bitboard getQueenAttack(int square, U64 occupancy) {
static Bitboard getQueenAttack(int square, U64 occupancy) {
U64 bishopOccupancy = occupancy;
U64 rookOccupancy = occupancy;
@@ -269,7 +271,7 @@ Bitboard getQueenAttack(int square, U64 occupancy) {
return rookAttacks[square][rookOccupancy] | bishopAttacks[square][bishopOccupancy];
}
Bitboard getPieceAttacks(int square, int piece, int color, U64 occupancy) {
static Bitboard getPieceAttacks(int square, int piece, int color, U64 occupancy) {
if (piece == Pawn) {
return pawnAttacks[color][square];
} else if (piece == Knight) {
@@ -987,18 +989,19 @@ void GenAttackMaps(Board &board) {
}
}
void GenerateMoves(Board &board, bool side) {
void GenerateMoves(Board &board) {
board.ResetMoves();
if (board.InCheck(side)) {
GenCheckEvasions(board, side);
if (board.InCheck()) {
GenCheckEvasions(board, board.sideToMove);
return;
}
GenPawnMoves(board, side);
GenKnightMoves(board, side);
GenKingMoves(board, side, false);
GenBishopMoves(board, side);
GenRookMoves(board, side);
GenQueenMoves(board, side);
GenPawnMoves(board, board.sideToMove);
GenKnightMoves(board, board.sideToMove);
GenKingMoves(board, board.sideToMove, false);
GenBishopMoves(board, board.sideToMove);
GenRookMoves(board, board.sideToMove);
GenQueenMoves(board, board.sideToMove);
}
}
+10 -51
View File
@@ -3,6 +3,15 @@
#include "move.h"
#include "board.h"
namespace MOVEGEN {
void initLeaperAttacks();
void initSliderAttacks();
void GenAttackMaps(Board &board);
void GenerateMoves(Board &board);
// bishop relevant occupancy bit count
constexpr int bishopRelevantBits[64] = {
6, 5, 5, 5, 5, 5, 5, 6,
@@ -160,54 +169,4 @@ constexpr U64 bishopMagicNumbers[64] = {
0x8918844842082200ULL,
0x4010011029020020ULL
};
// pawn attacks table [side][square]
extern Bitboard pawnAttacks[2][64];
extern Bitboard knightAttacks[64];
extern Bitboard kingAttacks[64];
extern Bitboard bishopMasks[64];
extern Bitboard rookMasks[64];
// bishop attacks table [square][occupancies]
extern Bitboard bishopAttacks[64][512];
// rook attacks table [square][occupancies]
extern Bitboard rookAttacks[64][4096];
// generate pawn masks
Bitboard maskPawnAttacks(int square, int side);
// generate knight masks
Bitboard maskKnightAttacks(int square);
// generate king masks
Bitboard maskKingAttacks(int square);
// generate bishop masks
Bitboard maskBishopAttacks(int square);
//generate bishop attacks
Bitboard bishopAttacksOnTheFly(int square, Bitboard block);
// generate rook masks
Bitboard maskRookAttacks(int square);
//generate rook attacks
Bitboard rookAttacksOnTheFly(int square, Bitboard block);
void initLeaperAttacks();
void initSliderAttacks();
Bitboard getBishopAttack(int square, U64 occupancy);
Bitboard getRookAttack(int square, U64 occupancy);
Bitboard getQueenAttack(int square, U64 occupancy);
// Returns Bitboard of piece attacks from given square
// Returns attacks regardless of board (so occupancy is 0ULL)
Bitboard getPieceAttacks(int square, int piece, int color);
void GenAttackMaps(Board &board);
void GenerateMoves(Board &board, bool side);
}
+2 -2
View File
@@ -2,7 +2,7 @@
#include "stopwatch.h"
U64 HelperPerft(Board board, int depth) {
GenerateMoves(board, board.sideToMove);
MOVEGEN::GenerateMoves(board);
if (depth == 1) return board.currentMoveIndex;
if (depth == 0) return 1ULL;
@@ -19,7 +19,7 @@ U64 HelperPerft(Board board, int depth) {
}
void Perft(Board &board, int depth) {
GenerateMoves(board, board.sideToMove);
MOVEGEN::GenerateMoves(board);
U64 totalNodes = 0;
+5 -5
View File
@@ -128,7 +128,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) {
alpha = bestScore;
}
GenerateMoves(board, board.sideToMove);
MOVEGEN::GenerateMoves(board);
SortMoves(board, ply);
@@ -183,7 +183,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
const int staticEval = HCE::Evaluate(board);
if (board.InCheck(board.sideToMove)) {
if (board.InCheck()) {
depth++;
} else {
if (ply) {
@@ -195,7 +195,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
// Null Move Pruning
if (!doingNullMove && staticEval >= beta) {
if (depth >= 3 && !board.InPossibleZug(board.sideToMove)) {
if (depth >= 3 && !board.InPossibleZug()) {
Board copy = board;
copy.MakeMove(Move());
@@ -210,10 +210,10 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
}
}
GenerateMoves(board, board.sideToMove);
MOVEGEN::GenerateMoves(board);
if (board.currentMoveIndex == 0) {
if (board.InCheck(board.sideToMove)) { // checkmate
if (board.InCheck()) { // checkmate
return -99000 + ply;
} else { // stalemate
return 0;
+2 -2
View File
@@ -36,8 +36,8 @@ void ParsePosition(Board &board, std::string_view command) {
}
}
GenerateMoves(board, board.sideToMove);
GenAttackMaps(board);
MOVEGEN::GenerateMoves(board);
MOVEGEN::GenAttackMaps(board);
}
static int ReadParam(std::string param, std::string &command) {
+1 -1
View File
@@ -25,7 +25,7 @@ int parseSquare(std::string_view str) {
}
Move parseMove(Board &board, std::string_view str) {
GenerateMoves(board, board.sideToMove);
MOVEGEN::GenerateMoves(board);
int from = parseSquare(str.substr(0, 2));
int to = parseSquare(str.substr(2, 2));