Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AccessModifierOffset: -1
AccessModifierOffset: 0
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: Consecutive
AlignConsecutiveDeclarations: Consecutive
Expand All @@ -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
Expand All @@ -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
Expand All @@ -33,7 +41,7 @@ MaxEmptyLinesToKeep: 2
NamespaceIndentation: None
PackConstructorInitializers: Never
ReflowComments: false
SortIncludes: false
SortIncludes: Never
SortUsingDeclarations: false
SpaceAfterCStyleCast: true
SpaceAfterTemplateKeyword: false
Expand Down
43 changes: 43 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -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
35 changes: 17 additions & 18 deletions src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 '.';
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
76 changes: 42 additions & 34 deletions src/move.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,70 @@

namespace Clockwork {

static_assert(static_cast<u16>(PieceType::knight) == 1);
static_assert(static_cast<u16>(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<u16>(PieceType::knight) - 1)) << 12,
promo_bishop = (0b1000 | (static_cast<u16>(PieceType::bishop) - 1)) << 12,
promo_rook = (0b1000 | (static_cast<u16>(PieceType::rook) - 1)) << 12,
promo_queen = (0b1000 | (static_cast<u16>(PieceType::queen) - 1)) << 12,
promo_capture = 0b1100 << 12,
promo_knight_capture = (0b1100 | (static_cast<u16>(PieceType::knight) - 1)) << 12,
promo_bishop_capture = (0b1100 | (static_cast<u16>(PieceType::bishop) - 1)) << 12,
promo_rook_capture = (0b1100 | (static_cast<u16>(PieceType::rook) - 1)) << 12,
promo_queen_capture = (0b1100 | (static_cast<u16>(PieceType::queen) - 1)) << 12,
Normal = 0b0000 << 12,
Castle = 0b0001 << 12,
CaptureBit = 0b0100 << 12,
EnPassant = 0b0110 << 12,
PromotionBit = 0b1000 << 12,
PromoKnight = (0b1000 | (static_cast<u16>(PieceType::Knight) - 1)) << 12,
PromoBishop = (0b1000 | (static_cast<u16>(PieceType::Bishop) - 1)) << 12,
PromoRook = (0b1000 | (static_cast<u16>(PieceType::Rook) - 1)) << 12,
PromoQueen = (0b1000 | (static_cast<u16>(PieceType::Queen) - 1)) << 12,
PromoCapture = 0b1100 << 12,
PromoKnightCapture = (0b1100 | (static_cast<u16>(PieceType::Knight) - 1)) << 12,
PromoBishopCapture = (0b1100 | (static_cast<u16>(PieceType::Bishop) - 1)) << 12,
PromoRookCapture = (0b1100 | (static_cast<u16>(PieceType::Rook) - 1)) << 12,
PromoQueenCapture = (0b1100 | (static_cast<u16>(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<u16>(flags);
}

constexpr Square from() const { return Square(raw & 0x3F); }
[[nodiscard]] constexpr Square from() const {
return static_cast<Square>(raw & 0x3F);
}

constexpr Square to() const { return Square((raw >> 6) & 0x3F); }
[[nodiscard]] constexpr Square to() const {
return static_cast<Square>((raw >> 6) & 0x3F);
}

constexpr MoveFlags flags() const { return MoveFlags{static_cast<u16>(raw & (0xF << 12))}; }
[[nodiscard]] constexpr MoveFlags flags() const {
return MoveFlags{static_cast<u16>(raw & (0xF << 12))};
}

constexpr bool is_capture() const { return raw & static_cast<u16>(MoveFlags::capture_bit); }
[[nodiscard]] constexpr bool is_capture() const {
return raw & static_cast<u16>(MoveFlags::CaptureBit);
}

constexpr bool is_promotion() const { return raw & static_cast<u16>(MoveFlags::promotion_bit); }
[[nodiscard]] constexpr bool is_promotion() const {
return raw & static_cast<u16>(MoveFlags::PromotionBit);
}

constexpr std::optional<PieceType> 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<PieceType> 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;
}
}

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;
Expand Down
19 changes: 12 additions & 7 deletions src/square.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>(rank * 8 + file)};
Expand All @@ -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<char>('a' + sq.file());
Expand Down
61 changes: 24 additions & 37 deletions src/uci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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") {}
}
}

Expand Down
Loading