From 8320f7ec281891501a9b3092e20d990e7fd14700 Mon Sep 17 00:00:00 2001 From: zedarider Date: Fri, 13 Jan 2017 23:19:00 +0000 Subject: made it more object oriented but removed the board implementation --- include/chess_ai.hpp | 240 ----------------------------------------------- include/chess_piece.hpp | 104 +++++++++++++++++++++ src/bishop.cpp | 25 +++++ src/chess_board.cpp | 244 ------------------------------------------------ src/chess_piece.cpp | 61 +----------- src/king.cpp | 25 +++++ src/knight.cpp | 25 +++++ src/main.cpp.bck | 23 ----- src/pawn.cpp | 25 +++++ src/queen.cpp | 25 +++++ src/rook.cpp | 25 +++++ src/test_bench.cpp | 45 ++------- 12 files changed, 269 insertions(+), 598 deletions(-) delete mode 100644 include/chess_ai.hpp create mode 100644 include/chess_piece.hpp create mode 100644 src/bishop.cpp delete mode 100644 src/chess_board.cpp create mode 100644 src/king.cpp create mode 100644 src/knight.cpp delete mode 100644 src/main.cpp.bck create mode 100644 src/pawn.cpp create mode 100644 src/queen.cpp create mode 100644 src/rook.cpp diff --git a/include/chess_ai.hpp b/include/chess_ai.hpp deleted file mode 100644 index 06cbd43..0000000 --- a/include/chess_ai.hpp +++ /dev/null @@ -1,240 +0,0 @@ -#ifndef CHESS_AI_HPP -#define CHESS_AI_HPP - -#define CHESS_BOARD_SIZE 8 - -#include -#include -#include - -namespace chess_ai { - - // Describes the different types of chess pieces there are on the board - enum piece_type : unsigned; - - // Describes the colour of the pieces - enum piece_colour : unsigned; - - // just describes if the board is full or empty - enum board_state : unsigned; - - // defines the errors that can happen when moving a piece - enum move_error : unsigned; - - // The chess board that will be played on - class chess_board; - - // Any chess piece in the game - class chess_piece; - - // typedefs for iterators to access elements easier - - typedef std::vector >:: - iterator vector_iterator; - - typedef std::vector::iterator square_iterator; - - enum piece_type : unsigned { - // A pawn can only move forward twice on the first move, otherwise only - // once. It only take pieces that are on the diagonals in front of it, - // or through en passant. - // They are chess_initially placed as a line in front of the other pieces. - pawn, - - // A rook can only move vertically or horizontally and they are placed - // in the corners of the board. - rook, - - // A knight can only move to 8 locations around it which are specified - // by moving twice horizontally or vertically in a row and once - // diagonally. These are placed next to the rook. - knight, - - // A Bishop can only move diagonally and are placed next to the knight - bishop, - - // The queen combines the bishop and the rook and is placed on either - // d1 or d8 - queen, - - // The king moves like the queen but only for one square and is placed - // on e1 or e8 - king, - - // empty square - empty - }; - - enum piece_colour : unsigned { - // Looking at the board white will be at the bottom - white, - // Black will be at the top - black, - // no colour - none - }; - - enum board_state : unsigned { - // The starting position of the board with all pieces in the right - // position - initial, - // While the game is going this will be the state - playing, - // When the game is over - finished, - // When the board is completely empty - clear - }; - - enum move_error : unsigned { - // when king is in check there are limited possibilities - move_error_KingInCheckAfterMove, - // when there is a friendly piece in the way - move_error_FriendlyPieceOnDestination, - // when there is an enemy piece blocking the way - move_error_EnemyPieceOnDestination, - // illegal move for chose piece - move_error_IllegalMove, - - // when the move is successful - move_Success, - // when a move leads to a check - move_Check, - // when a move leads to a checkmate - move_Checkmate, - }; - - class chess_board { - public: - - // Create an empty chess board - chess_board(); - - // Create a chess board depending on the state - chess_board(board_state state); - - // destructor to clean up the variables - ~chess_board(); - - // prints the current board state - void print_board(); - - // returns the piece at the location - chess_piece at(unsigned x, unsigned y); - - // Set a piece somewhere on the board replacing anything that was there - // previously - void set_piece(chess_piece piece); - - // set piece directly on board - void set_piece(piece_type type, piece_colour colour, unsigned x, - unsigned y); - - // removes a piece from the board at location - void remove_piece(chess_piece piece); - - // remove piece at a specific location only - void remove_piece(unsigned x, unsigned y); - - // move a piece according to the chess rules - move_error move_piece(chess_piece piece); - - // move piece using only x and y (for pawns) - move_error move_piece(unsigned x, unsigned y); - - // moves a piece to an x, y coordinate - move_error move_piece(chess_piece piece, unsigned x, unsigned y); - - // move piece with x and y as original and final destination - move_error move_piece(unsigned orig_x, unsigned orig_y, - unsigned dest_x, unsigned dest_y); - - // move piece and return a piece that has been captured - move_error move_piece(unsigned orig_x, unsigned orig_y, - unsigned dest_x, unsigned dest_y, chess_piece& taken_piece); - - // iterate through the list and return the pointer to change - square_iterator& iterate_board(square_iterator& it, unsigned x, - unsigned y); - - - private: - - // initialises vector - void init_board_vector(); - - // moves the pawn and tests all the cases that it should. - move_error move_pawn(square_iterator it, square_iterator new_it, - chess_piece& taken_piece); - - // The size of the chess board is a constant and hence defined by a - // preprocessed define statement. - const unsigned SIZE; - - // The actual board where the values of the pieces will be changed. - std::vector > grid; - }; - - class chess_piece { - friend class chess_board; - public: - - // Initialises the chess piece to an empty square on the board - chess_piece() : type(empty), colour(none), x(0), y(0) {} - - // Otherwise initialise the chess piece to a king or queen where the - // location is alreay known - chess_piece(piece_type type, piece_colour colour); - - // Finally initialise the chess piece to a specified piece - chess_piece(piece_type type, piece_colour colour, unsigned x, - unsigned y); - - // destructor to clean up the variables - ~chess_piece(); - - // overload == operator - friend bool operator==(const chess_piece& p1, const chess_piece& p2) { - if(p1.type == p2.type && p1.colour == p2.colour && p1.x == p2.x && - p1.y == p2.y) { - - return true; - } - return false; - } - - // Set the type of the chess_piece - void set_type(piece_type type); - - // set the colour of the piece - void set_colour(piece_colour colour); - - // set the x coordinate of the piece - void set_x(unsigned x); - - // set the y coordinate of the piece - void set_y(unsigned y); - - // set the different values - void set(piece_type type, piece_colour colour, unsigned x, unsigned y); - - // return a printable version of the square - std::string str(); - - private: - - // Type of the chess piece, eg. bishop or queen - piece_type type; - - // Colour of the chess piece - piece_colour colour; - - // x location of the chess piece - unsigned x; - - // y location of the chess piece - unsigned y; - }; -} - -#endif diff --git a/include/chess_piece.hpp b/include/chess_piece.hpp new file mode 100644 index 0000000..6037b89 --- /dev/null +++ b/include/chess_piece.hpp @@ -0,0 +1,104 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * + * ----------------------------------------------------------------------------- + * + * Chess Piece class with header for all the pieces as well that inherit + * from the Chess Piece class. + * + */ + +#ifndef YMH_CHESS_PIECE_HPP +#define YMH_CHESS_PIECE_HPP + +namespace ymhChessAI { +class ChessPiece { +public: + ChessPiece(); + ChessPiece(const ChessPiece& other) = default; + ChessPiece(const int& x, const int& y); + virtual ~ChessPiece() = default; + + virtual void move(const int& x, const int& y) = 0; +protected: + int m_x, m_y; +private: +}; + +class King : public ChessPiece { +public: + King(); + King(const King& other) = default; + King(const int& x, const int& y); + ~King() = default; + + void move(const int& x, const int& y); +protected: +private: +}; + +class Queen : public ChessPiece { +public: + Queen(); + Queen(const Queen& other) = default; + Queen(const int& x, const int& y); + ~Queen() = default; + + void move(const int& x, const int& y); +protected: +private: +}; + +class Rook : public ChessPiece { +public: + Rook(); + Rook(const Rook& other) = default; + Rook(const int& x, const int& y); + ~Rook() = default; + + void move(const int& x, const int& y); +protected: +private: +}; + +class Bishop : public ChessPiece { +public: + Bishop(); + Bishop(const Bishop& other) = default; + Bishop(const int& x, const int& y); + ~Bishop() = default; + + void move(const int& x, const int& y); +protected: +private: +}; + +class Knight : public ChessPiece { +public: + Knight(); + Knight(const Knight& other) = default; + Knight(const int& x, const int& y); + ~Knight() = default; + + void move(const int& x, const int& y); +protected: +private: +}; + +class Pawn : public ChessPiece { +public: + Pawn(); + Pawn(const Pawn& other) = default; + Pawn(const int& x, const int& y); + ~Pawn() = default; + + void move(const int& x, const int& y); +protected: +private: +}; +}; + +#endif diff --git a/src/bishop.cpp b/src/bishop.cpp new file mode 100644 index 0000000..fc87db2 --- /dev/null +++ b/src/bishop.cpp @@ -0,0 +1,25 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * + * ----------------------------------------------------------------------------- + * + * Bishop class implementation + * + */ + +#include "chess_piece.hpp" + +using namespace ymhChessAI; + + +Bishop::Bishop() : ChessPiece() { +} + +Bishop::Bishop(const int& x, const int& y) : ChessPiece(x, y) { +} + +void Bishop::move(const int& x, const int& y) { +} diff --git a/src/chess_board.cpp b/src/chess_board.cpp deleted file mode 100644 index de21015..0000000 --- a/src/chess_board.cpp +++ /dev/null @@ -1,244 +0,0 @@ -#include "chess_ai.hpp" - -chess_ai::chess_board::chess_board() : SIZE(CHESS_BOARD_SIZE) { - init_board_vector(); -} - -chess_ai::chess_board::chess_board(board_state state) : SIZE(CHESS_BOARD_SIZE) { - unsigned vec_index, sqr_index; - - init_board_vector(); - - if(state == initial) { - - for(vector_iterator it_vec = grid.begin(); it_vec != grid.end(); - ++it_vec) { - - for(square_iterator it_sqr = (*it_vec).begin(); - it_sqr != (*it_vec).end(); ++it_sqr) { - chess_piece piece; - - piece_colour colour; - - if(vec_index < 3) { - colour = black; - } else { - colour = white; - } - - vec_index = it_vec - grid.begin(); - sqr_index = it_sqr - (*it_vec).begin(); - - if(vec_index == 1 || vec_index == 6) { - piece.set_type(pawn); - } else if(vec_index == 0 || vec_index == 7) { - - if(sqr_index == 0 || sqr_index == 7) - piece.set_type(rook); - else if(sqr_index == 1 || sqr_index == 6) - piece.set_type(knight); - else if(sqr_index == 2 || sqr_index == 5) - piece.set_type(bishop); - else if(sqr_index == 3) - piece.set_type(queen); - else if(sqr_index == 4) - piece.set_type(king); - } - - piece.set_colour(colour); - piece.set_x(sqr_index); - piece.set_y(vec_index); - - *it_sqr = piece; - } - } - } else { - for(auto& row : grid) { - for(auto& square : row) { - chess_piece piece; - square = piece; - } - } - } -} - -chess_ai::chess_board::~chess_board() {} - -void chess_ai::chess_board::init_board_vector() { - for(unsigned i = 0; i < SIZE; ++i) { - std::vector tmp_vec; - for(unsigned j = 0; j < SIZE; ++j) { - chess_piece piece; - tmp_vec.push_back(piece); - } - grid.push_back(tmp_vec); - } -} - -void chess_ai::chess_board::print_board() { - std::cout << " |"; - for(unsigned i = 0; i < SIZE; ++i) { - std::cout << "---|"; - } - std::cout << std::endl << "8 |"; - - for(unsigned y = 0; y < SIZE; ++y) { - for(unsigned x = 0; x < SIZE; ++x) { - std::cout << " " << grid[y][x].str() << " |"; - } - - std::cout << std::endl << " |"; - - - if(y == SIZE-1) { - std::cout << "---+"; - for(unsigned i = 0; i < SIZE-2; ++i) { - std::cout << "---+"; - } - std::cout << "---|" << std::endl; - } else { - for(unsigned i = 0; i < SIZE-1; ++i) { - std::cout << "---+"; - } - std::cout << "---|" << std::endl; - std::cout << 7-y << " |"; - } - } - std::cout << " a b c d e f g h" << std::endl; -} - -chess_ai::chess_piece chess_ai::chess_board::at(unsigned x, - unsigned y) { - - square_iterator it_sqr; - chess_piece tmp_piece; - - tmp_piece = *iterate_board(it_sqr, x, y); - return tmp_piece; -} - -void chess_ai::chess_board::set_piece(chess_piece piece) { - square_iterator it_sqr; - *iterate_board(it_sqr, piece.x, piece.y) = piece; -} - -void chess_ai::chess_board::set_piece(piece_type type, piece_colour colour, - unsigned x, unsigned y) { - - chess_piece piece(type, colour, x, y); - set_piece(piece); -} - -void chess_ai::chess_board::remove_piece(chess_piece piece) { - remove_piece(piece.x, piece.y); -} - -void chess_ai::chess_board::remove_piece(unsigned x, unsigned y) { - square_iterator it_sqr; - *iterate_board(it_sqr, x, y) = chess_piece(); -} - -chess_ai::move_error chess_ai::chess_board::move_piece(chess_piece piece) { - return move_piece(piece.x, piece.y); -} - -chess_ai::move_error chess_ai::chess_board::move_piece(unsigned x, unsigned y) { - square_iterator it; - iterate_board(it, x, y); - - if(it->y < 7 && it->y > 0) { - if(it->colour == white) - return move_piece(x, y, x, y-1); - return move_piece(x, y, x, y+1); - } - return move_error_IllegalMove; -} - -chess_ai::move_error chess_ai::chess_board::move_piece -(chess_ai::chess_piece piece, unsigned x, unsigned y) { - - return move_piece(piece.x, piece.y, x, y); -} - -chess_ai::move_error chess_ai::chess_board::move_piece -(unsigned orig_x, unsigned orig_y, unsigned dest_x, unsigned dest_y) { - - chess_piece taken; - return move_piece(orig_x, orig_y, dest_x, dest_y, taken); -} - -chess_ai::move_error chess_ai::chess_board::move_piece -(unsigned orig_x, unsigned orig_y, unsigned dest_x, unsigned dest_y, - chess_piece& taken_piece) { - - square_iterator it; - square_iterator new_it; - - iterate_board(it, orig_x, orig_y); - iterate_board(new_it, dest_x, dest_y); - - switch(it->type) { - case pawn: - return move_pawn(it, new_it, taken_piece); - case rook: - case knight: - case bishop: - case queen: - case king: - default: - return move_error_IllegalMove; - } -} - -chess_ai::square_iterator& chess_ai::chess_board::iterate_board -(square_iterator& it, unsigned x, unsigned y) { - - unsigned vec_index, sqr_index; - for(vector_iterator it_vec = grid.begin(); it_vec != grid.end(); ++it_vec) { - for(square_iterator it_sqr = (*it_vec).begin(); - it_sqr != (*it_vec).end(); ++it_sqr) { - vec_index = it_vec - grid.begin(); - sqr_index = it_sqr - (*it_vec).begin(); - - if(vec_index == y && sqr_index == x) { - it = it_sqr; - return it; - } - } - } - return it; -} - -chess_ai::move_error chess_ai::chess_board::move_pawn -(square_iterator it, square_iterator new_it, chess_piece& taken_piece) { - - chess_piece piece(it->type, it->colour, new_it->x, new_it->y); - - if((new_it->y - it->y == 2 && it->y == 1) || - ((int)new_it->y - (int)it->y == -2 && it->y == 6) || - (new_it->y - it->y == 1 && piece.colour == black && it->y < 7) || - ((int)new_it->y - (int)it->y == -1 && piece.colour == white && - it->y > 0)) { - if(new_it->x == it->x) { - if(new_it->type == empty) { - remove_piece(it->x, it->y); - set_piece(piece); - return move_Success; - } else if(new_it->colour == it->colour) { - return move_error_FriendlyPieceOnDestination; - } - return move_error_EnemyPieceOnDestination; - } else if(new_it->x - it->x == 1 || (int)new_it->x - (int)it->x == -1) { - if(new_it->colour != it->colour && new_it->colour != none) { - taken_piece.set(new_it->type, new_it->colour, new_it->x, - new_it->y); - remove_piece(it->x, it->y); - set_piece(piece); - return move_Success; - } else if(new_it->colour == it->colour) { - return move_error_FriendlyPieceOnDestination; - } - } - } - return move_error_IllegalMove; -} diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp index f73d4c1..8d4cb94 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -1,63 +1,12 @@ -#include "chess_ai.hpp" +#include "chess_piece.hpp" -chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour) : - type(type), colour(colour) { +using namespace ymhChessAI; - if(colour == black) { - y = 0; - } else { - y = 7; - } - if(type == king) { - x = 4; - } else { - x = 3; - } -} - -chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour, - unsigned x, unsigned y) : - type(type), colour(colour), x(x), y(y) {} - -chess_ai::chess_piece::~chess_piece() {} - -void chess_ai::chess_piece::set_type(piece_type type) { - this->type = type; -} - -void chess_ai::chess_piece::set_colour(piece_colour colour) { - this->colour = colour; -} - -void chess_ai::chess_piece::set_x(unsigned x) { - this->x = x; -} - -void chess_ai::chess_piece::set_y(unsigned y) { - this->y = y; -} +// Chess Piece Base class that all the pieces inherit from -void chess_ai::chess_piece::set(piece_type type, piece_colour colour, - unsigned x, unsigned y) { - set_type(type); - set_colour(colour); - set_x(x); - set_y(y); +ChessPiece::ChessPiece() : m_x(0), m_y(0) { } -std::string chess_ai::chess_piece::str() { - if(type == empty) - return " "; - else if(type == pawn) - return "p"; - else if(type == rook) - return "r"; - else if(type == knight) - return "n"; - else if(type == bishop) - return "b"; - else if(type == queen) - return "q"; - return "k"; +ChessPiece::ChessPiece(const int& x, const int& y) : m_x(x), m_y(y) { } diff --git a/src/king.cpp b/src/king.cpp new file mode 100644 index 0000000..0f332b7 --- /dev/null +++ b/src/king.cpp @@ -0,0 +1,25 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * + * ----------------------------------------------------------------------------- + * + * King class implementation + * + */ + +#include "chess_piece.hpp" + +using namespace ymhChessAI; + + +King::King() : ChessPiece() { +} + +King::King(const int& x, const int& y) : ChessPiece(x, y) { +} + +void King::move(const int& x, const int& y) { +} diff --git a/src/knight.cpp b/src/knight.cpp new file mode 100644 index 0000000..c40921f --- /dev/null +++ b/src/knight.cpp @@ -0,0 +1,25 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * + * ----------------------------------------------------------------------------- + * + * Knight class implementation + * + */ + +#include "chess_piece.hpp" + +using namespace ymhChessAI; + + +Knight::Knight() : ChessPiece() { +} + +Knight::Knight(const int& x, const int& y) : ChessPiece(x, y) { +} + +void Knight::move(const int& x, const int& y) { +} diff --git a/src/main.cpp.bck b/src/main.cpp.bck deleted file mode 100644 index 2fd9be2..0000000 --- a/src/main.cpp.bck +++ /dev/null @@ -1,23 +0,0 @@ -/* - - description: This is the main file for the chess_ai - - author: Yann Herklotz - date created: DD-MM-YYYY - - */ - -#include "chess_ai.hpp" -#include "chess_tester.hpp" - -#include - -using namespace std; -using namespace chess_ai; - -int main(int argc, char** argv) { - (void)argc; - (void)argv; - - return 0; -} diff --git a/src/pawn.cpp b/src/pawn.cpp new file mode 100644 index 0000000..c1f172f --- /dev/null +++ b/src/pawn.cpp @@ -0,0 +1,25 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * + * ----------------------------------------------------------------------------- + * + * Pawn class implementation + * + */ + +#include "chess_piece.hpp" + +using namespace ymhChessAI; + + +Pawn::Pawn() : ChessPiece() { +} + +Pawn::Pawn(const int& x, const int& y) : ChessPiece(x, y) { +} + +void Pawn::move(const int& x, const int& y) { +} diff --git a/src/queen.cpp b/src/queen.cpp new file mode 100644 index 0000000..d4e7ce0 --- /dev/null +++ b/src/queen.cpp @@ -0,0 +1,25 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * + * ----------------------------------------------------------------------------- + * + * Queen class implementation + * + */ + +#include "chess_piece.hpp" + +using namespace ymhChessAI; + + +Queen::Queen() : ChessPiece() { +} + +Queen::Queen(const int& x, const int& y) : ChessPiece(x, y) { +} + +void Queen::move(const int& x, const int& y) { +} diff --git a/src/rook.cpp b/src/rook.cpp new file mode 100644 index 0000000..166e114 --- /dev/null +++ b/src/rook.cpp @@ -0,0 +1,25 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * + * ----------------------------------------------------------------------------- + * + * Rook class implementation + * + */ + +#include "chess_piece.hpp" + +using namespace ymhChessAI; + + +Rook::Rook() : ChessPiece() { +} + +Rook::Rook(const int& x, const int& y) : ChessPiece(x, y) { +} + +void Rook::move(const int& x, const int& y) { +} diff --git a/src/test_bench.cpp b/src/test_bench.cpp index 68c93fb..861f51b 100644 --- a/src/test_bench.cpp +++ b/src/test_bench.cpp @@ -1,10 +1,10 @@ /* - - description: This is the test_bench file for the chess_ai - - author: Yann Herklotz - date created: DD-MM-YYYY - + * + * description: This is the test_bench file for the chess_ai + * + * author: Yann Herklotz + * date created: DD-MM-YYYY + * */ #include "chess_ai.hpp" @@ -15,34 +15,9 @@ using namespace std; using namespace chess_ai; -int main(int argc, char** argv) { - (void)argc; - (void)argv; - - int test_id; - bool passed; - - chess_tester test_bench; - - chess_board empty_board(clear); - chess_board initial_board(initial); - - test_bench.chess_begin_test_suite(); - - test_id = test_bench.chess_begin_test("PAWN"); - chess_piece piece(pawn, white, 1, 6); - empty_board.set_piece(piece); - empty_board.print_board(); - cout << empty_board.move_piece(1, 6, 1, 4) << endl; - piece.set(pawn, white, 1, 4); - if(empty_board.at(1, 4) == piece) - passed = true; - else - passed = false; - empty_board.print_board(); - test_bench.chess_end_test(test_id, passed); - - test_bench.chess_end_test_suite(); +int main(int argc, char **argv) { + (void)argc; + (void)argv; - return 0; + return 0; } -- cgit From 6fc3dfc10ce0e690b0de9c811e617dc76104db40 Mon Sep 17 00:00:00 2001 From: zedarider Date: Sat, 14 Jan 2017 00:09:29 +0000 Subject: adding a few small comments --- include/chess_piece.hpp | 4 ++++ src/test_bench.cpp | 19 ++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/include/chess_piece.hpp b/include/chess_piece.hpp index 6037b89..f03dad8 100644 --- a/include/chess_piece.hpp +++ b/include/chess_piece.hpp @@ -14,6 +14,10 @@ #ifndef YMH_CHESS_PIECE_HPP #define YMH_CHESS_PIECE_HPP + +// defining seperate namespace so that I don't have to worry with duplicate +// names + namespace ymhChessAI { class ChessPiece { public: diff --git a/src/test_bench.cpp b/src/test_bench.cpp index 861f51b..474c53b 100644 --- a/src/test_bench.cpp +++ b/src/test_bench.cpp @@ -1,23 +1,28 @@ /* * - * description: This is the test_bench file for the chess_ai + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com * - * author: Yann Herklotz - * date created: DD-MM-YYYY + * ----------------------------------------------------------------------------- + * + * Main file that tests the chess ai * */ -#include "chess_ai.hpp" #include "chess_tester.hpp" +#include "chess_piece.hpp" #include using namespace std; -using namespace chess_ai; +using namespace ymhChessAI; int main(int argc, char **argv) { - (void)argc; - (void)argv; + cout << "Program name: " << argv[0] << endl; + cout << "Arguments: " << argc - 1 << endl; + + cout << endl << "====== Executed Successfully ======" << endl; return 0; } -- cgit From a5bc94a36fe37fb20eba61516da3b6b58208537b Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Sat, 14 Jan 2017 14:17:26 +0000 Subject: added colour to pieces and comments --- include/chess_piece.hpp | 55 +++++++++++++++++++++++++++++++++++-------------- src/bishop.cpp | 2 +- src/chess_piece.cpp | 18 ++++++++++++---- src/king.cpp | 2 +- src/knight.cpp | 2 +- src/pawn.cpp | 2 +- src/queen.cpp | 2 +- src/rook.cpp | 2 +- 8 files changed, 60 insertions(+), 25 deletions(-) diff --git a/include/chess_piece.hpp b/include/chess_piece.hpp index f03dad8..60b6fe5 100644 --- a/include/chess_piece.hpp +++ b/include/chess_piece.hpp @@ -19,27 +19,51 @@ // names namespace ymhChessAI { +// Colour enum class so that we can have the colours in the Chess Piece and not +// using normal enums because of undefined errors + +enum class Colour { + White, + Black +}; + + +// Base Chess Piece class that is inherited by all other chess pieces + class ChessPiece { public: + // constructor sets the initial position of the chess piece to 0, 0 ChessPiece(); + // using the default constructor for the copy constructor ChessPiece(const ChessPiece& other) = default; - ChessPiece(const int& x, const int& y); + // initialise piece at a specific location on the board to save code + ChessPiece(const int& x, const int& y, const Colour& colour); + // using default virtual d, const Colour &colourfault; virtual ~ChessPiece() = default; + // pure virtual function to move a piece as it depends completely on the + // implementation of the piece virtual void move(const int& x, const int& y) = 0; protected: + // current location of the piece which is protected as it can still be + // inherited by the piece classes int m_x, m_y; -private: + + // defines what colour the piece is + Colour m_colour; }; + +// King class that provides the class for the king + class King : public ChessPiece { public: King(); King(const King& other) = default; - King(const int& x, const int& y); + King(const int& x, const int& y, const Colour& colour); ~King() = default; - void move(const int& x, const int& y); + virtual void move(const int& x, const int& y); protected: private: }; @@ -48,10 +72,10 @@ class Queen : public ChessPiece { public: Queen(); Queen(const Queen& other) = default; - Queen(const int& x, const int& y); + Queen(const int& x, const int& y, const Colour& colour); ~Queen() = default; - void move(const int& x, const int& y); + virtual void move(const int& x, const int& y); protected: private: }; @@ -60,22 +84,23 @@ class Rook : public ChessPiece { public: Rook(); Rook(const Rook& other) = default; - Rook(const int& x, const int& y); + Rook(const int& x, const int& y, const Colour& colour); ~Rook() = default; - void move(const int& x, const int& y); + virtual void move(const int& x, const int& y); protected: private: }; + class Bishop : public ChessPiece { public: Bishop(); - Bishop(const Bishop& other) = default; - Bishop(const int& x, const int& y); + Bishop(const Bishop &other) = default; + Bishop(const int& x, const int& y, const Colour& colour); ~Bishop() = default; - void move(const int& x, const int& y); + virtual void move(const int& x, const int& y); protected: private: }; @@ -84,10 +109,10 @@ class Knight : public ChessPiece { public: Knight(); Knight(const Knight& other) = default; - Knight(const int& x, const int& y); + Knight(const int& x, const int& y, const Colour& colour); ~Knight() = default; - void move(const int& x, const int& y); + virtual void move(const int& x, const int& y); protected: private: }; @@ -96,10 +121,10 @@ class Pawn : public ChessPiece { public: Pawn(); Pawn(const Pawn& other) = default; - Pawn(const int& x, const int& y); + Pawn(const int& x, const int& y, const Colour& colour); ~Pawn() = default; - void move(const int& x, const int& y); + virtual void move(const int& x, const int& y); protected: private: }; diff --git a/src/bishop.cpp b/src/bishop.cpp index fc87db2..4894be5 100644 --- a/src/bishop.cpp +++ b/src/bishop.cpp @@ -18,7 +18,7 @@ using namespace ymhChessAI; Bishop::Bishop() : ChessPiece() { } -Bishop::Bishop(const int& x, const int& y) : ChessPiece(x, y) { +Bishop::Bishop(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { } void Bishop::move(const int& x, const int& y) { diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp index 8d4cb94..ee307e2 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -1,12 +1,22 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * + * ----------------------------------------------------------------------------- + * + * Chess Piece class implementation + * + */ + #include "chess_piece.hpp" using namespace ymhChessAI; -// Chess Piece Base class that all the pieces inherit from - -ChessPiece::ChessPiece() : m_x(0), m_y(0) { +ChessPiece::ChessPiece() : m_x(0), m_y(0), m_colour(Colour::White) { } -ChessPiece::ChessPiece(const int& x, const int& y) : m_x(x), m_y(y) { +ChessPiece::ChessPiece(const int& x, const int& y, const Colour& colour) : m_x(x), m_y(y), m_colour(colour) { } diff --git a/src/king.cpp b/src/king.cpp index 0f332b7..8e8a6b9 100644 --- a/src/king.cpp +++ b/src/king.cpp @@ -18,7 +18,7 @@ using namespace ymhChessAI; King::King() : ChessPiece() { } -King::King(const int& x, const int& y) : ChessPiece(x, y) { +King::King(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { } void King::move(const int& x, const int& y) { diff --git a/src/knight.cpp b/src/knight.cpp index c40921f..5c2a946 100644 --- a/src/knight.cpp +++ b/src/knight.cpp @@ -18,7 +18,7 @@ using namespace ymhChessAI; Knight::Knight() : ChessPiece() { } -Knight::Knight(const int& x, const int& y) : ChessPiece(x, y) { +Knight::Knight(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { } void Knight::move(const int& x, const int& y) { diff --git a/src/pawn.cpp b/src/pawn.cpp index c1f172f..e81959f 100644 --- a/src/pawn.cpp +++ b/src/pawn.cpp @@ -18,7 +18,7 @@ using namespace ymhChessAI; Pawn::Pawn() : ChessPiece() { } -Pawn::Pawn(const int& x, const int& y) : ChessPiece(x, y) { +Pawn::Pawn(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { } void Pawn::move(const int& x, const int& y) { diff --git a/src/queen.cpp b/src/queen.cpp index d4e7ce0..d685548 100644 --- a/src/queen.cpp +++ b/src/queen.cpp @@ -18,7 +18,7 @@ using namespace ymhChessAI; Queen::Queen() : ChessPiece() { } -Queen::Queen(const int& x, const int& y) : ChessPiece(x, y) { +Queen::Queen(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { } void Queen::move(const int& x, const int& y) { diff --git a/src/rook.cpp b/src/rook.cpp index 166e114..cba38a9 100644 --- a/src/rook.cpp +++ b/src/rook.cpp @@ -18,7 +18,7 @@ using namespace ymhChessAI; Rook::Rook() : ChessPiece() { } -Rook::Rook(const int& x, const int& y) : ChessPiece(x, y) { +Rook::Rook(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { } void Rook::move(const int& x, const int& y) { -- cgit From 1f138d96ab7f517e7842710ef3428f67a2965877 Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Sun, 15 Jan 2017 19:44:15 +0000 Subject: creating the board --- include/chess_board.hpp | 42 ++++++++++++++++++++++++++++++++++++++++++ include/chess_piece.hpp | 19 +++++++++++++++++-- src/bishop.cpp | 1 + src/chess_board.cpp | 34 ++++++++++++++++++++++++++++++++++ src/chess_piece.cpp | 1 + src/king.cpp | 3 +++ src/knight.cpp | 1 + src/pawn.cpp | 1 + src/queen.cpp | 1 + src/rook.cpp | 1 + src/test_bench.cpp | 6 ++++++ 11 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 include/chess_board.hpp create mode 100644 src/chess_board.cpp diff --git a/include/chess_board.hpp b/include/chess_board.hpp new file mode 100644 index 0000000..fef1f69 --- /dev/null +++ b/include/chess_board.hpp @@ -0,0 +1,42 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * date created: 14/01/17 + * + * ----------------------------------------------------------------------------- + * + * Chess Board class that will have all the chess pieces on it + * + */ + +#ifndef YMH_CHESS_BOARD_HPP +#define YMH_CHESS_BOARD_HPP + +#include "chess_piece.hpp" + +#include +#include + +namespace ymhChessAI { +typedef std::vector >::iterator boardIterator; +typedef std::vector > boardVector; +typedef std::unique_ptr chessPiecePtr; + +class ChessBoard { +public: + ChessBoard(); + ChessBoard(const ChessBoard& other) = default; + virtual ~ChessBoard() = default; + + void printBoard(); +protected: +private: + static const unsigned CHESS_BOARD_SIZE = 64; + + boardVector board; +}; +} + +#endif diff --git a/include/chess_piece.hpp b/include/chess_piece.hpp index 60b6fe5..ce6ca5c 100644 --- a/include/chess_piece.hpp +++ b/include/chess_piece.hpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * @@ -54,7 +55,7 @@ protected: }; -// King class that provides the class for the king +// King class class King : public ChessPiece { public: @@ -68,6 +69,9 @@ protected: private: }; + +// Queen class + class Queen : public ChessPiece { public: Queen(); @@ -80,6 +84,9 @@ protected: private: }; + +// Rook class + class Rook : public ChessPiece { public: Rook(); @@ -93,6 +100,8 @@ private: }; +// Bishop class + class Bishop : public ChessPiece { public: Bishop(); @@ -105,6 +114,9 @@ protected: private: }; + +// Knight class + class Knight : public ChessPiece { public: Knight(); @@ -117,6 +129,9 @@ protected: private: }; + +// Pawn class + class Pawn : public ChessPiece { public: Pawn(); @@ -128,6 +143,6 @@ public: protected: private: }; -}; +} #endif diff --git a/src/bishop.cpp b/src/bishop.cpp index 4894be5..849db19 100644 --- a/src/bishop.cpp +++ b/src/bishop.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * diff --git a/src/chess_board.cpp b/src/chess_board.cpp new file mode 100644 index 0000000..30c8aff --- /dev/null +++ b/src/chess_board.cpp @@ -0,0 +1,34 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * date created: 14/01/17 + * + * ----------------------------------------------------------------------------- + * + * Chess Board class implementation + * + */ + +#include "chess_board.hpp" +#include +using namespace ymhChessAI; + + +ChessBoard::ChessBoard() { + board.reserve(CHESS_BOARD_SIZE); + + for(auto&& piece : board) + piece = std::unique_ptr(new King); + + // for(unsigned i = 0; i < CHESS_BOARD_SIZE; ++i) + // board.emplace_back(new King); +} + +void ChessBoard::printBoard() { + for(auto&& piece : board) { + piece->move(1, 2); + std::cout << "Hello" << std::endl; + } +} diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp index ee307e2..626377e 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * diff --git a/src/king.cpp b/src/king.cpp index 8e8a6b9..1d297d3 100644 --- a/src/king.cpp +++ b/src/king.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * @@ -11,6 +12,7 @@ */ #include "chess_piece.hpp" +#include using namespace ymhChessAI; @@ -22,4 +24,5 @@ King::King(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, } void King::move(const int& x, const int& y) { + std::cout << "This is the king" << std::endl; } diff --git a/src/knight.cpp b/src/knight.cpp index 5c2a946..6b7f5ac 100644 --- a/src/knight.cpp +++ b/src/knight.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * diff --git a/src/pawn.cpp b/src/pawn.cpp index e81959f..1374886 100644 --- a/src/pawn.cpp +++ b/src/pawn.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * diff --git a/src/queen.cpp b/src/queen.cpp index d685548..897237a 100644 --- a/src/queen.cpp +++ b/src/queen.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * diff --git a/src/rook.cpp b/src/rook.cpp index cba38a9..cd72029 100644 --- a/src/rook.cpp +++ b/src/rook.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * diff --git a/src/test_bench.cpp b/src/test_bench.cpp index 474c53b..ab23d6c 100644 --- a/src/test_bench.cpp +++ b/src/test_bench.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * @@ -12,6 +13,7 @@ #include "chess_tester.hpp" #include "chess_piece.hpp" +#include "chess_board.hpp" #include @@ -22,6 +24,10 @@ int main(int argc, char **argv) { cout << "Program name: " << argv[0] << endl; cout << "Arguments: " << argc - 1 << endl; + ChessBoard cb; + + cb.printBoard(); + cout << endl << "====== Executed Successfully ======" << endl; return 0; -- cgit From 088b948122cbf24dd9dc1dfedf4be2724bab2157 Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Mon, 16 Jan 2017 01:12:25 +0000 Subject: backing up files, with parser changes --- include/board_state_parser.hpp | 44 +++++++++++++++++++++++++++++ include/chess_board.hpp | 4 +-- include/chess_piece.hpp | 16 ----------- res/default.board | 4 +++ src/board_state_parser.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++ src/chess_board.cpp | 13 ++++----- src/knight.cpp | 2 ++ src/test_bench.cpp | 5 +--- 8 files changed, 120 insertions(+), 31 deletions(-) create mode 100644 include/board_state_parser.hpp create mode 100644 res/default.board create mode 100644 src/board_state_parser.cpp diff --git a/include/board_state_parser.hpp b/include/board_state_parser.hpp new file mode 100644 index 0000000..39286d4 --- /dev/null +++ b/include/board_state_parser.hpp @@ -0,0 +1,44 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * date created: 15/01/17 + * + * ----------------------------------------------------------------------------- + * + * Parser that takes in the board state and outputs it into a format that the + * Chess Board class can read + * + */ + +#ifndef YMH_BOARD_STATE_PARSER_HPP +#define YMH_BOARD_STATE_PARSER_HPP + +#include +#include +#include + +namespace ymhChessAI { +typedef std::vector boardStateData; + +class BoardStateParser { +public: + BoardStateParser(); + BoardStateParser(const std::string& boardFileName); + ~BoardStateParser(); + + void populateBoardState(); + + static unsigned stringToInt(const std::string& str); + static std::string getStateFromLine(const std::string& str); +protected: +private: + static const unsigned BOARD_SIZE = 64; + + boardStateData boardStateVector; + std::ifstream boardFile; +}; +} + +#endif diff --git a/include/chess_board.hpp b/include/chess_board.hpp index fef1f69..d3516fc 100644 --- a/include/chess_board.hpp +++ b/include/chess_board.hpp @@ -27,13 +27,11 @@ typedef std::unique_ptr chessPiecePtr; class ChessBoard { public: ChessBoard(); - ChessBoard(const ChessBoard& other) = default; - virtual ~ChessBoard() = default; void printBoard(); protected: private: - static const unsigned CHESS_BOARD_SIZE = 64; + static const unsigned BOARD_SIZE = 64; boardVector board; }; diff --git a/include/chess_piece.hpp b/include/chess_piece.hpp index ce6ca5c..5febc99 100644 --- a/include/chess_piece.hpp +++ b/include/chess_piece.hpp @@ -35,12 +35,8 @@ class ChessPiece { public: // constructor sets the initial position of the chess piece to 0, 0 ChessPiece(); - // using the default constructor for the copy constructor - ChessPiece(const ChessPiece& other) = default; // initialise piece at a specific location on the board to save code ChessPiece(const int& x, const int& y, const Colour& colour); - // using default virtual d, const Colour &colourfault; - virtual ~ChessPiece() = default; // pure virtual function to move a piece as it depends completely on the // implementation of the piece @@ -60,9 +56,7 @@ protected: class King : public ChessPiece { public: King(); - King(const King& other) = default; King(const int& x, const int& y, const Colour& colour); - ~King() = default; virtual void move(const int& x, const int& y); protected: @@ -75,9 +69,7 @@ private: class Queen : public ChessPiece { public: Queen(); - Queen(const Queen& other) = default; Queen(const int& x, const int& y, const Colour& colour); - ~Queen() = default; virtual void move(const int& x, const int& y); protected: @@ -90,9 +82,7 @@ private: class Rook : public ChessPiece { public: Rook(); - Rook(const Rook& other) = default; Rook(const int& x, const int& y, const Colour& colour); - ~Rook() = default; virtual void move(const int& x, const int& y); protected: @@ -105,9 +95,7 @@ private: class Bishop : public ChessPiece { public: Bishop(); - Bishop(const Bishop &other) = default; Bishop(const int& x, const int& y, const Colour& colour); - ~Bishop() = default; virtual void move(const int& x, const int& y); protected: @@ -120,9 +108,7 @@ private: class Knight : public ChessPiece { public: Knight(); - Knight(const Knight& other) = default; Knight(const int& x, const int& y, const Colour& colour); - ~Knight() = default; virtual void move(const int& x, const int& y); protected: @@ -135,9 +121,7 @@ private: class Pawn : public ChessPiece { public: Pawn(); - Pawn(const Pawn& other) = default; Pawn(const int& x, const int& y, const Colour& colour); - ~Pawn() = default; virtual void move(const int& x, const int& y); protected: diff --git a/res/default.board b/res/default.board new file mode 100644 index 0000000..d4ea5b0 --- /dev/null +++ b/res/default.board @@ -0,0 +1,4 @@ +0 0br 1bn 2bb 3bq 4bk 5bb 6bn 7br +1 0bp 1bp 2bp 3bp 4bp 5bp 6bp 7bp +6 0wp 1wp 2wp 3wp 4wp 5wp 6wp 7wp +7 0wr 1wn 2wb 3wq 4wk 5wb 6wn 7wr diff --git a/src/board_state_parser.cpp b/src/board_state_parser.cpp new file mode 100644 index 0000000..fac502f --- /dev/null +++ b/src/board_state_parser.cpp @@ -0,0 +1,63 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * date created: 15/01/17 + * + * ----------------------------------------------------------------------------- + * + * Implementation of the Board state class + * + */ + +#include "board_state_parser.hpp" + +using namespace ymhChessAI; + + +BoardStateParser::BoardStateParser() : boardFile("res/default.board", std::ifstream::in) { +} + +BoardStateParser::BoardStateParser(const std::string& boardFileName) : boardFile(boardFileName.c_str()) { +} + +BoardStateParser::~BoardStateParser() { + boardFile.close(); +} + +void BoardStateParser::populateBoardState() { + std::string currentLine; + unsigned index; + + boardStateVector.reserve(BOARD_SIZE); + + if(!boardFile.is_open()) + throw "Error: Can't open file"; + + while(getline(boardFile, currentLine)) + boardStateVector[stringToInt(currentLine)] = getStateFromLine(currentLine); +} + +unsigned BoardStateParser::stringToInt(const std::string& str) { + unsigned finalInt = 0; + bool foundWhiteSpace = false; + + for(char character : str) + if(character == ' ') + foundWhiteSpace = true; + else if(!foundWhiteSpace && character >= '0' && character <= '9') + finalInt = finalInt * 10 + character - '0'; + + return finalInt; +} + +std::string BoardStateParser::getStateFromLine(const std::string& str) { + std::string state; + + for(unsigned i = 0; i < str.size(); ++i) + if(i > 1) + state += str[i]; + + return state; +} diff --git a/src/chess_board.cpp b/src/chess_board.cpp index 30c8aff..1eaa7d4 100644 --- a/src/chess_board.cpp +++ b/src/chess_board.cpp @@ -13,22 +13,19 @@ #include "chess_board.hpp" #include + using namespace ymhChessAI; ChessBoard::ChessBoard() { - board.reserve(CHESS_BOARD_SIZE); - - for(auto&& piece : board) - piece = std::unique_ptr(new King); - - // for(unsigned i = 0; i < CHESS_BOARD_SIZE; ++i) - // board.emplace_back(new King); } void ChessBoard::printBoard() { + int i = 0; + for(auto&& piece : board) { + std::cout << i << " "; piece->move(1, 2); - std::cout << "Hello" << std::endl; + ++i; } } diff --git a/src/knight.cpp b/src/knight.cpp index 6b7f5ac..25e5ab9 100644 --- a/src/knight.cpp +++ b/src/knight.cpp @@ -12,6 +12,7 @@ */ #include "chess_piece.hpp" +#include using namespace ymhChessAI; @@ -23,4 +24,5 @@ Knight::Knight(const int& x, const int& y, const Colour& colour) : ChessPiece(x, } void Knight::move(const int& x, const int& y) { + std::cout << "This is a Knight" << std::endl; } diff --git a/src/test_bench.cpp b/src/test_bench.cpp index ab23d6c..a68b60c 100644 --- a/src/test_bench.cpp +++ b/src/test_bench.cpp @@ -14,6 +14,7 @@ #include "chess_tester.hpp" #include "chess_piece.hpp" #include "chess_board.hpp" +#include "board_state_parser.hpp" #include @@ -24,10 +25,6 @@ int main(int argc, char **argv) { cout << "Program name: " << argv[0] << endl; cout << "Arguments: " << argc - 1 << endl; - ChessBoard cb; - - cb.printBoard(); - cout << endl << "====== Executed Successfully ======" << endl; return 0; -- cgit From f0799aef3ff8c4a8e8ffea79c963b8f128c462b9 Mon Sep 17 00:00:00 2001 From: zedarider Date: Tue, 17 Jan 2017 15:42:18 +0000 Subject: fixed the chess board and added empty pieces to fill holes, also added forward declarations --- include/board_state_parser.hpp | 2 + include/chess_board.hpp | 1 + include/chess_piece.hpp | 20 ++++++++ include/chess_tester.hpp | 105 ++++++++++++++++++++--------------------- src/chess_board.cpp | 7 +++ src/chess_piece.cpp | 9 ++++ src/test_bench.cpp | 4 ++ 7 files changed, 95 insertions(+), 53 deletions(-) diff --git a/include/board_state_parser.hpp b/include/board_state_parser.hpp index 39286d4..d4940db 100644 --- a/include/board_state_parser.hpp +++ b/include/board_state_parser.hpp @@ -20,6 +20,8 @@ #include namespace ymhChessAI { +class BoardStateParser; + typedef std::vector boardStateData; class BoardStateParser { diff --git a/include/chess_board.hpp b/include/chess_board.hpp index d3516fc..c277618 100644 --- a/include/chess_board.hpp +++ b/include/chess_board.hpp @@ -28,6 +28,7 @@ class ChessBoard { public: ChessBoard(); + void populateBoard(); void printBoard(); protected: private: diff --git a/include/chess_piece.hpp b/include/chess_piece.hpp index 5febc99..e7f2f79 100644 --- a/include/chess_piece.hpp +++ b/include/chess_piece.hpp @@ -20,6 +20,16 @@ // names namespace ymhChessAI { +enum class Colour; +class ChessPiece; +class King; +class Queen; +class Rook; +class Bishop; +class Knight; +class Pawn; +class EmptyPiece; + // Colour enum class so that we can have the colours in the Chess Piece and not // using normal enums because of undefined errors @@ -127,6 +137,16 @@ public: protected: private: }; + +class EmptyPiece : public ChessPiece { +public: + EmptyPiece(); + EmptyPiece(const int& x, const int& y, const Colour& colour); + + virtual void move(const int& x, const int& y); +protected: +private: +}; } #endif diff --git a/include/chess_tester.hpp b/include/chess_tester.hpp index d2994ad..f121ec8 100644 --- a/include/chess_tester.hpp +++ b/include/chess_tester.hpp @@ -11,79 +11,78 @@ #include #include +namespace ymhChessAI { struct tested_pieces { - std::string piece_name; - int num_passed; - int num_failed; - - friend bool operator==(const tested_pieces& tp1, const tested_pieces& tp2) { - if(tp1.piece_name == tp2.piece_name) { - return true; - } - return false; - } - - friend bool operator<(const tested_pieces& tp1, const tested_pieces& tp2) { - if(tp1.piece_name < tp2.piece_name) { - return true; - } - return false; - } + std::string piece_name; + int num_passed; + int num_failed; + + friend bool operator==(const tested_pieces& tp1, const tested_pieces& tp2) { + if(tp1.piece_name == tp2.piece_name) + return true; + return false; + } + + friend bool operator<(const tested_pieces& tp1, const tested_pieces& tp2) { + if(tp1.piece_name < tp2.piece_name) + return true; + return false; + } }; class chess_tester { - public: - // sets all the initial values for the tester - chess_tester(); + // sets all the initial values for the tester + chess_tester(); - // begins the test suite so that this class can record the results and - // analyse them - void chess_begin_test_suite(); + // begins the test suite so that this class can record the results and + // analyse them + void chess_begin_test_suite(); - // begins the test - int chess_begin_test(std::string test_name); + // begins the test + int chess_begin_test(std::string test_name); - // records the result of the test - void chess_end_test(int test_id, bool passed); + // records the result of the test + void chess_end_test(int test_id, bool passed); - // analyses the results and prints them out - void chess_end_test_suite(); + // analyses the results and prints them out + void chess_end_test_suite(); - // see if the test is acceptable - bool is_in_arr(const std::string& test_piece) const; + // see if the test is acceptable + bool is_in_arr(const std::string& test_piece) const; - // see if test is in vector - bool is_in_vec(const tested_pieces& piece) const; + // see if test is in vector + bool is_in_vec(const tested_pieces& piece) const; private: - // checks if the test suite is running - bool ts_begin; + // checks if the test suite is running + bool ts_begin; - // list of pieces to test - const std::string chess_test_pieces[CHESS_TEST_SIZE] = { - "", - "PAWN", - "ROOK", - "KNIGHT", - "BISHOP", - "QUEEN", - "KING" - }; + // list of pieces to test + const std::string chess_test_pieces[CHESS_TEST_SIZE] = { + "", + "PAWN", + "ROOK", + "KNIGHT", + "BISHOP", + "QUEEN", + "KING" + }; - // vector that contains the tested pieces - std::vector test_piece; + // vector that contains the tested pieces + std::vector test_piece; - // the test_id of the current instruction - int test_id; + // the test_id of the current instruction + int test_id; - // the test id of the test that has been started - int test_id_test; + // the test id of the test that has been started + int test_id_test; - // the current test that is being tested - std::string current_test; + // the current test that is being tested + std::string current_test; }; +} #endif diff --git a/src/chess_board.cpp b/src/chess_board.cpp index 1eaa7d4..3432b52 100644 --- a/src/chess_board.cpp +++ b/src/chess_board.cpp @@ -18,6 +18,13 @@ using namespace ymhChessAI; ChessBoard::ChessBoard() { + for(unsigned i = 0; i < BOARD_SIZE; ++i) + board.emplace_back(new EmptyPiece); +} + +void ChessBoard::populateBoard() { + for(auto&& piece : board) + piece = chessPiecePtr(new Knight); } void ChessBoard::printBoard() { diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp index 626377e..d4d14e6 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -21,3 +21,12 @@ ChessPiece::ChessPiece() : m_x(0), m_y(0), m_colour(Colour::White) { ChessPiece::ChessPiece(const int& x, const int& y, const Colour& colour) : m_x(x), m_y(y), m_colour(colour) { } + +EmptyPiece::EmptyPiece() : ChessPiece() { +} + +EmptyPiece::EmptyPiece(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { +} + +void EmptyPiece::move(const int& x, const int& y) { +} diff --git a/src/test_bench.cpp b/src/test_bench.cpp index a68b60c..b1fe4ba 100644 --- a/src/test_bench.cpp +++ b/src/test_bench.cpp @@ -25,6 +25,10 @@ int main(int argc, char **argv) { cout << "Program name: " << argv[0] << endl; cout << "Arguments: " << argc - 1 << endl; + ChessBoard cb; + cb.populateBoard(); + cb.printBoard(); + cout << endl << "====== Executed Successfully ======" << endl; return 0; -- cgit From 9a457557caed9ef614456a9c8139c89084381ed3 Mon Sep 17 00:00:00 2001 From: zedarider Date: Tue, 17 Jan 2017 15:48:32 +0000 Subject: made it more readable --- include/chess_board.hpp | 6 +++--- include/chess_piece.hpp | 4 +++- src/chess_board.cpp | 7 +++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/include/chess_board.hpp b/include/chess_board.hpp index c277618..ac96323 100644 --- a/include/chess_board.hpp +++ b/include/chess_board.hpp @@ -28,11 +28,11 @@ class ChessBoard { public: ChessBoard(); - void populateBoard(); - void printBoard(); + bool populateBoard(); + bool printBoard(); protected: private: - static const unsigned BOARD_SIZE = 64; + static const std::size_t BOARD_SIZE = 64; boardVector board; }; diff --git a/include/chess_piece.hpp b/include/chess_piece.hpp index e7f2f79..5b2c1c2 100644 --- a/include/chess_piece.hpp +++ b/include/chess_piece.hpp @@ -30,6 +30,8 @@ class Knight; class Pawn; class EmptyPiece; +typedef unsigned int boardPosition; + // Colour enum class so that we can have the colours in the Chess Piece and not // using normal enums because of undefined errors @@ -54,7 +56,7 @@ public: protected: // current location of the piece which is protected as it can still be // inherited by the piece classes - int m_x, m_y; + boardPosition m_x, m_y; // defines what colour the piece is Colour m_colour; diff --git a/src/chess_board.cpp b/src/chess_board.cpp index 3432b52..051decd 100644 --- a/src/chess_board.cpp +++ b/src/chess_board.cpp @@ -22,12 +22,13 @@ ChessBoard::ChessBoard() { board.emplace_back(new EmptyPiece); } -void ChessBoard::populateBoard() { +bool ChessBoard::populateBoard() { for(auto&& piece : board) piece = chessPiecePtr(new Knight); + return true; } -void ChessBoard::printBoard() { +bool ChessBoard::printBoard() { int i = 0; for(auto&& piece : board) { @@ -35,4 +36,6 @@ void ChessBoard::printBoard() { piece->move(1, 2); ++i; } + + return true; } -- cgit From a42ecf7b9b42d6f0d8e0e4ea6c35a09e872f5c8d Mon Sep 17 00:00:00 2001 From: zedarider Date: Tue, 17 Jan 2017 15:50:30 +0000 Subject: added forward declaration for board --- include/chess_board.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/chess_board.hpp b/include/chess_board.hpp index ac96323..e5e4d7b 100644 --- a/include/chess_board.hpp +++ b/include/chess_board.hpp @@ -20,6 +20,8 @@ #include namespace ymhChessAI { +class ChessBoard; + typedef std::vector >::iterator boardIterator; typedef std::vector > boardVector; typedef std::unique_ptr chessPiecePtr; -- cgit From 863c4617d4d0048d75910b87deab8edfcfaff6a9 Mon Sep 17 00:00:00 2001 From: zedarider Date: Tue, 17 Jan 2017 15:53:29 +0000 Subject: added size_t --- include/board_state_parser.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/board_state_parser.hpp b/include/board_state_parser.hpp index d4940db..d98ac4f 100644 --- a/include/board_state_parser.hpp +++ b/include/board_state_parser.hpp @@ -36,7 +36,7 @@ public: static std::string getStateFromLine(const std::string& str); protected: private: - static const unsigned BOARD_SIZE = 64; + static const std::size_t BOARD_SIZE = 64; boardStateData boardStateVector; std::ifstream boardFile; -- cgit From 560cca06326b3d93ba095fde2fbc14bd4c122f00 Mon Sep 17 00:00:00 2001 From: zedarider Date: Tue, 17 Jan 2017 22:49:49 +0000 Subject: fixed the chess tester --- src/chess_tester.cpp | 187 +++++++++++++++++++++++++-------------------------- 1 file changed, 91 insertions(+), 96 deletions(-) diff --git a/src/chess_tester.cpp b/src/chess_tester.cpp index 6f3745c..9e20fd3 100644 --- a/src/chess_tester.cpp +++ b/src/chess_tester.cpp @@ -1,125 +1,120 @@ #include "chess_tester.hpp" -chess_tester::chess_tester() : ts_begin(false) { - srand(time(NULL)); +using namespace ymhChessAI; - test_id = rand() % 0xffffffff; - test_id_test = test_id; +chess_tester::chess_tester() : ts_begin(false) { + srand(time(NULL)); + test_id = rand() % 0xffffffff; + test_id_test = test_id; } void chess_tester::chess_begin_test_suite() { - if(ts_begin) { - fprintf(stderr, "Error: already started test suite\n"); - exit(1); - } + if(ts_begin) { + fprintf(stderr, "Error: already started test suite\n"); + exit(1); + } - ts_begin = true; + ts_begin = true; } int chess_tester::chess_begin_test(std::string test_name) { - if(!ts_begin) { - fprintf(stderr, "Error: the test suite hasn't been started yet\n"); - exit(1); - } + if(!ts_begin) { + fprintf(stderr, "Error: the test suite hasn't been started yet\n"); + exit(1); + } - if(!is_in_arr(test_name)) { - fprintf(stderr, "Error: the test name does not exist\n"); - exit(1); - } + if(!is_in_arr(test_name)) { + fprintf(stderr, "Error: the test name does not exist\n"); + exit(1); + } - current_test = test_name; + current_test = test_name; - test_id = rand() % 0xffffffff; - test_id_test = test_id; + test_id = rand() % 0xffffffff; + test_id_test = test_id; - return test_id; + return test_id; } void chess_tester::chess_end_test(int test_id, bool passed) { - if(test_id != test_id_test) { - fprintf(stderr , "Error: the test from before has not been ended\n"); - exit(1); - } - - tested_pieces tmp; - tmp.piece_name = current_test; - - if(passed) { - tmp.num_passed = 1; - tmp.num_failed = 0; - } else { - tmp.num_passed = 0; - tmp.num_failed = 1; - } - - if(test_piece.empty() || !is_in_vec(tmp)) { - test_piece.push_back(tmp); - } else { - for(unsigned i = 0; i < test_piece.size(); ++i) { - if(test_piece[i] == tmp) { - if(passed) { - test_piece[i].num_passed++; - } else { - test_piece[i].num_failed++; - } - } - } - } + if(test_id != test_id_test) { + fprintf(stderr, "Error: the test from before has not been ended\n"); + exit(1); + } + + tested_pieces tmp; + tmp.piece_name = current_test; + + if(passed) { + tmp.num_passed = 1; + tmp.num_failed = 0; + } else { + tmp.num_passed = 0; + tmp.num_failed = 1; + } + + if(test_piece.empty() || !is_in_vec(tmp)) { + test_piece.push_back(tmp); + } else { + for(unsigned i = 0; i < test_piece.size(); ++i) { + if(test_piece[i] == tmp) { + if(passed) + test_piece[i].num_passed++; + else + test_piece[i].num_failed++; + } + } + } } void chess_tester::chess_end_test_suite() { - std::sort(test_piece.begin(), test_piece.end()); - - int pieces_passed = 0; - int pieces_partial = 0; - int pieces_fail = 0; - - fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----", - "------", "--------"); - fprintf(stderr, "| %10s | %5s | %6s | %8s |\n", "Piece ", "Total", "Passed", - "Passed %"); - fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----", - "------", "--------"); - for(unsigned i = 0; i < test_piece.size(); ++i) { - fprintf(stderr, "| %10s | %5d | %6d | %7.3f%% |\n", - test_piece[i].piece_name.c_str(), test_piece[i].num_passed + - test_piece[i].num_failed, test_piece[i].num_passed, - 100.0*(double)test_piece[i].num_passed / - (double)(test_piece[i].num_passed + test_piece[i].num_failed)); - - if(test_piece[i].num_passed == 0) { - pieces_fail++; - } else if(test_piece[i].num_failed == 0) { - pieces_passed++; - } else { - pieces_partial++; - } - } - fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----", - "------", "--------"); - - fprintf(stderr, "\nTotal pieces tested: %lu\n", test_piece.size()); - fprintf(stderr, "Passed: %d\n", pieces_passed); - fprintf(stderr, "Partially working: %d\n", pieces_partial); - fprintf(stderr, "Failed: %d\n", pieces_fail); + std::sort(test_piece.begin(), test_piece.end()); + + int pieces_passed = 0; + int pieces_partial = 0; + int pieces_fail = 0; + + fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----", + "------", "--------"); + fprintf(stderr, "| %10s | %5s | %6s | %8s |\n", "Piece ", "Total", "Passed", + "Passed %"); + fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----", + "------", "--------"); + for(unsigned i = 0; i < test_piece.size(); ++i) { + fprintf(stderr, "| %10s | %5d | %6d | %7.3f%% |\n", + test_piece[i].piece_name.c_str(), test_piece[i].num_passed + + test_piece[i].num_failed, test_piece[i].num_passed, + 100.0 * (double)test_piece[i].num_passed / + (double)(test_piece[i].num_passed + test_piece[i].num_failed)); + + if(test_piece[i].num_passed == 0) + pieces_fail++; + else if(test_piece[i].num_failed == 0) + pieces_passed++; + else + pieces_partial++; + } + fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----", + "------", "--------"); + + fprintf(stderr, "\nTotal pieces tested: %lu\n", test_piece.size()); + fprintf(stderr, "Passed: %d\n", pieces_passed); + fprintf(stderr, "Partially working: %d\n", pieces_partial); + fprintf(stderr, "Failed: %d\n", pieces_fail); } bool chess_tester::is_in_arr(const std::string& test_piece) const { - for(unsigned i = 0; i < CHESS_TEST_SIZE; ++i) { - if(chess_test_pieces[i] == test_piece) { - return true; - } - } + for(unsigned i = 0; i < CHESS_TEST_SIZE; ++i) + if(chess_test_pieces[i] == test_piece) + return true; - return false; + return false; } bool chess_tester::is_in_vec(const tested_pieces& piece) const { - for(unsigned i = 0; i < test_piece.size(); ++i) { - if(test_piece[i] == piece) { - return true; - } - } - return false; + for(unsigned i = 0; i < test_piece.size(); ++i) + if(test_piece[i] == piece) + return true; + return false; } -- cgit From 6d61c014039818d13a6908ae47caf377c8f948f3 Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Thu, 19 Jan 2017 13:42:28 +0000 Subject: Backing up files changed on laptop --- include/hjk.py | 0 src/chess_tester.cpp | 5 ++--- 2 files changed, 2 insertions(+), 3 deletions(-) create mode 100644 include/hjk.py diff --git a/include/hjk.py b/include/hjk.py new file mode 100644 index 0000000..e69de29 diff --git a/src/chess_tester.cpp b/src/chess_tester.cpp index 9e20fd3..a8ec0b5 100644 --- a/src/chess_tester.cpp +++ b/src/chess_tester.cpp @@ -10,10 +10,9 @@ chess_tester::chess_tester() : ts_begin(false) { } void chess_tester::chess_begin_test_suite() { - if(ts_begin) { + if(ts_begin) fprintf(stderr, "Error: already started test suite\n"); - exit(1); - } + exit(1); ts_begin = true; } -- cgit From 43910de3a8899b332e697ba210436be70e6a6f7c Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Sat, 21 Jan 2017 16:04:46 +0000 Subject: Change to lexer --- include/hjk.py | 0 src/board_state_parser.cpp | 5 +---- 2 files changed, 1 insertion(+), 4 deletions(-) delete mode 100644 include/hjk.py diff --git a/include/hjk.py b/include/hjk.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/board_state_parser.cpp b/src/board_state_parser.cpp index fac502f..5ff6bf3 100644 --- a/src/board_state_parser.cpp +++ b/src/board_state_parser.cpp @@ -28,7 +28,6 @@ BoardStateParser::~BoardStateParser() { void BoardStateParser::populateBoardState() { std::string currentLine; - unsigned index; boardStateVector.reserve(BOARD_SIZE); @@ -55,9 +54,7 @@ unsigned BoardStateParser::stringToInt(const std::string& str) { std::string BoardStateParser::getStateFromLine(const std::string& str) { std::string state; - for(unsigned i = 0; i < str.size(); ++i) - if(i > 1) - state += str[i]; + state = str.substr(2, str.length() - 2); return state; } -- cgit From 7ec074f67ae29a792e718fc48f7817b3a77b81ad Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Mon, 23 Jan 2017 15:18:05 +0000 Subject: Created a seperate file --- include/board_state_parser.hpp | 2 -- include/chess_board.hpp | 2 -- include/chess_constants.hpp | 28 ++++++++++++++++++++++++++++ include/chess_piece.hpp | 5 +++-- src/board_state_parser.cpp | 3 ++- src/chess_board.cpp | 8 +++++--- src/chess_piece.cpp | 2 +- src/pawn.cpp | 8 ++++++++ 8 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 include/chess_constants.hpp diff --git a/include/board_state_parser.hpp b/include/board_state_parser.hpp index d98ac4f..b054007 100644 --- a/include/board_state_parser.hpp +++ b/include/board_state_parser.hpp @@ -36,8 +36,6 @@ public: static std::string getStateFromLine(const std::string& str); protected: private: - static const std::size_t BOARD_SIZE = 64; - boardStateData boardStateVector; std::ifstream boardFile; }; diff --git a/include/chess_board.hpp b/include/chess_board.hpp index e5e4d7b..fdaf46e 100644 --- a/include/chess_board.hpp +++ b/include/chess_board.hpp @@ -34,8 +34,6 @@ public: bool printBoard(); protected: private: - static const std::size_t BOARD_SIZE = 64; - boardVector board; }; } diff --git a/include/chess_constants.hpp b/include/chess_constants.hpp new file mode 100644 index 0000000..cb50a37 --- /dev/null +++ b/include/chess_constants.hpp @@ -0,0 +1,28 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * date created: 23/01/17 + * + * ----------------------------------------------------------------------------- + * + * Chess Piece class with header for all the pieces as well that inherit + * from the Chess Piece class. + * + */ + +#ifndef YMH_CHESS_CONSTANTS +#define YMH_CHESS_CONSTANTS + +class ChessConstants { +public: + static const unsigned BOARD_SIZE = 64; + + static const unsigned WHITE_PAWN_ROW = 6; + static const unsigned BLACK_PAWN_ROW = 1; +protected: +private: +}; + +#endif diff --git a/include/chess_piece.hpp b/include/chess_piece.hpp index 5b2c1c2..1838af8 100644 --- a/include/chess_piece.hpp +++ b/include/chess_piece.hpp @@ -37,7 +37,8 @@ typedef unsigned int boardPosition; enum class Colour { White, - Black + Black, + None }; @@ -143,7 +144,7 @@ private: class EmptyPiece : public ChessPiece { public: EmptyPiece(); - EmptyPiece(const int& x, const int& y, const Colour& colour); + EmptyPiece(const int& x, const int& y); virtual void move(const int& x, const int& y); protected: diff --git a/src/board_state_parser.cpp b/src/board_state_parser.cpp index 5ff6bf3..288a491 100644 --- a/src/board_state_parser.cpp +++ b/src/board_state_parser.cpp @@ -12,6 +12,7 @@ */ #include "board_state_parser.hpp" +#include "chess_constats.hpp" using namespace ymhChessAI; @@ -29,7 +30,7 @@ BoardStateParser::~BoardStateParser() { void BoardStateParser::populateBoardState() { std::string currentLine; - boardStateVector.reserve(BOARD_SIZE); + boardStateVector.reserve(ChessConstants::BOARD_SIZE); if(!boardFile.is_open()) throw "Error: Can't open file"; diff --git a/src/chess_board.cpp b/src/chess_board.cpp index 051decd..e0967ad 100644 --- a/src/chess_board.cpp +++ b/src/chess_board.cpp @@ -12,18 +12,20 @@ */ #include "chess_board.hpp" +#include "chess_constants.hpp" + #include using namespace ymhChessAI; ChessBoard::ChessBoard() { - for(unsigned i = 0; i < BOARD_SIZE; ++i) + for(unsigned i = 0; i < ChessConstants::BOARD_SIZE; ++i) board.emplace_back(new EmptyPiece); } bool ChessBoard::populateBoard() { - for(auto&& piece : board) + for(auto && piece : board) piece = chessPiecePtr(new Knight); return true; } @@ -31,7 +33,7 @@ bool ChessBoard::populateBoard() { bool ChessBoard::printBoard() { int i = 0; - for(auto&& piece : board) { + for(auto && piece : board) { std::cout << i << " "; piece->move(1, 2); ++i; diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp index d4d14e6..33ce34f 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -25,7 +25,7 @@ ChessPiece::ChessPiece(const int& x, const int& y, const Colour& colour) : m_x(x EmptyPiece::EmptyPiece() : ChessPiece() { } -EmptyPiece::EmptyPiece(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { +EmptyPiece::EmptyPiece(const int& x, const int& y) : ChessPiece(x, y, Colour::None) { } void EmptyPiece::move(const int& x, const int& y) { diff --git a/src/pawn.cpp b/src/pawn.cpp index 1374886..458b980 100644 --- a/src/pawn.cpp +++ b/src/pawn.cpp @@ -12,6 +12,7 @@ */ #include "chess_piece.hpp" +#include "chess_constants.hpp" using namespace ymhChessAI; @@ -23,4 +24,11 @@ Pawn::Pawn(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, } void Pawn::move(const int& x, const int& y) { + if(m_colour == Colour::White) { + if(m_y == ChessConstants::WHITE_PAWN_ROW) { + } + } else { + if(m_y == ChessConstants::BLACK_PAWN_ROW) { + } + } } -- cgit From 4e79ea68d54305e6705a3a8e83a730508b9f1c1b Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Wed, 25 Jan 2017 13:22:29 +0000 Subject: Starting movement of the pawn --- src/pawn.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/pawn.cpp b/src/pawn.cpp index 458b980..5c5f1f6 100644 --- a/src/pawn.cpp +++ b/src/pawn.cpp @@ -7,13 +7,19 @@ * * ----------------------------------------------------------------------------- * - * Pawn class implementation + * Pawn class implementation, checking if there is a piece in the way of the + * one that is currently moving will be the chess boards concern + * + * Update: Checking if a piece is in the way will be the concern of this class + * because the move function has to perform the whole move. * */ #include "chess_piece.hpp" #include "chess_constants.hpp" +#include + using namespace ymhChessAI; @@ -24,11 +30,15 @@ Pawn::Pawn(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, } void Pawn::move(const int& x, const int& y) { + int move_length = y - (int)m_y; + + // First check what colour the pawn is if(m_colour == Colour::White) { - if(m_y == ChessConstants::WHITE_PAWN_ROW) { + // if this condition is met the pawn can move + if(move_length == -2 && m_y == ChessConstants::WHITE_PAWN_ROW) { + // we now have to check if there is a piece in between the pawn and + // it's desination } } else { - if(m_y == ChessConstants::BLACK_PAWN_ROW) { - } } } -- cgit