diff --git a/CMakeLists.txt b/CMakeLists.txt index 350191d..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) \ 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 f253cbe..4466fbc 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,29 @@ #include +#include "util/Color.h" +#include "util/Piece.h" +#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; + + auto p = Piece(static_cast(PieceType::BLACK_QUEEN)); + + std::cout << "Letter is " << p.getLetter() << std::endl; + + 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/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 diff --git a/util/Square.cpp b/util/Square.cpp new file mode 100644 index 0000000..85e9fac --- /dev/null +++ b/util/Square.cpp @@ -0,0 +1,33 @@ +// +// Created by awo on 17-04-2018. +// + +#include "Square.h" + +#include + +static bool initialized = false; + +Square::Square() { + init(); +} + +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]; + + 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 new file mode 100644 index 0000000..272076d --- /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: + static std::map lookup; + static void init(); + 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