diff --git a/.clang-format b/.clang-format index 952d7d90..8a62dc29 100644 --- a/.clang-format +++ b/.clang-format @@ -1,4 +1,4 @@ -AccessModifierOffset: -1 +AccessModifierOffset: 0 AlignAfterOpenBracket: Align AlignConsecutiveAssignments: Consecutive AlignConsecutiveDeclarations: Consecutive @@ -8,7 +8,10 @@ AlignTrailingComments: true AllowAllParametersOfDeclarationOnNextLine: true AllowShortCaseLabelsOnASingleLine: false AllowShortEnumsOnASingleLine: false -AllowShortIfStatementsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortLambdasOnASingleLine: None AlwaysBreakTemplateDeclarations: Yes BasedOnStyle: WebKit BitFieldColonSpacing: After @@ -18,8 +21,13 @@ BreakBeforeBraces: Custom BraceWrapping: AfterFunction: false AfterClass: false - AfterControlStatement: true + AfterControlStatement: Never BeforeElse: true + AfterEnum: false + AfterNamespace: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false BreakBeforeTernaryOperators: true BreakConstructorInitializers: AfterColon BreakStringLiterals: false @@ -33,7 +41,7 @@ MaxEmptyLinesToKeep: 2 NamespaceIndentation: None PackConstructorInitializers: Never ReflowComments: false -SortIncludes: false +SortIncludes: Never SortUsingDeclarations: false SpaceAfterCStyleCast: true SpaceAfterTemplateKeyword: false diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 00000000..93a938ee --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,43 @@ +Checks: > + -*, + readability-identifier-naming, + modernize-use-nodiscard, + modernize-avoid-c-arrays, + modernize-use-nullptr, + modernize-deprecated-headers, + modernize-replace-auto-ptr, + modernize-use-default-member-init, + modernize-use-equals-default, + modernize-use-equals-delete, + modernize-return-braced-init-list, + modernize-use-using, + modernize-use-transparent-functors, + cppcoreguidelines-avoid-c-arrays, + cppcoreguidelines-pro-type-cstyle-cast + +WarningsAsErrors: '' + +CheckOptions: + # Function names: snake_case + - key: readability-identifier-naming.FunctionCase + value: lower_case + + # Variable names: snake_case + - key: readability-identifier-naming.VariableCase + value: lower_case + + # Constants: SCREAMING_SNAKE_CASE + - key: readability-identifier-naming.ConstantCase + value: UPPER_CASE + + # Enum constant values: PascalCase + - key: readability-identifier-naming.EnumConstantCase + value: CamelCase + + # Public data members: snake_case + - key: readability-identifier-naming.PublicMemberCase + value: lower_case + + # Private data members: snake_case + - key: readability-identifier-naming.PrivateMemberCase + value: lower_case \ No newline at end of file diff --git a/src/common.hpp b/src/common.hpp index a1548053..4c78ee1f 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -3,37 +3,36 @@ namespace Clockwork { enum class Color { - white, - black + White, + Black }; enum class PieceType { - pawn, - knight, - bishop, - rook, - queen, - king, - empty, + Pawn, + Knight, + Bishop, + Rook, + Queen, + King, + Empty, }; constexpr char piece_char(PieceType piece) { using enum PieceType; - switch (piece) - { - case pawn : + switch (piece) { + case Pawn : return 'p'; - case knight : + case Knight : return 'n'; - case bishop : + case Bishop : return 'b'; - case rook : + case Rook : return 'r'; - case queen : + case Queen : return 'q'; - case king : + case King : return 'k'; - case empty : + case Empty : return '.'; } } diff --git a/src/main.cpp b/src/main.cpp index b36647ed..046f2049 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ int main(int argc, char* argv[]) { UCI::UCIHandler uci; if (argc > 1) - uci.handleCommandLine(argc, argv); + uci.handle_command_line(argc, argv); else uci.loop(); diff --git a/src/move.hpp b/src/move.hpp index 3f13ce75..68f2b514 100644 --- a/src/move.hpp +++ b/src/move.hpp @@ -8,53 +8,62 @@ namespace Clockwork { -static_assert(static_cast(PieceType::knight) == 1); +static_assert(static_cast(PieceType::Knight) == 1); enum class MoveFlags : u16 { - normal = 0b0000 << 12, - castle = 0b0001 << 12, - capture_bit = 0b0100 << 12, - en_passant = 0b0110 << 12, - promotion_bit = 0b1000 << 12, - promo_knight = (0b1000 | (static_cast(PieceType::knight) - 1)) << 12, - promo_bishop = (0b1000 | (static_cast(PieceType::bishop) - 1)) << 12, - promo_rook = (0b1000 | (static_cast(PieceType::rook) - 1)) << 12, - promo_queen = (0b1000 | (static_cast(PieceType::queen) - 1)) << 12, - promo_capture = 0b1100 << 12, - promo_knight_capture = (0b1100 | (static_cast(PieceType::knight) - 1)) << 12, - promo_bishop_capture = (0b1100 | (static_cast(PieceType::bishop) - 1)) << 12, - promo_rook_capture = (0b1100 | (static_cast(PieceType::rook) - 1)) << 12, - promo_queen_capture = (0b1100 | (static_cast(PieceType::queen) - 1)) << 12, + Normal = 0b0000 << 12, + Castle = 0b0001 << 12, + CaptureBit = 0b0100 << 12, + EnPassant = 0b0110 << 12, + PromotionBit = 0b1000 << 12, + PromoKnight = (0b1000 | (static_cast(PieceType::Knight) - 1)) << 12, + PromoBishop = (0b1000 | (static_cast(PieceType::Bishop) - 1)) << 12, + PromoRook = (0b1000 | (static_cast(PieceType::Rook) - 1)) << 12, + PromoQueen = (0b1000 | (static_cast(PieceType::Queen) - 1)) << 12, + PromoCapture = 0b1100 << 12, + PromoKnightCapture = (0b1100 | (static_cast(PieceType::Knight) - 1)) << 12, + PromoBishopCapture = (0b1100 | (static_cast(PieceType::Bishop) - 1)) << 12, + PromoRookCapture = (0b1100 | (static_cast(PieceType::Rook) - 1)) << 12, + PromoQueenCapture = (0b1100 | (static_cast(PieceType::Queen) - 1)) << 12, }; struct Move { u16 raw; - constexpr Move(Square from, Square to, MoveFlags flags = MoveFlags::normal) { + constexpr Move(Square from, Square to, MoveFlags flags = MoveFlags::Normal) { raw = from.raw | (to.raw << 6) | static_cast(flags); } - constexpr Square from() const { return Square(raw & 0x3F); } + [[nodiscard]] constexpr Square from() const { + return static_cast(raw & 0x3F); + } - constexpr Square to() const { return Square((raw >> 6) & 0x3F); } + [[nodiscard]] constexpr Square to() const { + return static_cast((raw >> 6) & 0x3F); + } - constexpr MoveFlags flags() const { return MoveFlags{static_cast(raw & (0xF << 12))}; } + [[nodiscard]] constexpr MoveFlags flags() const { + return MoveFlags{static_cast(raw & (0xF << 12))}; + } - constexpr bool is_capture() const { return raw & static_cast(MoveFlags::capture_bit); } + [[nodiscard]] constexpr bool is_capture() const { + return raw & static_cast(MoveFlags::CaptureBit); + } - constexpr bool is_promotion() const { return raw & static_cast(MoveFlags::promotion_bit); } + [[nodiscard]] constexpr bool is_promotion() const { + return raw & static_cast(MoveFlags::PromotionBit); + } - constexpr std::optional promo() const { - switch (flags()) - { - case MoveFlags::promo_knight : - return PieceType::knight; - case MoveFlags::promo_bishop : - return PieceType::bishop; - case MoveFlags::promo_rook : - return PieceType::rook; - case MoveFlags::promo_queen : - return PieceType::queen; + [[nodiscard]] constexpr std::optional promo() const { + switch (flags()) { + case MoveFlags::PromoKnight : + return PieceType::Knight; + case MoveFlags::PromoBishop : + return PieceType::Bishop; + case MoveFlags::PromoRook : + return PieceType::Rook; + case MoveFlags::PromoQueen : + return PieceType::Queen; default : return std::nullopt; } @@ -62,8 +71,7 @@ struct Move { friend std::ostream& operator<<(std::ostream& os, Move mv) { os << mv.from() << mv.to(); - if (auto promo = mv.promo()) - { + if (auto promo = mv.promo()) { os << piece_char(*promo); } return os; diff --git a/src/square.hpp b/src/square.hpp index 21aa3536..c8ca68f3 100644 --- a/src/square.hpp +++ b/src/square.hpp @@ -13,9 +13,10 @@ struct Square { u8 raw; constexpr Square(u8 r) : - raw{r} {} + raw{r} { + } - static constexpr Square fromFileAndRank(int file, int rank) { + static constexpr Square from_file_and_rank(int file, int rank) { assert(file >= 0 && file < 8); assert(rank >= 0 && rank < 8); return Square{static_cast(rank * 8 + file)}; @@ -26,16 +27,20 @@ struct Square { return std::nullopt; if (str[0] < 'a' or str[0] > 'h') return std::nullopt; - const int file = str[0] - 'a'; + const int FILE = str[0] - 'a'; if (str[1] < '1' or str[1] > '8') return std::nullopt; - const int rank = str[1] - '1'; - return fromFileAndRank(file, rank); + const int RANK = str[1] - '1'; + return from_file_and_rank(FILE, RANK); } - constexpr usize file() const { return raw % 8; } + [[nodiscard]] constexpr usize file() const { + return raw % 8; + } - constexpr usize rank() const { return raw / 8; } + [[nodiscard]] constexpr usize rank() const { + return raw / 8; + } friend std::ostream& operator<<(std::ostream& os, Square sq) { char file = static_cast('a' + sq.file()); diff --git a/src/uci.cpp b/src/uci.cpp index 4080e74c..158e0297 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -13,22 +13,21 @@ void UCIHandler::loop() { std::string input; while (std::getline(std::cin, input)) - executeCommand(input); + execute_command(input); } -void UCIHandler::handleCommandLine(int argc, char* argv[]) { +void UCIHandler::handle_command_line(int argc, char* argv[]) { for (int i = 1; i < argc; ++i) - executeCommand(argv[i]); + execute_command(argv[i]); } -void UCIHandler::executeCommand(const std::string& line) { +void UCIHandler::execute_command(const std::string& line) { std::istringstream is{line}; std::string command; is >> std::skipws >> command; - if (command == "uci") - { + if (command == "uci") { std::cout << "id Name Clockwork\n"; std::cout << "id author The Clockwork community" << std::endl; } @@ -37,54 +36,42 @@ void UCIHandler::executeCommand(const std::string& line) { else if (command == "quit") std::exit(0); else if (command == "go") - handleGo(is); + handle_go(is); else if (command == "position") - handlePosition(is); + handle_position(is); else std::cout << "Unknown command" << std::endl; } -void UCIHandler::handleGo(std::istringstream& is) { +void UCIHandler::handle_go(std::istringstream& is) { std::string token; - while (is >> token) - { - if (token == "depth") - { + while (is >> token) { + if (token == "depth") { is >> settings.depth; } - else if (token == "movetime") - { - is >> settings.moveTime; + else if (token == "movetime") { + is >> settings.move_time; } - else if (token == "wtime") - { - is >> settings.wTime; + else if (token == "wtime") { + is >> settings.w_time; } - else if (token == "btime") - { - is >> settings.bTime; + else if (token == "btime") { + is >> settings.b_time; } - else if (token == "winc") - { - is >> settings.wInc; + else if (token == "winc") { + is >> settings.w_inc; } - else if (token == "binc") - { - is >> settings.bInc; + else if (token == "binc") { + is >> settings.b_inc; } } } -void UCIHandler::handlePosition(std::istringstream& is) { +void UCIHandler::handle_position(std::istringstream& is) { std::string token; - if (is >> token) - { - if (token == "startpos") - { - } - else if (token == "fen") - { - } + if (is >> token) { + if (token == "startpos") {} + else if (token == "fen") {} } } diff --git a/src/uci.hpp b/src/uci.hpp index 7945a4d9..566b98a5 100644 --- a/src/uci.hpp +++ b/src/uci.hpp @@ -5,24 +5,24 @@ namespace Clockwork::UCI { struct SearchSettings { - int depth = 0; - int wTime = 0; - int bTime = 0; - int wInc = 0; - int bInc = 0; - int moveTime = 0; + int depth = 0; + int w_time = 0; + int b_time = 0; + int w_inc = 0; + int b_inc = 0; + int move_time = 0; }; class UCIHandler { - public: + public: void loop(); - void handleCommandLine(int argc, char* argv[]); + void handle_command_line(int argc, char* argv[]); - private: + private: SearchSettings settings; - void executeCommand(const std::string&); - void handleGo(std::istringstream&); - void handlePosition(std::istringstream&); + void execute_command(const std::string&); + void handle_go(std::istringstream&); + void handle_position(std::istringstream&); }; }