aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-02-25 12:59:47 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-02-25 12:59:47 +0000
commitb4336dd8f5bcd7370d9f439ea46829d7fce59e76 (patch)
treef51149312b636317aa72679879360dea26bab63d
parentb39845e0d36e5806b3f4629b1e5eeca4164572c0 (diff)
parent4e79ea68d54305e6705a3a8e83a730508b9f1c1b (diff)
downloadChessAI-master.tar.gz
ChessAI-master.zip
Fixed conflictsHEADmaster
-rw-r--r--include/board_state_parser.hpp44
-rw-r--r--include/chess_ai.hpp240
-rw-r--r--include/chess_board.hpp41
-rw-r--r--include/chess_constants.hpp28
-rw-r--r--include/chess_piece.hpp155
-rw-r--r--include/chess_tester.hpp105
-rw-r--r--res/default.board4
-rw-r--r--src/bishop.cpp26
-rw-r--r--src/board_state_parser.cpp61
-rw-r--r--src/chess_board.cpp263
-rw-r--r--src/chess_piece.cpp69
-rw-r--r--src/chess_tester.cpp186
-rw-r--r--src/king.cpp28
-rw-r--r--src/knight.cpp28
-rw-r--r--src/main.cpp.bck23
-rw-r--r--src/pawn.cpp44
-rw-r--r--src/queen.cpp26
-rw-r--r--src/rook.cpp26
-rw-r--r--src/test_bench.cpp44
19 files changed, 720 insertions, 721 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
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/bishop.cpp b/src/bishop.cpp
new file mode 100644
index 0000000..849db19
--- /dev/null
+++ b/src/bishop.cpp
@@ -0,0 +1,26 @@
+/*
+ *
+ * author: Yann Herklotz
+ * username: ymherklotz
+ * email: ymherklotz@gmail.com
+ * date created: 13/01/17
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Bishop class implementation
+ *
+ */
+
+#include "chess_piece.hpp"
+
+using namespace ymhChessAI;
+
+
+Bishop::Bishop() : ChessPiece() {
+}
+
+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/board_state_parser.cpp b/src/board_state_parser.cpp
new file mode 100644
index 0000000..288a491
--- /dev/null
+++ b/src/board_state_parser.cpp
@@ -0,0 +1,61 @@
+/*
+ *
+ * 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"
+#include "chess_constats.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;
+
+ boardStateVector.reserve(ChessConstants::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;
+
+ state = str.substr(2, str.length() - 2);
+
+ return state;
+}
diff --git a/src/chess_board.cpp b/src/chess_board.cpp
index de21015..e0967ad 100644
--- a/src/chess_board.cpp
+++ b/src/chess_board.cpp
@@ -1,244 +1,43 @@
-#include "chess_ai.hpp"
+/*
+ *
+ * author: Yann Herklotz
+ * username: ymherklotz
+ * email: ymherklotz@gmail.com
+ * date created: 14/01/17
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Chess Board class implementation
+ *
+ */
-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;
+#include "chess_board.hpp"
+#include "chess_constants.hpp"
- if(vec_index < 3) {
- colour = black;
- } else {
- colour = white;
- }
+#include <iostream>
- vec_index = it_vec - grid.begin();
- sqr_index = it_sqr - (*it_vec).begin();
+using namespace ymhChessAI;
- 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);
- }
+ChessBoard::ChessBoard() {
+ for(unsigned i = 0; i < ChessConstants::BOARD_SIZE; ++i)
+ board.emplace_back(new EmptyPiece);
}
-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;
+bool ChessBoard::populateBoard() {
+ for(auto && piece : board)
+ piece = chessPiecePtr(new Knight);
+ return true;
}
-chess_ai::move_error chess_ai::chess_board::move_pawn
-(square_iterator it, square_iterator new_it, chess_piece& taken_piece) {
+bool ChessBoard::printBoard() {
+ int i = 0;
- chess_piece piece(it->type, it->colour, new_it->x, new_it->y);
+ for(auto && piece : board) {
+ std::cout << i << " ";
+ piece->move(1, 2);
+ ++i;
+ }
- 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;
+ return true;
}
diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp
index f73d4c1..33ce34f 100644
--- a/src/chess_piece.cpp
+++ b/src/chess_piece.cpp
@@ -1,63 +1,32 @@
-#include "chess_ai.hpp"
-
-chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour) :
- type(type), colour(colour) {
-
- if(colour == black) {
- y = 0;
- } else {
- y = 7;
- }
-
- if(type == king) {
- x = 4;
- } else {
- x = 3;
- }
-}
+/*
+ *
+ * author: Yann Herklotz
+ * username: ymherklotz
+ * email: ymherklotz@gmail.com
+ * date created: 13/01/17
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Chess Piece class implementation
+ *
+ */
-chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour,
- unsigned x, unsigned y) :
- type(type), colour(colour), x(x), y(y) {}
+#include "chess_piece.hpp"
-chess_ai::chess_piece::~chess_piece() {}
+using namespace ymhChessAI;
-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;
+ChessPiece::ChessPiece() : m_x(0), m_y(0), m_colour(Colour::White) {
}
-void chess_ai::chess_piece::set_x(unsigned x) {
- this->x = x;
+ChessPiece::ChessPiece(const int& x, const int& y, const Colour& colour) : m_x(x), m_y(y), m_colour(colour) {
}
-void chess_ai::chess_piece::set_y(unsigned y) {
- this->y = y;
+EmptyPiece::EmptyPiece() : ChessPiece() {
}
-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);
+EmptyPiece::EmptyPiece(const int& x, const int& y) : ChessPiece(x, y, Colour::None) {
}
-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";
+void EmptyPiece::move(const int& x, const int& y) {
}
diff --git a/src/chess_tester.cpp b/src/chess_tester.cpp
index 6f3745c..a8ec0b5 100644
--- a/src/chess_tester.cpp
+++ b/src/chess_tester.cpp
@@ -1,125 +1,119 @@
#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;
}
diff --git a/src/king.cpp b/src/king.cpp
new file mode 100644
index 0000000..1d297d3
--- /dev/null
+++ b/src/king.cpp
@@ -0,0 +1,28 @@
+/*
+ *
+ * author: Yann Herklotz
+ * username: ymherklotz
+ * email: ymherklotz@gmail.com
+ * date created: 13/01/17
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * King class implementation
+ *
+ */
+
+#include "chess_piece.hpp"
+#include <iostream>
+
+using namespace ymhChessAI;
+
+
+King::King() : ChessPiece() {
+}
+
+King::King(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) {
+}
+
+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
new file mode 100644
index 0000000..25e5ab9
--- /dev/null
+++ b/src/knight.cpp
@@ -0,0 +1,28 @@
+/*
+ *
+ * author: Yann Herklotz
+ * username: ymherklotz
+ * email: ymherklotz@gmail.com
+ * date created: 13/01/17
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Knight class implementation
+ *
+ */
+
+#include "chess_piece.hpp"
+#include <iostream>
+
+using namespace ymhChessAI;
+
+
+Knight::Knight() : ChessPiece() {
+}
+
+Knight::Knight(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) {
+}
+
+void Knight::move(const int& x, const int& y) {
+ std::cout << "This is a Knight" << std::endl;
+}
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..5c5f1f6
--- /dev/null
+++ b/src/pawn.cpp
@@ -0,0 +1,44 @@
+/*
+ *
+ * author: Yann Herklotz
+ * username: ymherklotz
+ * email: ymherklotz@gmail.com
+ * date created: 13/01/17
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * 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 <cmath>
+
+using namespace ymhChessAI;
+
+
+Pawn::Pawn() : ChessPiece() {
+}
+
+Pawn::Pawn(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) {
+}
+
+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 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 {
+ }
+}
diff --git a/src/queen.cpp b/src/queen.cpp
new file mode 100644
index 0000000..897237a
--- /dev/null
+++ b/src/queen.cpp
@@ -0,0 +1,26 @@
+/*
+ *
+ * author: Yann Herklotz
+ * username: ymherklotz
+ * email: ymherklotz@gmail.com
+ * date created: 13/01/17
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Queen class implementation
+ *
+ */
+
+#include "chess_piece.hpp"
+
+using namespace ymhChessAI;
+
+
+Queen::Queen() : ChessPiece() {
+}
+
+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
new file mode 100644
index 0000000..cd72029
--- /dev/null
+++ b/src/rook.cpp
@@ -0,0 +1,26 @@
+/*
+ *
+ * author: Yann Herklotz
+ * username: ymherklotz
+ * email: ymherklotz@gmail.com
+ * date created: 13/01/17
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Rook class implementation
+ *
+ */
+
+#include "chess_piece.hpp"
+
+using namespace ymhChessAI;
+
+
+Rook::Rook() : ChessPiece() {
+}
+
+Rook::Rook(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) {
+}
+
+void Rook::move(const int& x, const int& y) {
+}
diff --git a/src/test_bench.cpp b/src/test_bench.cpp
index 8c79568..b1fe4ba 100644
--- a/src/test_bench.cpp
+++ b/src/test_bench.cpp
@@ -1,45 +1,35 @@
/*
*
- * description: This is the test_bench file for the chess_ai
+ * author: Yann Herklotz
+ * username: ymherklotz
+ * email: ymherklotz@gmail.com
+ * date created: 13/01/17
*
- * author: Yann Herklotz <ymherklotz@gmail.com>
- * 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 "chess_board.hpp"
+#include "board_state_parser.hpp"
#include <iostream>
using namespace std;
-using namespace chess_ai;
+using namespace ymhChessAI;
int main(int argc, char **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();
+ cout << "Program name: " << argv[0] << endl;
+ cout << "Arguments: " << argc - 1 << endl;
- 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);
+ ChessBoard cb;
+ cb.populateBoard();
+ cb.printBoard();
- test_bench.chess_end_test_suite();
+ cout << endl << "====== Executed Successfully ======" << endl;
return 0;
}