diff options
author | zedarider <ymherklotz@gmail.com> | 2017-01-13 23:19:00 +0000 |
---|---|---|
committer | zedarider <ymherklotz@gmail.com> | 2017-01-13 23:19:00 +0000 |
commit | 8320f7ec281891501a9b3092e20d990e7fd14700 (patch) | |
tree | f2ed38f470e74c491107743eac078f23e2e3aa9e | |
parent | eda873106d7f91214a4e4a32ee6b16889b248216 (diff) | |
download | ChessAI-8320f7ec281891501a9b3092e20d990e7fd14700.tar.gz ChessAI-8320f7ec281891501a9b3092e20d990e7fd14700.zip |
made it more object oriented but removed the board implementation
-rw-r--r-- | include/chess_ai.hpp | 240 | ||||
-rw-r--r-- | include/chess_piece.hpp | 104 | ||||
-rw-r--r-- | src/bishop.cpp | 25 | ||||
-rw-r--r-- | src/chess_board.cpp | 244 | ||||
-rw-r--r-- | src/chess_piece.cpp | 61 | ||||
-rw-r--r-- | src/king.cpp | 25 | ||||
-rw-r--r-- | src/knight.cpp | 25 | ||||
-rw-r--r-- | src/main.cpp.bck | 23 | ||||
-rw-r--r-- | src/pawn.cpp | 25 | ||||
-rw-r--r-- | src/queen.cpp | 25 | ||||
-rw-r--r-- | src/rook.cpp | 25 | ||||
-rw-r--r-- | src/test_bench.cpp | 45 |
12 files changed, 269 insertions, 598 deletions
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 <iostream> -#include <vector> -#include <string> - -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<std::vector<chess_ai::chess_piece> >:: - iterator vector_iterator; - - typedef std::vector<chess_ai::chess_piece>::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<std::vector<chess_piece> > 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<chess_ai::chess_piece> 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 <ymherklotz@gmail.com> - date created: DD-MM-YYYY - - */ - -#include "chess_ai.hpp" -#include "chess_tester.hpp" - -#include <iostream> - -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 <ymherklotz@gmail.com> - date created: DD-MM-YYYY - + * + * description: This is the test_bench file for the chess_ai + * + * author: Yann Herklotz <ymherklotz@gmail.com> + * 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; } |