From 0c38056b788e2982bc2b063902ddd6e23eb17619 Mon Sep 17 00:00:00 2001 From: awo-dev Date: Tue, 17 Apr 2018 16:14:26 +0200 Subject: [PATCH 1/3] Add piece class --- CMakeLists.txt | 2 +- main.cpp | 9 ++++++ util/Color.h | 14 ++++++++++ util/Piece.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ util/Piece.h | 36 ++++++++++++++++++++++++ util/PieceType.h | 30 ++++++++++++++++++++ 6 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 util/Color.h create mode 100644 util/Piece.cpp create mode 100644 util/Piece.h create mode 100644 util/PieceType.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 350191d..67756a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(chessai_cpp) set(CMAKE_CXX_STANDARD 11) -add_executable(chessai_cpp main.cpp) \ No newline at end of file +add_executable(chessai_cpp main.cpp util/Color.h util/Piece.cpp util/Piece.h util/PieceType.h) \ No newline at end of file diff --git a/main.cpp b/main.cpp index f253cbe..fc61374 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,15 @@ #include +#include "util/Color.h" +#include "util/Piece.h" +#include "util/PieceType.h" int main() { std::cout << "Hello, World!" << std::endl; + + std::cout << "A king has a value of " << static_cast(PieceType::BLACK_QUEEN) << std::endl; + + auto p = Piece(static_cast(PieceType::BLACK_QUEEN)); + + std::cout << "Letter is " << p.getLetter() << std::endl; return 0; } \ No newline at end of file diff --git a/util/Color.h b/util/Color.h new file mode 100644 index 0000000..e235a50 --- /dev/null +++ b/util/Color.h @@ -0,0 +1,14 @@ +// +// Created by awo on 17-04-2018. +// + +#ifndef CHESSAI_CPP_COLOR_H +#define CHESSAI_CPP_COLOR_H + + +enum class Color { + WHITE, BLACK +}; + + +#endif //CHESSAI_CPP_COLOR_H diff --git a/util/Piece.cpp b/util/Piece.cpp new file mode 100644 index 0000000..d2161a3 --- /dev/null +++ b/util/Piece.cpp @@ -0,0 +1,73 @@ +// +// Created by awo on 17-04-2018. +// + +#include +#include "Piece.h" + +Piece::Piece(const int value) { + this->TYPE = PieceType::NIL; + this->value = value; +} + +int Piece::getValue() { + return value; +} + +Color Piece::getColor() { + return (isWhite() ? Color::WHITE : Color::BLACK); +} + +bool Piece::isWhite() { + return value > 0; +} + +bool Piece::isBlack() { + return value < 0; +} + +bool Piece::isKing() { + return std::abs(value) == static_cast(PieceType::KING); +} + +bool Piece::isQueen() { + return std::abs(value) == static_cast(PieceType::QUEEN); +} + +bool Piece::isRook() { + return std::abs(value) == static_cast(PieceType::ROOK); +} + +bool Piece::isBishop() { + return std::abs(value) == static_cast(PieceType::BISHOP); +} + +bool Piece::isKnight() { + return std::abs(value) == static_cast(PieceType::KNIGHT); +} + +bool Piece::isPawn() { + return std::abs(value) == static_cast(PieceType::PAWN); +} + +std::string Piece::getLetter() { + std::string letter = ""; + + if (isKing()) + letter = "K"; + else if (isQueen()) + letter = "Q"; + else if (isRook()) + letter = "R"; + else if (isBishop()) + letter = "B"; + else if (isKnight()) + letter = "N"; + else if (isPawn()) + letter = "P"; + + if (isBlack()) + std::transform(letter.begin(), letter.end(), letter.begin(), ::tolower); + + return letter; +} \ No newline at end of file diff --git a/util/Piece.h b/util/Piece.h new file mode 100644 index 0000000..8fee904 --- /dev/null +++ b/util/Piece.h @@ -0,0 +1,36 @@ +// +// Created by awo on 17-04-2018. +// + +#ifndef CHESSAI_CPP_PIECE_H +#define CHESSAI_CPP_PIECE_H + +#include +#include "PieceType.h" +#include "Color.h" + +class Piece { +public: + PieceType TYPE; + + Piece(const int value); +// ~Piece(); + + int getValue(); + Color getColor(); + bool isWhite(); + bool isBlack(); + bool isKing(); + bool isQueen(); + bool isRook(); + bool isBishop(); + bool isKnight(); + bool isPawn(); + + std::string getLetter(); +private: + int value; +}; + + +#endif //CHESSAI_CPP_PIECE_H diff --git a/util/PieceType.h b/util/PieceType.h new file mode 100644 index 0000000..fa11e80 --- /dev/null +++ b/util/PieceType.h @@ -0,0 +1,30 @@ +// +// Created by awo on 17-04-2018. +// + +#ifndef CHESSAI_CPP_PIECETYPE_H +#define CHESSAI_CPP_PIECETYPE_H + +enum class PieceType { + NIL = 0, + PAWN = 1, + KNIGHT = 2, + KING = 3, + BISHOP = 5, + ROOK = 6, + QUEEN = 7, + WHITE_PAWN = 1, + WHITE_KNIGHT = 2, + WHITE_KING = 3, + WHITE_BISHOP = 5, + WHITE_ROOK = 6, + WHITE_QUEEN = 7, + BLACK_PAWN = -1, + BLACK_KNIGHT = -2, + BLACK_KING = -3, + BLACK_BISHOP = -5, + BLACK_ROOK = -6, + BLACK_QUEEN = -7 +}; + +#endif //CHESSAI_CPP_PIECETYPE_H From 51f38ec78fcad01c3f796c909d6750a9f52a7b0d Mon Sep 17 00:00:00 2001 From: awo-dev Date: Tue, 17 Apr 2018 16:49:09 +0200 Subject: [PATCH 2/3] Starting work on square --- CMakeLists.txt | 2 +- main.cpp | 5 +++++ util/Square.cpp | 16 ++++++++++++++++ util/Square.h | 22 ++++++++++++++++++++++ util/SquareType.h | 20 ++++++++++++++++++++ util/static_block.h | 26 ++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 util/Square.cpp create mode 100644 util/Square.h create mode 100644 util/SquareType.h create mode 100644 util/static_block.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 67756a8..086f561 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(chessai_cpp) set(CMAKE_CXX_STANDARD 11) -add_executable(chessai_cpp main.cpp util/Color.h util/Piece.cpp util/Piece.h util/PieceType.h) \ No newline at end of file +add_executable(chessai_cpp main.cpp util/Color.h util/Piece.cpp util/Piece.h util/PieceType.h util/Square.cpp util/Square.h util/SquareType.h util/static_block.h) \ No newline at end of file diff --git a/main.cpp b/main.cpp index fc61374..4f8f696 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include "util/Color.h" #include "util/Piece.h" #include "util/PieceType.h" +#include "util/Square.h" int main() { std::cout << "Hello, World!" << std::endl; @@ -11,5 +12,9 @@ int main() { auto p = Piece(static_cast(PieceType::BLACK_QUEEN)); std::cout << "Letter is " << p.getLetter() << std::endl; + + Square s1 = Square(); + Square s2 = Square(); + return 0; } \ No newline at end of file diff --git a/util/Square.cpp b/util/Square.cpp new file mode 100644 index 0000000..dca86d9 --- /dev/null +++ b/util/Square.cpp @@ -0,0 +1,16 @@ +// +// Created by awo on 17-04-2018. +// + +#include "Square.h" +#include "static_block.h" + +#include + +Square::Square() { + +} + +static_block { + +} \ No newline at end of file diff --git a/util/Square.h b/util/Square.h new file mode 100644 index 0000000..750d5aa --- /dev/null +++ b/util/Square.h @@ -0,0 +1,22 @@ +// +// Created by awo on 17-04-2018. +// + +#ifndef CHESSAI_CPP_SQUARE_H +#define CHESSAI_CPP_SQUARE_H + + +#include +#include "SquareType.h" + +class Square { +public: + Square(); + SquareType type; +private: + const static std::map lookup; + int value; +}; + + +#endif //CHESSAI_CPP_SQUARE_H diff --git a/util/SquareType.h b/util/SquareType.h new file mode 100644 index 0000000..4c9e40c --- /dev/null +++ b/util/SquareType.h @@ -0,0 +1,20 @@ +// +// Created by awo on 17-04-2018. +// + +#ifndef CHESSAI_CPP_SQUARETYPE_H +#define CHESSAI_CPP_SQUARETYPE_H + +enum class SquareType { + NIL = -1, + A1 = 0, B1 = 1, C1 = 2, D1 = 3, E1 = 4, F1 = 5, G1 = 6, H1 = 7, + A2 = 16, B2 = 17, C2 = 18, D2 = 19, E2 = 20, F2 = 21, G2 = 22, H2 = 23, + A3 = 32, B3 = 33, C3 = 34, D3 = 35, E3 = 36, F3 = 37, G3 = 38, H3 = 39, + A4 = 48, B4 = 49, C4 = 50, D4 = 51, E4 = 52, F4 = 53, G4 = 54, H4 = 55, + A5 = 64, B5 = 65, C5 = 66, D5 = 67, E5 = 68, F5 = 69, G5 = 70, H5 = 71, + A6 = 80, B6 = 81, C6 = 82, D6 = 83, E6 = 84, F6 = 85, G6 = 86, H6 = 87, + A7 = 96, B7 = 97, C7 = 98, D7 = 99, E7 = 100, F7 = 101, G7 = 102, H7 = 103, + A8 = 112, B8 = 113, C8 = 114, D8 = 115, E8 = 116, F8 = 117, G8 = 118, H8 = 119 +}; + +#endif //CHESSAI_CPP_SQUARETYPE_H diff --git a/util/static_block.h b/util/static_block.h new file mode 100644 index 0000000..d2e340c --- /dev/null +++ b/util/static_block.h @@ -0,0 +1,26 @@ +// +// Created by awo on 17-04-2018. +// + +//#ifndef CHESSAI_CPP_STATIC_BLOCK_H +//#define CHESSAI_CPP_STATIC_BLOCK_H + +#define CONCATENATE(s1, s2) s1##s2 +#define EXPAND_THEN_CONCATENATE(s1, s2) CONCATENATE(s1, s2) +#ifdef __COUNTER__ +#define UNIQUE_IDENTIFIER(prefix) EXPAND_THEN_CONCATENATE(prefix, __COUNTER__) +#else +#define UNIQUE_IDENTIFIER(prefix) EXPAND_THEN_CONCATENATE(prefix, __LINE__) +#endif /* COUNTER */ + +#define static_block STATIC_BLOCK_IMPL1(UNIQUE_IDENTIFIER(_static_block_)) + +#define STATIC_BLOCK_IMPL1(prefix) \ + STATIC_BLOCK_IMPL2(CONCATENATE(prefix,_fn),CONCATENATE(prefix,_var)) + +#define STATIC_BLOCK_IMPL2(function_name,var_name) \ +static void function_name(); \ +static int var_name __attribute((unused)) = (function_name(), 0) ; \ +static void function_name() + +//#endif //CHESSAI_CPP_STATIC_BLOCK_H From c9147bd294c75b62782877c42aca2f594b248522 Mon Sep 17 00:00:00 2001 From: awo-dev Date: Tue, 17 Apr 2018 18:05:25 +0200 Subject: [PATCH 3/3] fuck this shit, going to java --- main.cpp | 9 +++++++++ util/Square.cpp | 25 +++++++++++++++++++++---- util/Square.h | 6 +++--- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/main.cpp b/main.cpp index 4f8f696..4466fbc 100644 --- a/main.cpp +++ b/main.cpp @@ -4,7 +4,12 @@ #include "util/PieceType.h" #include "util/Square.h" +#include + + int main() { +// Square::init(); // Should only be executed once! + std::cout << "Hello, World!" << std::endl; std::cout << "A king has a value of " << static_cast(PieceType::BLACK_QUEEN) << std::endl; @@ -16,5 +21,9 @@ int main() { Square s1 = Square(); Square s2 = Square(); + SquareType st = SquareType::F4; + std::cout << "Value of st: " << static_cast(st) << std::endl; + std::cout << "Value = " << static_cast(st) << std::endl; + return 0; } \ No newline at end of file diff --git a/util/Square.cpp b/util/Square.cpp index dca86d9..85e9fac 100644 --- a/util/Square.cpp +++ b/util/Square.cpp @@ -3,14 +3,31 @@ // #include "Square.h" -#include "static_block.h" #include -Square::Square() { +static bool initialized = false; +Square::Square() { + init(); } -static_block { +void Square::init() { + if (initialized) return; + initialized = true; + + auto row = -16; + auto arr[64] = + for (auto i = 0; i < 64; i++) { + auto num = (i % 8) + row; + SquareType iterator = SquareType[num]; -} \ No newline at end of file + if (i % 8 == 0) { + std::cout << std::endl; + row += 16; + } + + std::cout << num << " "; + } + std::cout << std::endl << std::endl; +} diff --git a/util/Square.h b/util/Square.h index 750d5aa..272076d 100644 --- a/util/Square.h +++ b/util/Square.h @@ -5,7 +5,6 @@ #ifndef CHESSAI_CPP_SQUARE_H #define CHESSAI_CPP_SQUARE_H - #include #include "SquareType.h" @@ -13,10 +12,11 @@ class Square { public: Square(); SquareType type; + private: - const static std::map lookup; + static std::map lookup; + static void init(); int value; }; - #endif //CHESSAI_CPP_SQUARE_H