Refactor #26

Merged
rektdie merged 11 commits from refactor into main 2025-03-21 10:52:24 +01:00
9 changed files with 62 additions and 100 deletions
Showing only changes of commit 20ab2f476a - Show all commits

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