From 4428e477ae27e8634715610649b68919810dd684 Mon Sep 17 00:00:00 2001 From: zedarider Date: Sun, 6 Nov 2016 19:54:43 +0000 Subject: fixed makefile --- Makefile | 2 +- bin/chess_ai | Bin 234216 -> 235664 bytes include/chess_ai.hpp | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 3c0f9e7..1612948 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ TARGET := bin/chess_ai SRCEXT := cpp SOURCES := $(shell find $(SRCDIR) -type f -name "*.$(SRCEXT)") OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o)) -CFLAGS := -g -Wall -Wextra +CFLAGS := -g -Wall -Wextra -std=c++14 LIB := INC := -I include diff --git a/bin/chess_ai b/bin/chess_ai index a09b495..6950f5d 100755 Binary files a/bin/chess_ai and b/bin/chess_ai differ diff --git a/include/chess_ai.hpp b/include/chess_ai.hpp index c682bd2..c1496e3 100644 --- a/include/chess_ai.hpp +++ b/include/chess_ai.hpp @@ -141,7 +141,7 @@ namespace chess_ai { // overloading operators // so that we can make two copies of a point - chess_piece& operator=(const chess_piece& piece) { + chess_piece& operator==(const chess_piece& piece) { if(this != &piece) { this->set(piece.type, piece.colour, piece.x, piece.y); } -- cgit From f39a4463efa9deeaa6ec1f7986964f5baad784c2 Mon Sep 17 00:00:00 2001 From: zedarider Date: Sun, 6 Nov 2016 20:28:39 +0000 Subject: constructor is now default --- Makefile | 2 +- bin/chess_ai | Bin 235664 -> 235848 bytes include/chess_ai.hpp | 8 +++++++- src/chess_piece.cpp | 7 ------- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 1612948..a3a4bfd 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ TARGET := bin/chess_ai SRCEXT := cpp SOURCES := $(shell find $(SRCDIR) -type f -name "*.$(SRCEXT)") OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o)) -CFLAGS := -g -Wall -Wextra -std=c++14 +CFLAGS := -g -Wall -Wextra -Wpedantic -std=c++14 LIB := INC := -I include diff --git a/bin/chess_ai b/bin/chess_ai index 6950f5d..17216ca 100755 Binary files a/bin/chess_ai and b/bin/chess_ai differ diff --git a/include/chess_ai.hpp b/include/chess_ai.hpp index c1496e3..524ddc8 100644 --- a/include/chess_ai.hpp +++ b/include/chess_ai.hpp @@ -113,7 +113,7 @@ namespace chess_ai { public: // Initialises the chess piece to an empty square on the board - chess_piece(); + 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 @@ -153,6 +153,12 @@ namespace chess_ai { return *this; } + chess_piece operator++(int) { + chess_piece tmp(*this); + operator++(); + return tmp; + } + // return a printable version of the square std::string str(); diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp index cd28ef8..5e23444 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -1,12 +1,5 @@ #include "../include/chess_ai.hpp" -chess_ai::chess_piece::chess_piece() { - type = empty; - colour = none; - x = -1; - y = -1; -} - chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour) { this->type = type; this->colour = colour; -- cgit From c8bd95bd2905a512cbdab87cf25f3f250acf60ec Mon Sep 17 00:00:00 2001 From: zedarider Date: Tue, 8 Nov 2016 22:55:51 +0000 Subject: saving files as backup --- bin/chess_ai | Bin 235848 -> 236456 bytes include/chess_ai.hpp | 41 +++++++++++++++++++++++++---------------- src/chess_board.cpp | 28 ++++++++++++++++++++++------ src/chess_piece.cpp | 46 ++++++++++++++++++++++++++++++++++------------ 4 files changed, 81 insertions(+), 34 deletions(-) diff --git a/bin/chess_ai b/bin/chess_ai index 17216ca..e2a0805 100755 Binary files a/bin/chess_ai and b/bin/chess_ai differ diff --git a/include/chess_ai.hpp b/include/chess_ai.hpp index 524ddc8..8c6eb50 100644 --- a/include/chess_ai.hpp +++ b/include/chess_ai.hpp @@ -15,6 +15,10 @@ namespace chess_ai { class chess_board; class chess_piece; + typedef std::vector>:: +iterator vector_iterator; + typedef std::vector::iterator square_iterator; + // Describes the different types of chess pieces there are on the board enum piece_type : unsigned { // A pawn can only move forward twice on the first move, otherwise only @@ -92,7 +96,24 @@ namespace chess_ai { // remove piece at a specific location only void remove_piece(unsigned x, unsigned y); + + // move a piece according to the chess rules + void move_piece(chess_piece piece); + + // move a piece from coordinates + void move_piece(unsigned x, unsigned y); + + // find a piece on the board using coordinates + void find_piece(unsigned x, unsigned y); + // iterate through the list with point + void iterate_board(chess_piece piece, square_iterator& it); + + // iterate through the list and return the pointer to change + void iterate_board(unsigned x, unsigned y, square_iterator& it); + + + protected: void init_board_vector(); @@ -101,7 +122,7 @@ namespace chess_ai { // 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> grid; @@ -141,23 +162,11 @@ namespace chess_ai { // 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; - } + chess_piece& operator==(const chess_piece& piece); // overload ++ operator for pawns - chess_piece& operator++() { - return *this; - } - - chess_piece operator++(int) { - chess_piece tmp(*this); - operator++(); - return tmp; - } + chess_piece& operator++(); + chess_piece operator++(int); // return a printable version of the square std::string str(); diff --git a/src/chess_board.cpp b/src/chess_board.cpp index 9a6848b..badd585 100644 --- a/src/chess_board.cpp +++ b/src/chess_board.cpp @@ -1,14 +1,10 @@ #include "../include/chess_ai.hpp" -typedef std::vector>:: -iterator vector_iterator; -typedef std::vector::iterator square_iterator; - -chess_ai::chess_board::chess_board() { +chess_ai::chess_board::chess_board() : SIZE(CHESS_BOARD_SIZE) { init_board_vector(); } -chess_ai::chess_board::chess_board(board_state state) { +chess_ai::chess_board::chess_board(board_state state) : SIZE(CHESS_BOARD_SIZE){ unsigned vec_index, sqr_index; init_board_vector(); @@ -155,3 +151,23 @@ void chess_ai::chess_board::remove_piece(unsigned x, unsigned y) { } } } + +void chess_ai::chess_board::move_piece(chess_piece piece) { + +} + +void chess_ai::chess_board::iterate_board(unsigned x, unsigned y, + square_iterator& it) { + 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; + } + } + } +} diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp index 5e23444..bab3612 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -1,9 +1,7 @@ #include "../include/chess_ai.hpp" -chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour) { - this->type = type; - this->colour = colour; - +chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour) : + type(type), colour(colour) { if(colour == black) { y = 0; } else { @@ -17,14 +15,12 @@ chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour) { } } -chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour, - unsigned x, unsigned y) { - this->type = type; - this->colour = colour; - this->x = x; - this->y = y; - -} +chess_ai::chess_piece::chess_piece( + piece_type type, + piece_colour colour, + unsigned x, + unsigned y + ) : type(type), colour(colour), x(x), y(y) {} void chess_ai::chess_piece::set_type(piece_type type) { this->type = type; @@ -42,6 +38,32 @@ void chess_ai::chess_piece::set_y(unsigned y) { this->y = y; } +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); +} + +chess_ai::chess_piece& chess_ai::chess_piece::operator==(const chess_piece& + piece) { + if(this != &piece) { + this->set(piece.type, piece.colour, piece.x, piece.y); + } + return *this; +} + +chess_ai::chess_piece& chess_ai::chess_piece::operator++() { + return *this; +} + +chess_ai::chess_piece chess_ai::chess_piece::operator++(int) { + chess_piece tmp(*this); + operator++(); + return tmp; +} + std::string chess_ai::chess_piece::str() { if(type == empty) return " "; -- cgit From 451678aa0ac08581d14442967432e4383e1db3f3 Mon Sep 17 00:00:00 2001 From: zedarider Date: Tue, 8 Nov 2016 23:00:00 +0000 Subject: adding tmp files --- bin/chess_ai | Bin 236456 -> 237648 bytes src/chess_board.cpp | 16 +++------------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/bin/chess_ai b/bin/chess_ai index e2a0805..90f1c95 100755 Binary files a/bin/chess_ai and b/bin/chess_ai differ diff --git a/src/chess_board.cpp b/src/chess_board.cpp index badd585..c700ecb 100644 --- a/src/chess_board.cpp +++ b/src/chess_board.cpp @@ -121,19 +121,9 @@ void chess_ai::chess_board::set_piece(chess_piece piece) { } void chess_ai::chess_board::remove_piece(chess_piece piece) { - 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 == piece.y && sqr_index == piece.x) { - chess_piece empty_piece; - *it_sqr = empty_piece; - } - } - } + square_iterator it_sqr; + iterate_board(piece.x, piece.y, it_sqr); + *it_sqr = piece; } void chess_ai::chess_board::remove_piece(unsigned x, unsigned y) { -- cgit From c57aed1a546f0dc0a6efcb06d10125e2dbd34d60 Mon Sep 17 00:00:00 2001 From: zedarider Date: Tue, 8 Nov 2016 23:33:34 +0000 Subject: managed and going to merge --- bin/chess_ai | Bin 237648 -> 237744 bytes include/chess_ai.hpp | 11 ++++------ src/chess_board.cpp | 56 ++++++++++++++++++++++++--------------------------- src/chess_piece.cpp | 6 ++++++ 4 files changed, 36 insertions(+), 37 deletions(-) diff --git a/bin/chess_ai b/bin/chess_ai index 90f1c95..b938b9f 100755 Binary files a/bin/chess_ai and b/bin/chess_ai differ diff --git a/include/chess_ai.hpp b/include/chess_ai.hpp index 8c6eb50..efde215 100644 --- a/include/chess_ai.hpp +++ b/include/chess_ai.hpp @@ -102,15 +102,12 @@ iterator vector_iterator; // move a piece from coordinates void move_piece(unsigned x, unsigned y); + void move_piece(unsigned orig_x, unsigned orig_y, unsigned dest_x, + unsigned dest_y); + void move_piece(chess_piece piece, unsigned x, unsigned y); - // find a piece on the board using coordinates - void find_piece(unsigned x, unsigned y); - - // iterate through the list with point - void iterate_board(chess_piece piece, square_iterator& it); - // iterate through the list and return the pointer to change - void iterate_board(unsigned x, unsigned y, square_iterator& it); + square_iterator& iterate_board(square_iterator& it, unsigned x, unsigned y); diff --git a/src/chess_board.cpp b/src/chess_board.cpp index c700ecb..b46a284 100644 --- a/src/chess_board.cpp +++ b/src/chess_board.cpp @@ -106,48 +106,42 @@ void chess_ai::chess_board::print_board() { } void chess_ai::chess_board::set_piece(chess_piece piece) { - 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 == piece.y && sqr_index == piece.x) { - *it_sqr = piece; - } - } - } + square_iterator it_sqr; + *iterate_board(it_sqr, piece.x, piece.y) = piece; } void chess_ai::chess_board::remove_piece(chess_piece piece) { - square_iterator it_sqr; - iterate_board(piece.x, piece.y, it_sqr); - *it_sqr = piece; + remove_piece(piece.x, piece.y); } void chess_ai::chess_board::remove_piece(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) { - chess_piece empty_piece; - *it_sqr = empty_piece; - } - } - } + square_iterator it_sqr; + *iterate_board(it_sqr, x, y) = chess_piece(); } void chess_ai::chess_board::move_piece(chess_piece piece) { + move_piece(piece.x, piece.y); +} + +void chess_ai::chess_board::move_piece(unsigned x, unsigned y) { + square_iterator it; + iterate_board(it, x, y); + + ++(*it); +} + +void chess_ai::chess_board::move_piece(chess_ai::chess_piece piece, + unsigned x, unsigned y) { + move_piece(piece.x, piece.y, x, y); +} + +void chess_ai::chess_board::move_piece(unsigned orig_x, unsigned orig_y, + unsigned dest_x, unsigned dest_y) { } -void chess_ai::chess_board::iterate_board(unsigned x, unsigned y, - square_iterator& it) { +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(); @@ -157,7 +151,9 @@ void chess_ai::chess_board::iterate_board(unsigned x, unsigned y, if(vec_index == y && sqr_index == x) { it = it_sqr; + return it; } } } + return it; } diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp index bab3612..44dfaa5 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -55,6 +55,12 @@ chess_ai::chess_piece& chess_ai::chess_piece::operator==(const chess_piece& } chess_ai::chess_piece& chess_ai::chess_piece::operator++() { + if(type == pawn) { + if(colour == white) + --y; + else + ++y; + } return *this; } -- cgit From 418b9ff72f3010d7f3f07cb28a7e12808650d701 Mon Sep 17 00:00:00 2001 From: zedarider Date: Wed, 9 Nov 2016 01:43:52 +0000 Subject: added movement to pawn --- bin/chess_ai | Bin 237744 -> 242832 bytes include/chess_ai.hpp | 72 ++++++++++++++++++++++++++----------------- src/chess_board.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++------- src/chess_piece.cpp | 24 --------------- src/main.cpp | 8 +++++ 5 files changed, 126 insertions(+), 62 deletions(-) diff --git a/bin/chess_ai b/bin/chess_ai index b938b9f..7f1e4c8 100755 Binary files a/bin/chess_ai and b/bin/chess_ai differ diff --git a/include/chess_ai.hpp b/include/chess_ai.hpp index efde215..aa0a9c0 100644 --- a/include/chess_ai.hpp +++ b/include/chess_ai.hpp @@ -9,17 +9,29 @@ 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 movement_flag : 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; - // Describes the different types of chess pieces there are on the board 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, @@ -51,7 +63,6 @@ iterator vector_iterator; empty }; - // Describes the colour of the pieces enum piece_colour : unsigned { // Looking at the board white will be at the bottom white, @@ -61,7 +72,6 @@ iterator vector_iterator; 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 @@ -74,7 +84,24 @@ iterator vector_iterator; clear }; - // The chess board that will be played on + enum movement_flag : 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: @@ -98,24 +125,26 @@ iterator vector_iterator; void remove_piece(unsigned x, unsigned y); // move a piece according to the chess rules - void move_piece(chess_piece piece); - - // move a piece from coordinates - void move_piece(unsigned x, unsigned y); - void move_piece(unsigned orig_x, unsigned orig_y, unsigned dest_x, - unsigned dest_y); - void move_piece(chess_piece piece, unsigned x, unsigned y); + movement_flag move_piece(chess_piece piece); + movement_flag move_piece(unsigned x, unsigned y); + movement_flag move_piece(unsigned orig_x, unsigned orig_y, unsigned dest_x, + unsigned dest_y); + movement_flag move_piece(unsigned orig_x, unsigned orig_y, unsigned dest_x, + unsigned dest_y, chess_piece& taken_piece); + movement_flag move_piece(chess_piece piece, unsigned x, unsigned y); // iterate through the list and return the pointer to change square_iterator& iterate_board(square_iterator& it, unsigned x, unsigned y); - - - protected: - - void init_board_vector(); private: + + // initialises vector + void init_board_vector(); + + // moves the pawn + movement_flag 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. @@ -125,7 +154,6 @@ iterator vector_iterator; std::vector> grid; }; - // Any chess piece in the game class chess_piece { friend class chess_board; public: @@ -156,19 +184,9 @@ iterator vector_iterator; // 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); - - // overload ++ operator for pawns - chess_piece& operator++(); - chess_piece operator++(int); - // return a printable version of the square std::string str(); - protected: private: // Type of the chess piece, eg. bishop or queen diff --git a/src/chess_board.cpp b/src/chess_board.cpp index b46a284..ac3e1ae 100644 --- a/src/chess_board.cpp +++ b/src/chess_board.cpp @@ -119,29 +119,58 @@ void chess_ai::chess_board::remove_piece(unsigned x, unsigned y) { *iterate_board(it_sqr, x, y) = chess_piece(); } -void chess_ai::chess_board::move_piece(chess_piece piece) { - move_piece(piece.x, piece.y); +chess_ai::movement_flag chess_ai::chess_board::move_piece(chess_piece piece) { + return move_piece(piece.x, piece.y); } -void chess_ai::chess_board::move_piece(unsigned x, unsigned y) { +chess_ai::movement_flag chess_ai::chess_board::move_piece(unsigned x, + unsigned y) { square_iterator it; iterate_board(it, x, y); - - ++(*it); + + 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; } -void chess_ai::chess_board::move_piece(chess_ai::chess_piece piece, - unsigned x, unsigned y) { - move_piece(piece.x, piece.y, x, y); +chess_ai::movement_flag chess_ai::chess_board::move_piece( + chess_ai::chess_piece piece, unsigned x, unsigned y) { + return move_piece(piece.x, piece.y, x, y); } -void chess_ai::chess_board::move_piece(unsigned orig_x, unsigned orig_y, - unsigned dest_x, unsigned dest_y) { +chess_ai::movement_flag 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::movement_flag 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) { + 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(); @@ -157,3 +186,36 @@ chess_ai::square_iterator& chess_ai::chess_board::iterate_board( } return it; } + +chess_ai::movement_flag 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 44dfaa5..6eb07e7 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -46,30 +46,6 @@ void chess_ai::chess_piece::set(piece_type type, piece_colour colour, set_y(y); } -chess_ai::chess_piece& chess_ai::chess_piece::operator==(const chess_piece& - piece) { - if(this != &piece) { - this->set(piece.type, piece.colour, piece.x, piece.y); - } - return *this; -} - -chess_ai::chess_piece& chess_ai::chess_piece::operator++() { - if(type == pawn) { - if(colour == white) - --y; - else - ++y; - } - return *this; -} - -chess_ai::chess_piece chess_ai::chess_piece::operator++(int) { - chess_piece tmp(*this); - operator++(); - return tmp; -} - std::string chess_ai::chess_piece::str() { if(type == empty) return " "; diff --git a/src/main.cpp b/src/main.cpp index b41c919..d946343 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,6 +24,14 @@ int main(int argc, char** argv) { chess_piece piece(rook, white, 5, 3); board.set_piece(piece); + board.print_board(); + + board.remove_piece(5, 3); + + board.print_board(); + + board.move_piece(4, 6, 4, 5); + board.print_board(); return 0; -- cgit From 476e22dd7d91a8ad061e022f28f65507870ef72c Mon Sep 17 00:00:00 2001 From: zedarider Date: Wed, 9 Nov 2016 01:48:40 +0000 Subject: added movement to pawn --- bin/chess_ai | Bin 242832 -> 242832 bytes src/main.cpp | 4 +++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/chess_ai b/bin/chess_ai index 7f1e4c8..bb6ae57 100755 Binary files a/bin/chess_ai and b/bin/chess_ai differ diff --git a/src/main.cpp b/src/main.cpp index d946343..81485f3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,7 +30,9 @@ int main(int argc, char** argv) { board.print_board(); - board.move_piece(4, 6, 4, 5); + board.move_piece(4, 6, 4, 4); + board.move_piece(4, 1, 4, 3); + board.move_piece(3, 6, 3, 5); board.print_board(); -- cgit From 51af69807a888e6eea2ee105bf6a2a4993aba7e8 Mon Sep 17 00:00:00 2001 From: zedarider Date: Wed, 9 Nov 2016 15:15:54 +0000 Subject: changing branch --- include/chess_ai.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/chess_ai.hpp b/include/chess_ai.hpp index efde215..090b4ee 100644 --- a/include/chess_ai.hpp +++ b/include/chess_ai.hpp @@ -107,7 +107,8 @@ iterator vector_iterator; void move_piece(chess_piece piece, unsigned x, unsigned y); // iterate through the list and return the pointer to change - square_iterator& iterate_board(square_iterator& it, unsigned x, unsigned y); + square_iterator& iterate_board(square_iterator& it, unsigned x, + unsigned y); -- cgit From 375e4b989af8b4ac2c3479ea32f9324f29ce9071 Mon Sep 17 00:00:00 2001 From: zedarider Date: Wed, 9 Nov 2016 15:29:24 +0000 Subject: changed the code formatting for easier viewing --- include/chess_ai.hpp | 32 +++++++++++++++++++++----------- src/chess_board.cpp | 35 ++++++++++++++++++++--------------- src/chess_piece.cpp | 10 ++++------ 3 files changed, 45 insertions(+), 32 deletions(-) diff --git a/include/chess_ai.hpp b/include/chess_ai.hpp index a62ddf4..380d406 100644 --- a/include/chess_ai.hpp +++ b/include/chess_ai.hpp @@ -19,7 +19,7 @@ namespace chess_ai { enum board_state : unsigned; // defines the errors that can happen when moving a piece - enum movement_flag : unsigned; + enum move_error : unsigned; // The chess board that will be played on class chess_board; @@ -28,8 +28,10 @@ namespace chess_ai { class chess_piece; // typedefs for iterators to access elements easier + typedef std::vector>:: -iterator vector_iterator; + iterator vector_iterator; + typedef std::vector::iterator square_iterator; enum piece_type : unsigned { @@ -84,7 +86,7 @@ iterator vector_iterator; clear }; - enum movement_flag : unsigned { + 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 @@ -125,13 +127,21 @@ iterator vector_iterator; void remove_piece(unsigned x, unsigned y); // move a piece according to the chess rules - movement_flag move_piece(chess_piece piece); - movement_flag move_piece(unsigned x, unsigned y); - movement_flag move_piece(unsigned orig_x, unsigned orig_y, unsigned dest_x, - unsigned dest_y); - movement_flag move_piece(unsigned orig_x, unsigned orig_y, unsigned dest_x, - unsigned dest_y, chess_piece& taken_piece); - movement_flag move_piece(chess_piece piece, unsigned x, unsigned y); + move_error move_piece(chess_piece piece); + + // move piece using only x and y (for pawns) + move_error move_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); + + move_error move_piece(chess_piece piece, unsigned x, unsigned y); // iterate through the list and return the pointer to change square_iterator& iterate_board(square_iterator& it, unsigned x, @@ -144,7 +154,7 @@ iterator vector_iterator; void init_board_vector(); // moves the pawn - movement_flag move_pawn(square_iterator it, square_iterator new_it, + 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 diff --git a/src/chess_board.cpp b/src/chess_board.cpp index ac3e1ae..4891981 100644 --- a/src/chess_board.cpp +++ b/src/chess_board.cpp @@ -4,7 +4,7 @@ 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){ +chess_ai::chess_board::chess_board(board_state state) : SIZE(CHESS_BOARD_SIZE) { unsigned vec_index, sqr_index; init_board_vector(); @@ -119,12 +119,11 @@ void chess_ai::chess_board::remove_piece(unsigned x, unsigned y) { *iterate_board(it_sqr, x, y) = chess_piece(); } -chess_ai::movement_flag chess_ai::chess_board::move_piece(chess_piece piece) { +chess_ai::move_error chess_ai::chess_board::move_piece(chess_piece piece) { return move_piece(piece.x, piece.y); } -chess_ai::movement_flag chess_ai::chess_board::move_piece(unsigned x, - unsigned y) { +chess_ai::move_error chess_ai::chess_board::move_piece(unsigned x, unsigned y) { square_iterator it; iterate_board(it, x, y); @@ -136,19 +135,23 @@ chess_ai::movement_flag chess_ai::chess_board::move_piece(unsigned x, return move_error_IllegalMove; } -chess_ai::movement_flag chess_ai::chess_board::move_piece( - chess_ai::chess_piece piece, unsigned x, unsigned y) { +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::movement_flag chess_ai::chess_board::move_piece(unsigned orig_x, - unsigned orig_y, unsigned dest_x, unsigned dest_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::movement_flag chess_ai::chess_board::move_piece(unsigned orig_x, - unsigned orig_y, unsigned dest_x, - unsigned dest_y, chess_piece& taken_piece) { + +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; @@ -169,8 +172,9 @@ chess_ai::movement_flag chess_ai::chess_board::move_piece(unsigned orig_x, } } -chess_ai::square_iterator& chess_ai::chess_board::iterate_board( - square_iterator& it, unsigned x, unsigned y) { +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(); @@ -187,8 +191,9 @@ chess_ai::square_iterator& chess_ai::chess_board::iterate_board( return it; } -chess_ai::movement_flag chess_ai::chess_board::move_pawn(square_iterator it, - square_iterator new_it, chess_piece& taken_piece) { +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) || diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp index 6eb07e7..e81b9b7 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -2,6 +2,7 @@ chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour) : type(type), colour(colour) { + if(colour == black) { y = 0; } else { @@ -15,12 +16,9 @@ chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour) : } } -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(piece_type type, piece_colour colour, + unsigned x, unsigned y) : + type(type), colour(colour), x(x), y(y) {} void chess_ai::chess_piece::set_type(piece_type type) { this->type = type; -- cgit From 95f75aa7aacf5e0e1ef11e8d200f84de44bd891f Mon Sep 17 00:00:00 2001 From: zedarider Date: Thu, 10 Nov 2016 18:19:22 +0000 Subject: finished the tester --- bin/chess_ai | Bin 242832 -> 326304 bytes include/chess_ai.hpp | 13 +++-- include/chess_tester.hpp | 89 +++++++++++++++++++++++++++++++++ src/chess_board.cpp | 3 +- src/chess_piece.cpp | 2 + src/chess_tester.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 19 +------ 7 files changed, 229 insertions(+), 22 deletions(-) create mode 100644 include/chess_tester.hpp create mode 100644 src/chess_tester.cpp diff --git a/bin/chess_ai b/bin/chess_ai index bb6ae57..6114edc 100755 Binary files a/bin/chess_ai and b/bin/chess_ai differ diff --git a/include/chess_ai.hpp b/include/chess_ai.hpp index 380d406..fc05b2a 100644 --- a/include/chess_ai.hpp +++ b/include/chess_ai.hpp @@ -112,6 +112,9 @@ 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(); @@ -132,6 +135,9 @@ namespace chess_ai { // 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); @@ -140,8 +146,6 @@ namespace chess_ai { move_error move_piece(unsigned orig_x, unsigned orig_y, unsigned dest_x, unsigned dest_y, chess_piece& taken_piece); - - move_error move_piece(chess_piece piece, unsigned x, unsigned y); // iterate through the list and return the pointer to change square_iterator& iterate_board(square_iterator& it, unsigned x, @@ -153,7 +157,7 @@ namespace chess_ai { // initialises vector void init_board_vector(); - // moves the pawn + // 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); @@ -180,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); 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 +#include +#include +#include +#include +#include +#include + +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] = { + "", + "PAWN", + "ROOK", + "KNIGHT", + "BISHOP", + "QUEEN", + "KING" + }; + + // 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 test that has been started + int test_id_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 4891981..8b449ca 100644 --- a/src/chess_board.cpp +++ b/src/chess_board.cpp @@ -62,6 +62,8 @@ chess_ai::chess_board::chess_board(board_state state) : SIZE(CHESS_BOARD_SIZE) { } } +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; @@ -158,7 +160,6 @@ chess_ai::move_error chess_ai::chess_board::move_piece 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); diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp index e81b9b7..83b664f 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -20,6 +20,8 @@ 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; } diff --git a/src/chess_tester.cpp b/src/chess_tester.cpp new file mode 100644 index 0000000..60f6ee0 --- /dev/null +++ b/src/chess_tester.cpp @@ -0,0 +1,125 @@ +#include "../include/chess_tester.hpp" + +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); + } + + 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(!is_in_arr(test_name)) { + fprintf(stderr, "Error: the test name does not exist\n"); + exit(1); + } + + current_test = test_name; + + test_id = rand() % 0xffffffff; + test_id_test = 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++; + } + } + } + } +} + +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); +} + +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; + } + } + + 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; +} diff --git a/src/main.cpp b/src/main.cpp index 81485f3..def5cf0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ */ #include "../include/chess_ai.hpp" +#include "../include/chess_tester.hpp" #include @@ -17,24 +18,6 @@ using namespace chess_ai; int main(int argc, char** argv) { (void)argc; (void)argv; - - chess_board board(initial); - board.print_board(); - - chess_piece piece(rook, white, 5, 3); - board.set_piece(piece); - - board.print_board(); - - board.remove_piece(5, 3); - - board.print_board(); - - board.move_piece(4, 6, 4, 4); - board.move_piece(4, 1, 4, 3); - board.move_piece(3, 6, 3, 5); - - board.print_board(); return 0; } -- cgit