Go to the documentation of this file.
50 #ifndef CHESS_H // header guard to prevent multiple includes of the classes (results in compilation error)
152 Chess & operator =(
const Chess &chess_object);
170 void setBoard(
const vector<Piece*> &board) {this->board = board;}
185 void setCheckPieces(
const vector<Piece*> &check_pieces) {this->check_pieces = check_pieces;}
287 void setReservoir(
const vector<pair<int, char>> & reservoir) {this->reservoir = reservoir;}
300 bool useReservoirPiece(
int src,
int dest);
333 bool makeMove(
int src,
int dest, istream &in);
347 void isCheckmate(
string check_type);
418 void makeMoveForType(
int src,
int dest);
432 void pieceSwap(
int src,
int dest, vector<Piece*> &board);
442 void handleChangeTurn();
454 void handleCheckmate();
464 void handleStalemate();
481 bool singleCheckPieceIterator(
Piece *piece,
Piece *king);
497 bool doubleCheckPieceIterator(
Piece *king);
546 this->square = square;
739 bool isSameColor(
int dest,
const Chess &chess);
752 bool isPinned(
int dest,
const Chess &chess);
764 bool isPathFree(
int dest,
const Chess &chess);
777 bool isLegalMove(
int dest,
Chess &chess);
788 bool causeCheck(
int dest,
Chess &chess);
800 bool causeDoubleCheck(
int dest,
Chess &chess);
903 Pawn() :
Piece(), en_passant_left{
false}, en_passant_right{
false} {}
919 :
Piece(square, type, color), en_passant_left{
false}, en_passant_right{
false} {}
924 void setEnPassantLeft(
bool en_passant_left)
override {this->en_passant_left = en_passant_left;}
928 void setEnPassantRight(
bool en_passant_right)
override {this->en_passant_right = en_passant_right;}
944 bool isPossibleMove(
int dest,
const Chess &chess)
override;
954 void enPassantHandling(
int src,
Chess &chess)
override;
965 void promotePawn(
Chess &chess, istream &in)
override;
1022 bool isPossibleMove(
int dest,
const Chess &chess)
override;
1065 bool isPossibleMove(
int dest,
const Chess &chess)
override;
1113 bool isPossibleMove(
int dest,
const Chess &chess)
override;
1161 bool isPossibleMove(
int dest,
const Chess &chess)
override;
1210 bool isPossibleMove(
int dest,
const Chess &chess)
override;
1222 bool canCastle(
int dest,
const Chess &chess)
override;
1235 bool movedIntoCheck(
int dest,
Chess &chess)
override;
1299 if(std::isalpha(input[0]) && input.length() > 1)
1301 int ascii_val = std::islower(input[0]) ? 97 : 65;
1302 return (
int(input[0]) - ascii_val) + (8 - (int(input[1]) - 48))*8;
1306 else if(std::isalpha(input[0]))
1307 return (
unsigned char) std::tolower(input[0]);
1309 else {
return std::stoi(input); }
1324 void printBoard(
const vector<Piece*> &board,
const vector<pair<int, char>> &reservoir);
vector< bool > flags
Definition: chess.h:390
void setPieceMoveInfo(bool moved)
(Mutator) Sets the piece move information.
Definition: chess.h:606
bool isKing()
Determines if the piece is a king.
Definition: chess.h:712
Empty()
Default constructor with default board parameter initialization - Constructs a new instance....
Definition: chess.h:1257
@ KNIGHT
1
Definition: chess.h:89
virtual void promotePawn(Chess &chess, istream &in)
Promotes the pawn if needed.
Definition: chess.h:841
~Empty()
Definition: chess.h:1248
@ KING
5
Definition: chess.h:93
vector< Piece * > getCheckPieces() const
(Accessor) Gets the check stack information.
Definition: chess.h:177
@ BLACK
0
Definition: chess.h:104
void drawOrResign(bool clear_screen, Chess &chess, istream &in)
At any moment, the players can either continue, draw, or resign.
Definition: chess.cpp:1732
bool getCheckmate() const
(Accessor) Gets the checkmate information.
Definition: chess.h:220
This class describes a bishop and provides functions related to bishop movements.
Definition: chess.h:1033
void setStalemate(bool stalemate)
(Mutator) Sets the stalemate information.
Definition: chess.h:241
~Rook()
Definition: chess.h:1077
pieceColor color
Definition: chess.h:880
void saveObject(const Chess &chess_object)
Saves an object by serializing its member fields to a text file, allowing it to later be reset.
Definition: chess.cpp:1843
virtual ~Piece()
Definition: chess.h:524
void printBoard(const vector< Piece * > &board, const vector< pair< int, char >> &reservoir)
Iterates through the pieces on a current board representation to produce the board on the console scr...
Definition: chess.cpp:1619
Knight(int square, pieceType type, pieceColor color)
Constructs a new instance with valid piece information initialization. Calls Piece class constructor ...
Definition: chess.h:1007
~Queen()
Definition: chess.h:1125
void setDoubleCheck(bool double_check)
(Mutator) Sets the double check information.
Definition: chess.h:213
This class describes the chess board on which the game takes place. It contains functions which analy...
Definition: chess.h:121
int num_moves
Definition: chess.h:399
virtual bool movedIntoCheck(int dest, Chess &chess)
Did the king move into check?
Definition: chess.h:863
void setTurn(pieceColor turn)
(Mutator) Sets the player's turn information.
Definition: chess.h:255
void restoreObject(Chess &chess_object)
De-serializes an object from a file based on the number of moves made, essentially restoring the obje...
Definition: chess.cpp:1858
@ NEUTRAL
1
Definition: chess.h:105
int getNumMoves() const
(Accessor) Gets the number of moves made.
Definition: chess.h:262
~King()
Definition: chess.h:1174
bool isBishop()
Determines if the piece is a bishop.
Definition: chess.h:682
virtual bool getEnPassantRight() const
(Accessor) Gets the piece en-passant ability information for right side.
Definition: chess.h:632
Rook()
Default constructor with default board parameter initialization - Constructs a new instance....
Definition: chess.h:1086
bool en_passant_left
Definition: chess.h:969
King(int square, pieceType type, pieceColor color)
Constructs a new instance with valid piece information initialization. Calls Piece class constructor ...
Definition: chess.h:1196
virtual bool getEnPassantLeft() const
(Accessor) Gets the piece en-passant ability information for left side.
Definition: chess.h:613
Knight()
Default constructor with default board parameter initialization - Constructs a new instance....
Definition: chess.h:994
bool isRook()
Determines if the piece is a rook.
Definition: chess.h:692
bool getCheck() const
(Accessor) Gets the check information.
Definition: chess.h:192
void setBoard(const vector< Piece * > &board)
(Mutator) Updates the board representation at the top of the board positions stack.
Definition: chess.h:170
void printMessage(string text, int color)
Prints the given message ('text') with a given 'color' to console.
Definition: chess.cpp:1830
virtual bool isPossibleMove(int dest, const Chess &chess)
Determine if the piece has a possible move towards the destination square.
Definition: chess.h:813
virtual void setEnPassantLeft(bool en_passant_left)
(Mutator) Sets the piece en-passant ability information for left side.
Definition: chess.h:624
pieceColor getTurn() const
(Accessor) Gets the player's turn information.
Definition: chess.h:248
bool isKnight()
Determines if the piece is a knight.
Definition: chess.h:672
void setPieceSquare(int square)
(Mutator) Sets the piece square information.
Definition: chess.h:564
bool getPieceMoveInfo() const
(Accessor) Gets the piece move information useful for pawns, rooks, kings.
Definition: chess.h:599
void setPieceType(pieceType type)
(Mutator) Sets the piece type information.
Definition: chess.h:578
~Pawn()
Definition: chess.h:893
void setEnPassantLeft(bool en_passant_left) override
(Mutator) Sets the piece en-passant ability information for left side.
Definition: chess.h:924
bool moved
Definition: chess.h:870
void clearScreen(bool apply)
Clears the screen of the console window using a special string instead of a platform specific command...
Definition: chess.cpp:1815
~Bishop()
Definition: chess.h:1035
ostream & operator<<(ostream &out, const Chess &chess_object)
Overloaded extraction operator.
Definition: chess.cpp:47
vector< pair< int, char > > getReservoir() const
Gets the current reservoir information (pieces and quantity).
Definition: chess.h:279
This class describes a king and provides functions related to king movements.
Definition: chess.h:1172
This class describes a pawn and provides functions related to pawn movements.
Definition: chess.h:891
Empty(int square, pieceType type, pieceColor color)
Constructs a new instance with valid piece information initialization. Calls Piece class constructor ...
Definition: chess.h:1270
void setCheck(bool check)
(Mutator) Sets the check information.
Definition: chess.h:199
void setPieceColor(pieceColor color)
(Mutator) Sets the piece color information.
Definition: chess.h:592
bool getDoubleCheck() const
(Accessor) Gets the double check information.
Definition: chess.h:206
pieceType
Piece's Type.
Definition: chess.h:87
int preProcessInput(T input)
Converts the input string into its corresponding coordinate (integer) for easier computation in chess...
Definition: chess.h:1297
bool getEnPassantRight() const override
(Accessor) Gets the piece en-passant ability information for right side.
Definition: chess.h:927
void setCheckmate(bool checkmate)
(Mutator) Sets the checkmate information.
Definition: chess.h:227
Pawn()
Default constructor with default board parameter initialization - Constructs a new instance....
Definition: chess.h:903
pieceColor getPieceColor() const
(Accessor) Gets the piece color information.
Definition: chess.h:585
bool getStalemate() const
(Accessor) Gets the stalemate information.
Definition: chess.h:234
virtual void setEnPassantRight(bool en_passant_right)
(Mutator) Sets the piece en-passant ability information for right side.
Definition: chess.h:643
@ BISHOP
2
Definition: chess.h:90
@ PAWN
0
Definition: chess.h:88
bool isPieceBlack()
Determines if the piece is black.
Definition: chess.h:728
This class describes a knight and provides functions related to knight movements.
Definition: chess.h:983
vector< pair< int, char > > reservoir
Definition: chess.h:393
pieceColor
Piece's Color.
Definition: chess.h:103
This class describes an empty square which is necessary to keep in mind when playing a game of chess.
Definition: chess.h:1246
int square
Definition: chess.h:867
Queen()
Default constructor with default board parameter initialization - Constructs a new instance....
Definition: chess.h:1134
istream & operator>>(istream &in, Chess &chess_object)
Overloaded insertion operator.
Definition: chess.cpp:71
pieceType type
Definition: chess.h:875
Rook(int square, pieceType type, pieceColor color)
Constructs a new instance with valid piece information initialization. Calls Piece class constructor ...
Definition: chess.h:1099
Queen(int square, pieceType type, pieceColor color)
Constructs a new instance with valid piece information initialization. Calls Piece class constructor ...
Definition: chess.h:1147
King()
Default constructor with default board parameter initialization - Constructs a new instance....
Definition: chess.h:1183
bool isQueen()
Determines if the piece is a queen.
Definition: chess.h:702
@ WHITE
2
Definition: chess.h:106
void setCheckPieces(const vector< Piece * > &check_pieces)
(Mutator) Sets the check stack information.
Definition: chess.h:185
Bishop(int square, pieceType type, pieceColor color)
Constructs a new instance with valid piece information initialization. Calls Piece class constructor ...
Definition: chess.h:1057
vector< Piece * > board
Definition: chess.h:384
bool isEmpty()
Determines if the square is empty.
Definition: chess.h:652
bool getEnPassantLeft() const override
(Accessor) Gets the piece en-passant ability information for left side.
Definition: chess.h:923
Bishop()
Default constructor with default board parameter initialization - Constructs a new instance....
Definition: chess.h:1044
Piece(int square, pieceType type, pieceColor color)
Constructs a new instance with valid piece information initialization.
Definition: chess.h:543
This class describes a rook and provides functions related to rook movements.
Definition: chess.h:1075
pieceType getPieceType() const
(Accessor) Gets the piece type information.
Definition: chess.h:571
Piece()
Default constructor with default board parameter initialization - Constructs a new instance.
Definition: chess.h:531
virtual void enPassantHandling(int src, Chess &chess)
Pawn attacks opposing pawn with en-passant
Definition: chess.h:828
Pawn(int square, pieceType type, pieceColor color)
Constructs a new instance with valid piece information initialization. Calls Piece class constructor ...
Definition: chess.h:918
@ QUEEN
4
Definition: chess.h:92
@ ROOK
3
Definition: chess.h:91
@ EMPTY
6
Definition: chess.h:94
void setReservoir(const vector< pair< int, char >> &reservoir)
Sets the piece reservoir after a piece on the current board representation is replaced.
Definition: chess.h:287
int getPieceSquare() const
(Accessor) Gets the piece square information.
Definition: chess.h:557
This class describes a queen and provides functions related to queen movements.
Definition: chess.h:1123
~Knight()
Definition: chess.h:985
bool isPawn()
Determines if the piece is a pawn.
Definition: chess.h:662
pieceColor turn
Definition: chess.h:396
void printFooterMessage(string input_message, const Chess &chess)
Prints the footer message before each move indicating whose move it is for the current board represen...
Definition: chess.cpp:1712
bool isPieceWhite()
Determines if the piece is white.
Definition: chess.h:721
void setEnPassantRight(bool en_passant_right) override
(Mutator) Sets the piece en-passant ability information for right side.
Definition: chess.h:928
vector< Piece * > getBoard() const
(Accessor) Gets the board representation at the top of the board positions stack.
Definition: chess.h:162
bool en_passant_right
Definition: chess.h:972
This namespace contains the global functions related to chessCAMO which are mainly used as helper fun...
Definition: chess.h:1282
void setNumMoves(int num_moves)
(Mutator) Sets the number of moves made on the board.
Definition: chess.h:269
This class describes the pieces on the board at any given moment. It contains functions for determini...
Definition: chess.h:522
vector< Piece * > check_pieces
Definition: chess.h:387
virtual bool canCastle(int dest, const Chess &chess)
Can the king castle?
Definition: chess.h:852