aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzedarider <ymherklotz@gmail.com>2016-11-09 01:43:52 +0000
committerzedarider <ymherklotz@gmail.com>2016-11-09 01:43:52 +0000
commit418b9ff72f3010d7f3f07cb28a7e12808650d701 (patch)
treeca522d89cfd1c6f18ebda1c9a8ed41bcf361d927
parentc57aed1a546f0dc0a6efcb06d10125e2dbd34d60 (diff)
downloadChessAI-418b9ff72f3010d7f3f07cb28a7e12808650d701.tar.gz
ChessAI-418b9ff72f3010d7f3f07cb28a7e12808650d701.zip
added movement to pawn
-rwxr-xr-xbin/chess_aibin237744 -> 242832 bytes
-rw-r--r--include/chess_ai.hpp72
-rw-r--r--src/chess_board.cpp84
-rw-r--r--src/chess_piece.cpp24
-rw-r--r--src/main.cpp8
5 files changed, 126 insertions, 62 deletions
diff --git a/bin/chess_ai b/bin/chess_ai
index b938b9f..7f1e4c8 100755
--- a/bin/chess_ai
+++ b/bin/chess_ai
Binary files 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<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
// 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<std::vector<chess_piece>> 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
@@ -25,6 +25,14 @@ int main(int argc, char** argv) {
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;
}