From 96791e7ab670176d25e313caf2a428e21ccef168 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 10 Aug 2026 17:17:49 +0200 Subject: folowing the lint suggestions --- src/board/board.cpp | 11 +++++++---- src/board/board.hpp | 1 - src/board/fen.cpp | 26 ++++++++++++++------------ src/board/fen.hpp | 2 +- src/evaluate.cpp | 26 +++++++++++++++----------- src/main.cpp | 2 +- 6 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/board/board.cpp b/src/board/board.cpp index c02aac5..ad5ab12 100644 --- a/src/board/board.cpp +++ b/src/board/board.cpp @@ -7,10 +7,10 @@ #include "moves.hpp" #include "zobrist.hpp" -int PositionToIndex(Position i) { return i.rank * 8 + i.file; } +int PositionToIndex(Position i) { return (i.rank * 8) + i.file; } -Position FindKing(Game *b, bool color) { - return IndexToPosition(__builtin_ctzll(b->PieceBitboards[color][KING])); +Position FindKing(Game *g, bool color) { + return IndexToPosition(__builtin_ctzll(g->PieceBitboards[color][KING])); } UndoMove MakeMove(Move move, Game *g) { UndoMove undo = {}; @@ -284,7 +284,10 @@ void UnMakeMove(UndoMove undo, Game *g) { rookTo.file = 7; g->pieces[PositionToIndex(rookTo)] = g->pieces[PositionToIndex(rookFrom)]; - g->pieces[PositionToIndex(rookFrom)] = {false, NONEPIECE}; + g->pieces[PositionToIndex(rookFrom)] = { + .color = false, + .type = NONEPIECE, + }; } else { // d -> a Position rookFrom = undo.from; diff --git a/src/board/board.hpp b/src/board/board.hpp index 3a8a899..6f72e56 100644 --- a/src/board/board.hpp +++ b/src/board/board.hpp @@ -1,7 +1,6 @@ #ifndef SRC_BOARD_H_ #define SRC_BOARD_H_ -#include #include #include diff --git a/src/board/fen.cpp b/src/board/fen.cpp index 73201b8..dd503e0 100644 --- a/src/board/fen.cpp +++ b/src/board/fen.cpp @@ -39,22 +39,24 @@ static void SetPiece(int i, PieceType type, bool color, Game *g) { } g->pieces[i] = {.color = color, .type = type}; } - -void setBoardFen(const std::string fen, Game *g) { - for (int i = 0; i < 64; ++i) { - g->pieces[i] = {.color = false, .type = NONEPIECE}; +static void clearBoard(Game *g) { + for (auto &piece : g->pieces) { + piece = {.color = false, .type = NONEPIECE}; } g->blackCastleKing = false; g->blackCastleQueen = false; g->whiteCastleKing = false; g->whiteCastleQueen = false; +} +void setBoardFen(const std::string &fen, Game *g) { + clearBoard(g); // example fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 int file = 0; int rank = 7; Position enpassant = {}; - enum parserState { + enum parserState : std::uint8_t { BOARD, PLAYER_TO_MOVE, CASTLE, @@ -127,7 +129,7 @@ void setBoardFen(const std::string fen, Game *g) { } } if (parser_state == BOARD) { - if (isdigit(c)) { + if (isdigit(c) != 0) { int move = c - '0'; file += move; if (file > 8) { @@ -150,32 +152,32 @@ void setBoardFen(const std::string fen, Game *g) { continue; }; if (toupper(c) == 'N') { - SetPiece(rank * 8 + file, KNIGHT, isupper(c), g); + SetPiece((rank * 8) + file, KNIGHT, isupper(c) != 0, g); file++; continue; }; if (toupper(c) == 'R') { - SetPiece(rank * 8 + file, ROOK, isupper(c), g); + SetPiece((rank * 8) + file, ROOK, isupper(c) != 0, g); file++; continue; } if (toupper(c) == 'Q') { - SetPiece(rank * 8 + file, QUEEN, isupper(c), g); + SetPiece((rank * 8) + file, QUEEN, isupper(c) != 0, g); file++; continue; } if (toupper(c) == 'B') { - SetPiece(rank * 8 + file, BISHOP, isupper(c), g); + SetPiece((rank * 8) + file, BISHOP, isupper(c) != 0, g); file++; continue; } if (toupper(c) == 'P') { - SetPiece(rank * 8 + file, PAWN, isupper(c), g); + SetPiece((rank * 8) + file, PAWN, isupper(c) != 0, g); file++; continue; } if (toupper(c) == 'K') { - SetPiece(rank * 8 + file, KING, isupper(c), g); + SetPiece((rank * 8) + file, KING, isupper(c) != 0, g); file++; continue; } diff --git a/src/board/fen.hpp b/src/board/fen.hpp index 7af3db3..f52afe5 100644 --- a/src/board/fen.hpp +++ b/src/board/fen.hpp @@ -5,5 +5,5 @@ #include "board.hpp" -void setBoardFen(const std::string fen, Game *b); +void setBoardFen(const std::string &fen, Game *b); #endif /* SRC_FEN_H_ */ diff --git a/src/evaluate.cpp b/src/evaluate.cpp index f08080d..9120445 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -1,7 +1,9 @@ #include "evaluate.hpp" #include "board/board.hpp" #include "moves.hpp" +#include #include +#include #include #include @@ -12,7 +14,7 @@ constexpr int ROOK_VALUE = 500; constexpr int QUEEN_VALUE = 900; constexpr int MATE = 10000; -static int PAWN_TABLE[64] = { +static std::array PAWN_TABLE = { 0, 0, 0, 0, 0, 0, 0, 0, // last rank promotes to a quuen 50, 50, 50, 50, 50, 50, 50, 50, // comments to stop formating to ruin it 10, 10, 20, 35, 35, 20, 10, 10, // @@ -23,7 +25,7 @@ static int PAWN_TABLE[64] = { 0, 0, 0, 0, 0, 0, 0, 0 // }; -static int KNIGHT_TABLE[64] = { +static std::array KNIGHT_TABLE = { -50, -40, -30, -30, -30, -30, -40, -50, // -40, -20, 0, 0, 0, 0, -20, -40, // -30, 0, 10, 15, 15, 10, 0, -30, // @@ -34,7 +36,7 @@ static int KNIGHT_TABLE[64] = { -50, -40, -30, -30, -30, -30, -40, -50, // }; -static int BISHOP_TABLE[64] = { +static std::array BISHOP_TABLE = { -20, -10, -10, -10, -10, -10, -10, -20, // -10, 5, 0, 0, 0, 0, 5, -10, // -10, 10, 10, 10, 10, 10, 10, -10, // @@ -45,7 +47,7 @@ static int BISHOP_TABLE[64] = { -20, -10, -10, -10, -10, -10, -10, -20, // }; -static int ROOK_TABLE[64] = { +static std::array ROOK_TABLE = { 0, 0, 5, 10, 10, 5, 0, 0, // 5, 10, 10, 10, 10, 10, 10, 5, // -5, 0, 0, 0, 0, 0, 0, -5, // @@ -56,7 +58,7 @@ static int ROOK_TABLE[64] = { 0, 0, 5, 10, 10, 5, 0, 0, // }; -static int QUEEN_TABLE[64] = { +static std::array QUEEN_TABLE = { -20, -10, -10, -5, -5, -10, -10, -20, // -10, 0, 0, 0, 0, 0, 0, -10, // -10, 0, 5, 5, 5, 5, 0, -10, // @@ -67,7 +69,7 @@ static int QUEEN_TABLE[64] = { -20, -10, -10, -5, -5, -10, -10, -20, // }; -static int KING_TABLE_EARLY[64] = { +static std::array KING_TABLE_EARLY = { -30, -40, -40, -50, -50, -40, -40, -30, // -30, -40, -40, -50, -50, -40, -40, -30, // -30, -40, -40, -50, -50, -40, -40, -30, // @@ -87,7 +89,7 @@ int EvaluateBoard(Game *g) { * i cound have come up with better name. * this function just takes square and if color is black rotate it */ -static int RotateBoardForBlack(const int square, const bool color) { +static size_t RotateBoardForBlack(const size_t square, const bool color) { return !color ? square : (56 ^ square); } @@ -114,7 +116,7 @@ int CountBoardMaterial(Game *g) { uint64_t piece_bitboard = g->PieceBitboard; while (piece_bitboard != 0) { - int i = __builtin_ctzll(piece_bitboard); + auto i = static_cast(__builtin_ctzll(piece_bitboard)); piece_bitboard &= piece_bitboard - 1; const Piece piece = g->pieces[i]; @@ -167,22 +169,24 @@ int CountBoardMaterial(Game *g) { bool IsEndgame(const Game *g) { int queens = 0; - int rooks = 0; + int minor = 0; for (const Piece &piece : g->pieces) { switch (piece.type) { case QUEEN: queens++; break; + case BISHOP: + case KNIGHT: case ROOK: - rooks++; + minor++; break; default: break; } } - return queens == 0 || (queens == 2 && rooks <= 1); + return queens == 0 || (queens == 2 && minor <= 1); } int EvaluateBoardForWhite(Game *g) { diff --git a/src/main.cpp b/src/main.cpp index a2af33f..aa238ce 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,7 +44,7 @@ static void perft(int argc, char *argv[]) { std::ostringstream oss; oss << "{\"ms\": " << ms << ", \"result\": " << count << "}"; - std::cout << oss.str() << std::endl; + std::cout << oss.str() << "\n"; } int main(int argc, char *argv[]) { -- cgit v1.2.3