diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-25 12:59:47 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-25 12:59:47 +0000 |
commit | b4336dd8f5bcd7370d9f439ea46829d7fce59e76 (patch) | |
tree | f51149312b636317aa72679879360dea26bab63d /include | |
parent | b39845e0d36e5806b3f4629b1e5eeca4164572c0 (diff) | |
parent | 4e79ea68d54305e6705a3a8e83a730508b9f1c1b (diff) | |
download | ChessAI-b4336dd8f5bcd7370d9f439ea46829d7fce59e76.tar.gz ChessAI-b4336dd8f5bcd7370d9f439ea46829d7fce59e76.zip |
Diffstat (limited to 'include')
-rw-r--r-- | include/board_state_parser.hpp | 44 | ||||
-rw-r--r-- | include/chess_ai.hpp | 240 | ||||
-rw-r--r-- | include/chess_board.hpp | 41 | ||||
-rw-r--r-- | include/chess_constants.hpp | 28 | ||||
-rw-r--r-- | include/chess_piece.hpp | 155 | ||||
-rw-r--r-- | include/chess_tester.hpp | 105 |
6 files changed, 320 insertions, 293 deletions
diff --git a/include/board_state_parser.hpp b/include/board_state_parser.hpp new file mode 100644 index 0000000..b054007 --- /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 <string> +#include <vector> +#include <fstream> + +namespace ymhChessAI { +class BoardStateParser; + +typedef std::vector<std::string> 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: + boardStateData boardStateVector; + std::ifstream boardFile; +}; +} + +#endif 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_board.hpp b/include/chess_board.hpp new file mode 100644 index 0000000..fdaf46e --- /dev/null +++ b/include/chess_board.hpp @@ -0,0 +1,41 @@ +/* + * + * 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 <vector> +#include <memory> + +namespace ymhChessAI { +class ChessBoard; + +typedef std::vector<std::unique_ptr<ChessPiece> >::iterator boardIterator; +typedef std::vector<std::unique_ptr<ChessPiece> > boardVector; +typedef std::unique_ptr<ChessPiece> chessPiecePtr; + +class ChessBoard { +public: + ChessBoard(); + + bool populateBoard(); + bool printBoard(); +protected: +private: + boardVector board; +}; +} + +#endif 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 new file mode 100644 index 0000000..1838af8 --- /dev/null +++ b/include/chess_piece.hpp @@ -0,0 +1,155 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * date created: 13/01/17 + * + * ----------------------------------------------------------------------------- + * + * 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 + + +// defining seperate namespace so that I don't have to worry with duplicate +// names + +namespace ymhChessAI { +enum class Colour; +class ChessPiece; +class King; +class Queen; +class Rook; +class Bishop; +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 + +enum class Colour { + White, + Black, + None +}; + + +// 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(); + // initialise piece at a specific location on the board to save code + ChessPiece(const int& x, const int& y, const Colour& colour); + + // 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 + boardPosition m_x, m_y; + + // defines what colour the piece is + Colour m_colour; +}; + + +// King class + +class King : public ChessPiece { +public: + King(); + King(const int& x, const int& y, const Colour& colour); + + virtual void move(const int& x, const int& y); +protected: +private: +}; + + +// Queen class + +class Queen : public ChessPiece { +public: + Queen(); + Queen(const int& x, const int& y, const Colour& colour); + + virtual void move(const int& x, const int& y); +protected: +private: +}; + + +// Rook class + +class Rook : public ChessPiece { +public: + Rook(); + Rook(const int& x, const int& y, const Colour& colour); + + virtual void move(const int& x, const int& y); +protected: +private: +}; + + +// Bishop class + +class Bishop : public ChessPiece { +public: + Bishop(); + Bishop(const int& x, const int& y, const Colour& colour); + + virtual void move(const int& x, const int& y); +protected: +private: +}; + + +// Knight class + +class Knight : public ChessPiece { +public: + Knight(); + Knight(const int& x, const int& y, const Colour& colour); + + virtual void move(const int& x, const int& y); +protected: +private: +}; + + +// Pawn class + +class Pawn : public ChessPiece { +public: + Pawn(); + Pawn(const int& x, const int& y, const Colour& colour); + + virtual void move(const int& x, const int& y); +protected: +private: +}; + +class EmptyPiece : public ChessPiece { +public: + EmptyPiece(); + EmptyPiece(const int& x, const int& y); + + 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 <algorithm> #include <iostream> +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] = { - "<INTERNAL>", - "PAWN", - "ROOK", - "KNIGHT", - "BISHOP", - "QUEEN", - "KING" - }; + // list of pieces to test + const std::string chess_test_pieces[CHESS_TEST_SIZE] = { + "<INTERNAL>", + "PAWN", + "ROOK", + "KNIGHT", + "BISHOP", + "QUEEN", + "KING" + }; - // vector that contains the tested pieces - std::vector<tested_pieces> test_piece; + // vector that contains the tested pieces + std::vector<tested_pieces> 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 |