From 1b006985ba2381a29dd9aa34aec1dafbae224897 Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 31 Jul 2026 20:27:02 +0200 Subject: using cpp check on this project --- src/board/board.cpp | 11 ++------ src/board/board.hpp | 7 +++-- src/board/fen.cpp | 2 +- src/board/fen.hpp | 2 +- src/bot.cpp | 2 +- src/evaluate.cpp | 2 +- src/evaluate.hpp | 2 +- src/main.cpp | 2 +- src/uci.cpp | 4 +-- src/uci.hpp | 2 +- src/zobrist.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ src/zobrist.hpp | 11 ++++++++ src/zobrist/zobrist.cpp | 72 ------------------------------------------------- src/zobrist/zobrist.hpp | 11 -------- 14 files changed, 97 insertions(+), 105 deletions(-) create mode 100644 src/zobrist.cpp create mode 100644 src/zobrist.hpp delete mode 100644 src/zobrist/zobrist.cpp delete mode 100644 src/zobrist/zobrist.hpp (limited to 'src') diff --git a/src/board/board.cpp b/src/board/board.cpp index af313d0..2cd705d 100644 --- a/src/board/board.cpp +++ b/src/board/board.cpp @@ -1,16 +1,9 @@ #include "board.hpp" -#include "../moves.hpp" -#include "zobrist/zobrist.hpp" +#include "moves.hpp" +#include "zobrist.hpp" #include #include -Piece createPiece(PieceType type, bool color) { - Piece piece; - piece.type = type; - piece.color = color; - return piece; -} - int PositionToIndex(Position i) { return i.rank * 8 + i.file; } Position FindKing(Game *b, bool color) { diff --git a/src/board/board.hpp b/src/board/board.hpp index 5d0d913..bcd0b95 100644 --- a/src/board/board.hpp +++ b/src/board/board.hpp @@ -45,9 +45,9 @@ struct Move { enum Flag { EXACT, LOWERBOUND, UPPERBOUND }; struct TranspositionsEntry { - int depth; - int Eval; - Flag flag; + int depth = -1; + int Eval = 0; + Flag flag = EXACT; Move bestMove = {}; }; struct Game { @@ -96,7 +96,6 @@ struct UndoMove { bool wasCastle = false; bool CastledSide = false; // 0 for queen side, 1 for king side }; -Piece createPiece(PieceType type, bool color); int PositionToIndex(Position i); UndoMove MakeMove(Move move, Game *g); diff --git a/src/board/fen.cpp b/src/board/fen.cpp index faa8f83..a7e65eb 100644 --- a/src/board/fen.cpp +++ b/src/board/fen.cpp @@ -11,7 +11,7 @@ bool WHITE = true; bool BLACK = false; -void setBoardFen(std::string fen, Game *g) { +void setBoardFen(const std::string fen, Game *g) { // example fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 int file = 0; int rank = 0; diff --git a/src/board/fen.hpp b/src/board/fen.hpp index 406f5fe..7af3db3 100644 --- a/src/board/fen.hpp +++ b/src/board/fen.hpp @@ -5,5 +5,5 @@ #include "board.hpp" -void setBoardFen(std::string fen, Game *b); +void setBoardFen(const std::string fen, Game *b); #endif /* SRC_FEN_H_ */ diff --git a/src/bot.cpp b/src/bot.cpp index 0358241..3ea6620 100644 --- a/src/bot.cpp +++ b/src/bot.cpp @@ -2,7 +2,7 @@ #include "board/board.hpp" #include "evaluate.hpp" #include "moves.hpp" -#include "zobrist/zobrist.hpp" +#include "zobrist.hpp" #include #include diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 7d0a78e..a777e0c 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -150,7 +150,7 @@ int CountBoardMaterial(Game *g) { return score; }; -bool IsEndgame(Game *g) { +bool IsEndgame(const Game *g) { int queens = 0; int rooks = 0; diff --git a/src/evaluate.hpp b/src/evaluate.hpp index feeff70..66b362d 100644 --- a/src/evaluate.hpp +++ b/src/evaluate.hpp @@ -9,6 +9,6 @@ */ int EvaluateBoardForWhite(Game *g); -bool IsEndgame(Game *g); +bool IsEndgame(const Game *g); #endif /* SRC_EVALUATE_H_ */ diff --git a/src/main.cpp b/src/main.cpp index 25082be..4469e40 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ #include "misc.hpp" #include "uci.hpp" -#include "zobrist/zobrist.hpp" +#include "zobrist.hpp" #include int main() { diff --git a/src/uci.cpp b/src/uci.cpp index d9972b2..4357e99 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -4,7 +4,7 @@ #include "bot.hpp" #include "misc.hpp" #include "moves.hpp" -#include "zobrist/zobrist.hpp" +#include "zobrist.hpp" #include #include @@ -37,7 +37,7 @@ void LogUci(const std::string &message) { } using namespace std; -Move UciToMove(string uci) { +Move UciToMove(const string uci) { Position from; Position to; diff --git a/src/uci.hpp b/src/uci.hpp index 4564895..372f007 100644 --- a/src/uci.hpp +++ b/src/uci.hpp @@ -5,7 +5,7 @@ #include #include -Move UciToMove(std::string uci); +Move UciToMove(const std::string uci); void LogUci(const std::string &message); void Uci(); Game initBoard( diff --git a/src/zobrist.cpp b/src/zobrist.cpp new file mode 100644 index 0000000..20ac5e9 --- /dev/null +++ b/src/zobrist.cpp @@ -0,0 +1,72 @@ +#include "zobrist.hpp" +#include "board/board.hpp" + +#include + +uint64_t PieceKeys[2][7][64]; +uint64_t SideKey; +uint64_t CastleKeys[16]; +uint64_t EnPassantKeys[8]; + +void InitZobrist() { + std::mt19937_64 rng(1234567); // fixed seed + + for (int color = 0; color < 2; color++) { + for (int piece = 0; piece < 7; piece++) { + for (int square = 0; square < 64; square++) { + PieceKeys[color][piece][square] = rng(); + } + } + } + + SideKey = rng(); + + for (int i = 0; i < 16; i++) { + CastleKeys[i] = rng(); + } + + for (int i = 0; i < 8; i++) { + EnPassantKeys[i] = rng(); + } +} + +uint64_t GenerateZobristKey(Game *b) { + uint64_t key = 0; + + for (int square = 0; square < 64; square++) { + Piece p = b->pieces[square]; + + if (p.type == NONEPIECE) + continue; + + key ^= PieceKeys[p.color][p.type][square]; + } + + // side to move + if (b->turn) + key ^= SideKey; + + // castling + int castle = 0; + + if (b->whiteCastleKing) + castle |= 1; + + if (b->whiteCastleQueen) + castle |= 2; + + if (b->blackCastleKing) + castle |= 4; + + if (b->blackCastleQueen) + castle |= 8; + + key ^= CastleKeys[castle]; + + // en passant + if (b->canEnpassant) { + key ^= EnPassantKeys[b->enPassant.file]; + } + + return key; +} diff --git a/src/zobrist.hpp b/src/zobrist.hpp new file mode 100644 index 0000000..575d912 --- /dev/null +++ b/src/zobrist.hpp @@ -0,0 +1,11 @@ +#pragma once +#include "board/board.hpp" +#include + +extern uint64_t PieceKeys[2][7][64]; +extern uint64_t SideKey; +extern uint64_t CastleKeys[16]; +extern uint64_t EnPassantKeys[8]; + +void InitZobrist(); +uint64_t GenerateZobristKey(Game *b); diff --git a/src/zobrist/zobrist.cpp b/src/zobrist/zobrist.cpp deleted file mode 100644 index a1efd78..0000000 --- a/src/zobrist/zobrist.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include "zobrist.hpp" -#include "../board/board.hpp" - -#include - -uint64_t PieceKeys[2][7][64]; -uint64_t SideKey; -uint64_t CastleKeys[16]; -uint64_t EnPassantKeys[8]; - -void InitZobrist() { - std::mt19937_64 rng(1234567); // fixed seed - - for (int color = 0; color < 2; color++) { - for (int piece = 0; piece < 7; piece++) { - for (int square = 0; square < 64; square++) { - PieceKeys[color][piece][square] = rng(); - } - } - } - - SideKey = rng(); - - for (int i = 0; i < 16; i++) { - CastleKeys[i] = rng(); - } - - for (int i = 0; i < 8; i++) { - EnPassantKeys[i] = rng(); - } -} - -uint64_t GenerateZobristKey(Game *b) { - uint64_t key = 0; - - for (int square = 0; square < 64; square++) { - Piece p = b->pieces[square]; - - if (p.type == NONEPIECE) - continue; - - key ^= PieceKeys[p.color][p.type][square]; - } - - // side to move - if (b->turn) - key ^= SideKey; - - // castling - int castle = 0; - - if (b->whiteCastleKing) - castle |= 1; - - if (b->whiteCastleQueen) - castle |= 2; - - if (b->blackCastleKing) - castle |= 4; - - if (b->blackCastleQueen) - castle |= 8; - - key ^= CastleKeys[castle]; - - // en passant - if (b->canEnpassant) { - key ^= EnPassantKeys[b->enPassant.file]; - } - - return key; -} diff --git a/src/zobrist/zobrist.hpp b/src/zobrist/zobrist.hpp deleted file mode 100644 index 9433421..0000000 --- a/src/zobrist/zobrist.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once -#include "../board/board.hpp" -#include - -extern uint64_t PieceKeys[2][7][64]; -extern uint64_t SideKey; -extern uint64_t CastleKeys[16]; -extern uint64_t EnPassantKeys[8]; - -void InitZobrist(); -uint64_t GenerateZobristKey(Game *b); -- cgit v1.2.3