aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorzedarider <ymherklotz@gmail.com>2016-11-24 17:56:43 +0000
committerzedarider <ymherklotz@gmail.com>2016-11-24 17:56:43 +0000
commit44bcbd77f83baf64dc57ae8f816492194da7aa87 (patch)
treecdcb44db021536b203fa3951f6afe2eef33a7ad3 /include
parentbea3f2ee3a313049c3513b3eedf5cb6519f19729 (diff)
parent95f75aa7aacf5e0e1ef11e8d200f84de44bd891f (diff)
downloadChessAI-44bcbd77f83baf64dc57ae8f816492194da7aa87.tar.gz
ChessAI-44bcbd77f83baf64dc57ae8f816492194da7aa87.zip
final changes from mac
Diffstat (limited to 'include')
-rw-r--r--include/chess_ai.hpp123
-rw-r--r--include/chess_tester.hpp89
2 files changed, 171 insertions, 41 deletions
diff --git a/include/chess_ai.hpp b/include/chess_ai.hpp
index 2984d49..3a0d95b 100644
--- a/include/chess_ai.hpp
+++ b/include/chess_ai.hpp
@@ -9,13 +9,31 @@
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;
- // Describes the different types of chess pieces there are on the board
+ // 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,
@@ -47,7 +65,6 @@ namespace chess_ai {
empty
};
- // Describes the colour of the pieces
enum piece_colour : unsigned {
// Looking at the board white will be at the bottom
white,
@@ -57,7 +74,6 @@ namespace chess_ai {
none
};
- // just describes if the board is full or empty
enum board_state : unsigned {
// The starting position of the board with all pieces in the right
// position
@@ -70,7 +86,24 @@ namespace chess_ai {
clear
};
- // The chess board that will be played on
+ 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:
@@ -79,7 +112,10 @@ namespace chess_ai {
// 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();
@@ -92,28 +128,53 @@ namespace chess_ai {
// remove piece at a specific location only
void remove_piece(unsigned x, unsigned y);
-
- protected:
- void init_board_vector();
-
+ // 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.
- unsigned const SIZE = CHESS_BOARD_SIZE;
+ const unsigned SIZE;
// The actual board where the values of the pieces will be changed.
std::vector<std::vector<chess_piece>> grid;
};
- // Any chess piece in the game
class chess_piece {
friend class chess_board;
public:
-
- // Initialises the chess piece to an empty square on the board
- chess_piece();
+
+ // 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
@@ -123,6 +184,9 @@ namespace chess_ai {
chess_piece(piece_type type, piece_colour colour, unsigned x,
unsigned y);
+ // destructor to clean up the variables
+ ~chess_piece();
+
// Set the type of the chess_piece
void set_type(piece_type type);
@@ -138,32 +202,9 @@ namespace chess_ai {
// set the different values
void set(piece_type type, piece_colour colour, unsigned x, unsigned y);
- // overloading operators
-
- // so that we can make two copies of a point
- chess_piece& operator=(const chess_piece& piece) {
- if(this != &piece) {
- this->set(piece.type, piece.colour, piece.x, piece.y);
- }
- return *this;
- }
-
- // overload ++ operator for pawns
- chess_piece& operator++() {
- if(this->type == pawn) {
- if(this->colour == white) {
- --pawn->y;
- } else {
- ++pawn->y;
- }
- }
- return *this;
- }
-
// return a printable version of the square
std::string str();
-
- protected:
+
private:
// Type of the chess piece, eg. bishop or queen
@@ -171,10 +212,10 @@ namespace chess_ai {
// Colour of the chess piece
piece_colour colour;
-
+
// x location of the chess piece
unsigned x;
-
+
// y location of the chess piece
unsigned y;
};
diff --git a/include/chess_tester.hpp b/include/chess_tester.hpp
new file mode 100644
index 0000000..c0c2f1c
--- /dev/null
+++ b/include/chess_tester.hpp
@@ -0,0 +1,89 @@
+#ifndef CHESS_TESTER_HPP
+#define CHESS_TESTER_HPP
+
+#define CHESS_TEST_SIZE 7
+
+#include <string>
+#include <vector>
+#include <cstdio>
+#include <cstdlib>
+#include <ctime>
+#include <algorithm>
+#include <iostream>
+
+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;
+ }
+};
+
+class chess_tester {
+
+public:
+
+ // 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
+ int chess_begin_test(std::string test_name);
+
+ // 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();
+
+ // 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;
+
+private:
+
+ // 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"
+ };
+
+ // 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 test that has been started
+ int test_id_test;
+
+ // the current test that is being tested
+ std::string current_test;
+};
+
+#endif