diff options
-rwxr-xr-x | bin/chess_ai | bin | 235848 -> 236456 bytes | |||
-rw-r--r-- | include/chess_ai.hpp | 41 | ||||
-rw-r--r-- | src/chess_board.cpp | 28 | ||||
-rw-r--r-- | src/chess_piece.cpp | 46 |
4 files changed, 81 insertions, 34 deletions
diff --git a/bin/chess_ai b/bin/chess_ai Binary files differindex 17216ca..e2a0805 100755 --- a/bin/chess_ai +++ b/bin/chess_ai 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<std::vector<chess_ai::chess_piece>>:: +iterator vector_iterator; + typedef std::vector<chess_ai::chess_piece>::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<std::vector<chess_piece>> 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<std::vector<chess_ai::chess_piece>>:: -iterator vector_iterator; -typedef std::vector<chess_ai::chess_piece>::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 " "; |