Skip to content
Open
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ project(chessai_cpp)

set(CMAKE_CXX_STANDARD 11)

add_executable(chessai_cpp main.cpp)
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)
23 changes: 23 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
#include <iostream>
#include "util/Color.h"
#include "util/Piece.h"
#include "util/PieceType.h"
#include "util/Square.h"

#include <map>


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<int>(PieceType::BLACK_QUEEN) << std::endl;

auto p = Piece(static_cast<int>(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<int>(st) << std::endl;
std::cout << "Value = " << static_cast<int>(st) << std::endl;

return 0;
}
14 changes: 14 additions & 0 deletions util/Color.h
Original file line number Diff line number Diff line change
@@ -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
73 changes: 73 additions & 0 deletions util/Piece.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// Created by awo on 17-04-2018.
//

#include <algorithm>
#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<int>(PieceType::KING);
}

bool Piece::isQueen() {
return std::abs(value) == static_cast<int>(PieceType::QUEEN);
}

bool Piece::isRook() {
return std::abs(value) == static_cast<int>(PieceType::ROOK);
}

bool Piece::isBishop() {
return std::abs(value) == static_cast<int>(PieceType::BISHOP);
}

bool Piece::isKnight() {
return std::abs(value) == static_cast<int>(PieceType::KNIGHT);
}

bool Piece::isPawn() {
return std::abs(value) == static_cast<int>(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;
}
36 changes: 36 additions & 0 deletions util/Piece.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Created by awo on 17-04-2018.
//

#ifndef CHESSAI_CPP_PIECE_H
#define CHESSAI_CPP_PIECE_H

#include <string>
#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
30 changes: 30 additions & 0 deletions util/PieceType.h
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions util/Square.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Created by awo on 17-04-2018.
//

#include "Square.h"

#include <iostream>

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;
}
22 changes: 22 additions & 0 deletions util/Square.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Created by awo on 17-04-2018.
//

#ifndef CHESSAI_CPP_SQUARE_H
#define CHESSAI_CPP_SQUARE_H

#include <map>
#include "SquareType.h"

class Square {
public:
Square();
SquareType type;

private:
static std::map<int, Square> lookup;
static void init();
int value;
};

#endif //CHESSAI_CPP_SQUARE_H
20 changes: 20 additions & 0 deletions util/SquareType.h
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions util/static_block.h
Original file line number Diff line number Diff line change
@@ -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