chessCAMO
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
Chess Class Reference

This class describes the chess board on which the game takes place. It contains functions which analyze specific flags to determinewhen to switch turns, display warnings, and even end the game. More...

#include <chess.h>

Public Member Functions

 Chess ()
 Default constructor with default board parameter initialization - Constructs a new instance. More...
 
 ~Chess ()
 Destroys the object and frees any dynamically allocated memory ('new') to avoid memory leaks. More...
 
 Chess (const Chess &chess_object)
 Copy constructor - Constructs a new instance and copies the calling object's values to it. More...
 
Chessoperator= (const Chess &chess_object)
 Copy Assignment operator - assigns values of one object to another existing object. More...
 
vector< Piece * > getBoard () const
 (Accessor) Gets the board representation at the top of the board positions stack. More...
 
void setBoard (const vector< Piece * > &board)
 (Mutator) Updates the board representation at the top of the board positions stack. More...
 
vector< Piece * > getCheckPieces () const
 (Accessor) Gets the check stack information. More...
 
void setCheckPieces (const vector< Piece * > &check_pieces)
 (Mutator) Sets the check stack information. More...
 
bool getCheck () const
 (Accessor) Gets the check information. More...
 
void setCheck (bool check)
 (Mutator) Sets the check information. More...
 
bool getDoubleCheck () const
 (Accessor) Gets the double check information. More...
 
void setDoubleCheck (bool double_check)
 (Mutator) Sets the double check information. More...
 
bool getCheckmate () const
 (Accessor) Gets the checkmate information. More...
 
void setCheckmate (bool checkmate)
 (Mutator) Sets the checkmate information. More...
 
bool getStalemate () const
 (Accessor) Gets the stalemate information. More...
 
void setStalemate (bool stalemate)
 (Mutator) Sets the stalemate information. More...
 
pieceColor getTurn () const
 (Accessor) Gets the player's turn information. More...
 
void setTurn (pieceColor turn)
 (Mutator) Sets the player's turn information. More...
 
int getNumMoves () const
 (Accessor) Gets the number of moves made. More...
 
void setNumMoves (int num_moves)
 (Mutator) Sets the number of moves made on the board. More...
 
vector< pair< int, char > > getReservoir () const
 Gets the current reservoir information (pieces and quantity). More...
 
void setReservoir (const vector< pair< int, char >> &reservoir)
 Sets the piece reservoir after a piece on the current board representation is replaced. More...
 
bool useReservoirPiece (int src, int dest)
 At any turn, a player can replace one of their pieces with a piece from the reservoir if the resources are available. More...
 
void boardInit ()
 Places the pieces on the board at their correct starting positions. More...
 
bool makeMove (int src, int dest, istream &in)
 Moves a piece on the board from 'src' to 'dest' if conditions for a legal move are met. More...
 
void isCheckmate (string check_type)
 Decide if a move caused a checkmate according to 'check_type'. More...
 
bool isStalemate ()
 Decide if a move caused a stalemate. More...
 

Private Member Functions

void makeMoveForType (int src, int dest)
 A move can be one of: attack, castle, en-passant, regular. More...
 
void pieceSwap (int src, int dest, vector< Piece * > &board)
 Used in Chess::makeMoveForType(int src, int dest) to swap pieces on the board. More...
 
void handleChangeTurn ()
 Indicates who will move next via a message to console. More...
 
void handleCheckmate ()
 Indicates which player won by checkmate via a message to console. More...
 
void handleStalemate ()
 Indicates the game is drawn via a message to console. More...
 
bool singleCheckPieceIterator (Piece *piece, Piece *king)
 If in a single check, see if piece can defend the king, capture attacking piece, or move the king out of check. Used in isCheckmate("single") More...
 
bool doubleCheckPieceIterator (Piece *king)
 If in a double check, see if the king can move out of check as this is the only valid move option. Used in isCheckmate("double"). More...
 
pieceColor switchTurn ()
 Decides whose turn it is currently and updates the private member variable ('turn') accordingly. More...
 

Private Attributes

vector< Piece * > board
 
vector< Piece * > check_pieces
 
vector< bool > flags
 
vector< pair< int, char > > reservoir
 
pieceColor turn
 
int num_moves
 

Friends

ostream & operator<< (ostream &out, const Chess &chess_object)
 Overloaded extraction operator. More...
 
istream & operator>> (istream &in, Chess &chess_object)
 Overlaoded insertion operator. More...
 

Detailed Description

This class describes the chess board on which the game takes place. It contains functions which analyze specific flags to determinewhen to switch turns, display warnings, and even end the game.

Constructor & Destructor Documentation

◆ Chess() [1/2]

Chess::Chess ( )

Default constructor with default board parameter initialization - Constructs a new instance.

◆ ~Chess()

Chess::~Chess ( )

Destroys the object and frees any dynamically allocated memory ('new') to avoid memory leaks.

Note
This is needed since a game can end before a corresponding checkmate/stalemate flag is reached and thus 'board' and 'check_pieces' will have dynamically allocated memory that isn't freed.

◆ Chess() [2/2]

Chess::Chess ( const Chess chess_object)

Copy constructor - Constructs a new instance and copies the calling object's values to it.

Parameters
[in]chess_objectThe object whose values will be copied

Member Function Documentation

◆ operator=()

Chess & Chess::operator= ( const Chess chess_object)

Copy Assignment operator - assigns values of one object to another existing object.

Parameters
[in]chess_objectThe object whose values will be copied
Returns
The resulting object from the assignment

◆ getBoard()

vector<Piece*> Chess::getBoard ( ) const
inline

(Accessor) Gets the board representation at the top of the board positions stack.

Returns
The board with current piece positions in correct indicies.

◆ setBoard()

void Chess::setBoard ( const vector< Piece * > &  board)
inline

(Mutator) Updates the board representation at the top of the board positions stack.

Parameters
[in]boardThe current board representation

◆ getCheckPieces()

vector<Piece*> Chess::getCheckPieces ( ) const
inline

(Accessor) Gets the check stack information.

Returns
The check stack after any given move.

◆ setCheckPieces()

void Chess::setCheckPieces ( const vector< Piece * > &  check_pieces)
inline

(Mutator) Sets the check stack information.

Parameters
[in]check_piecesThe check stack which contains the board representations after each move

◆ getCheck()

bool Chess::getCheck ( ) const
inline

(Accessor) Gets the check information.

Returns
True if board representation has a check, False otherwise.

◆ setCheck()

void Chess::setCheck ( bool  check)
inline

(Mutator) Sets the check information.

Parameters
[in]checkThe check flag

◆ getDoubleCheck()

bool Chess::getDoubleCheck ( ) const
inline

(Accessor) Gets the double check information.

Returns
True if board representation has a double check, False otherwise.

◆ setDoubleCheck()

void Chess::setDoubleCheck ( bool  double_check)
inline

(Mutator) Sets the double check information.

Parameters
[in]double_checkThe double check flag

◆ getCheckmate()

bool Chess::getCheckmate ( ) const
inline

(Accessor) Gets the checkmate information.

Returns
True if board representation has a checkmate, False otherwise.

◆ setCheckmate()

void Chess::setCheckmate ( bool  checkmate)
inline

(Mutator) Sets the checkmate information.

Parameters
[in]checkmateThe checkmate flag

◆ getStalemate()

bool Chess::getStalemate ( ) const
inline

(Accessor) Gets the stalemate information.

Returns
True if board representation has a stalemate, False otherwise.

◆ setStalemate()

void Chess::setStalemate ( bool  stalemate)
inline

(Mutator) Sets the stalemate information.

Parameters
[in]stalemateThe stalemate flag

◆ getTurn()

pieceColor Chess::getTurn ( ) const
inline

(Accessor) Gets the player's turn information.

Returns
The turn at any given moment (either white's or black's).

◆ setTurn()

void Chess::setTurn ( pieceColor  turn)
inline

(Mutator) Sets the player's turn information.

Parameters
[in]turnThe turn

◆ getNumMoves()

int Chess::getNumMoves ( ) const
inline

(Accessor) Gets the number of moves made.

Returns
The number of moves.

◆ setNumMoves()

void Chess::setNumMoves ( int  num_moves)
inline

(Mutator) Sets the number of moves made on the board.

Parameters
[in]num_movesThe number of moves made

◆ getReservoir()

vector<pair<int, char> > Chess::getReservoir ( ) const
inline

Gets the current reservoir information (pieces and quantity).

Returns
The piece reservoir.

◆ setReservoir()

void Chess::setReservoir ( const vector< pair< int, char >> &  reservoir)
inline

Sets the piece reservoir after a piece on the current board representation is replaced.

Parameters
[in]reservoirThe new piece reservoir information

◆ useReservoirPiece()

bool Chess::useReservoirPiece ( int  src,
int  dest 
)

At any turn, a player can replace one of their pieces with a piece from the reservoir if the resources are available.

Parameters
[in]srcThe piece's source square (this will be an ASCII code in [110, 114] depending on the character the user enters)
[in]destThe piece's destination square (piece that will be replaced on the board)
Returns
True if replacement is applied, False otherwise.

◆ boardInit()

void Chess::boardInit ( )

Places the pieces on the board at their correct starting positions.

Precondition
The chess object is intialized
Postcondition
Instantiates new objects corresponding to the pieces, places them in the corresponding index of the board vector and set the global object's board variable

◆ makeMove()

bool Chess::makeMove ( int  src,
int  dest,
istream &  in 
)

Moves a piece on the board from 'src' to 'dest' if conditions for a legal move are met.

Parameters
[in]srcThe source square (piece's current location)
[in]destThe destination square (piece's ending location)
ininput stream type (stdin or file)
Precondition
The chess object is created.
Postcondition
The pieces at 'src' and 'dest' positions are swapped. If needed (attacking, castling, etc.) an empty square is made. The board's state is updated to indicate that the move occured. On failure, an error message is printed and user is asked to retry.
Returns
True if move was made, False otherwise.

◆ isCheckmate()

void Chess::isCheckmate ( string  check_type)

Decide if a move caused a checkmate according to 'check_type'.

Parameters
[in]check_typeEither "single" or "double" corresponding to the check on the board
Precondition
The chess object is created.
Postcondition
If board's state is in checkmate, calls Chess::handleCheckmate() to print messages to console indicating the winner. Else, game continues as usual.

◆ isStalemate()

bool Chess::isStalemate ( )

Decide if a move caused a stalemate.

Precondition
The chess object is created.
Postcondition
Calls Chess::handleStalemate() to print messages to console indicating that game is drawn, if needed.
Returns
True if board's state is in stalemate, else False and game continues as usual.

◆ makeMoveForType()

void Chess::makeMoveForType ( int  src,
int  dest 
)
private

A move can be one of: attack, castle, en-passant, regular.

Parameters
[in]srcThe source square of piece
[in]destThe destination square of piece
Precondition
The chess object is created
Postcondition
Swaps the pieces on the board according to 'src' and 'dest' and proper chess rules, using Chess::pieceSwap(int src, int dest, vector<Piece*> & board). If a new empty square must be created, this is handled. Returns board representation with the made move.

◆ pieceSwap()

void Chess::pieceSwap ( int  src,
int  dest,
vector< Piece * > &  board 
)
private

Used in Chess::makeMoveForType(int src, int dest) to swap pieces on the board.

Parameters
[in]srcThe source square of piece
[in]destThe destination square of piece
boardThe current board representation
Precondition
The chess object is created
Postcondition
Swaps the pieces on the board according to 'src' and 'dest'.

◆ handleChangeTurn()

void Chess::handleChangeTurn ( )
private

Indicates who will move next via a message to console.

Precondition
The chess object is created
Postcondition
Player turn is switched, board is printed, and message is displayed if game is not over to indicate whose turn it is.

◆ handleCheckmate()

void Chess::handleCheckmate ( )
private

Indicates which player won by checkmate via a message to console.

Precondition
The chess object is created. A move was made (cannot checkmate in less than 2 moves in theory).
Postcondition
Object's checkmate state is set to true (to end the algorithm)

◆ handleStalemate()

void Chess::handleStalemate ( )
private

Indicates the game is drawn via a message to console.

Precondition
The chess object is created. A move was made.
Postcondition
Object's stalemate state is set to true (to end the algorithm)

◆ singleCheckPieceIterator()

bool Chess::singleCheckPieceIterator ( Piece piece,
Piece king 
)
private

If in a single check, see if piece can defend the king, capture attacking piece, or move the king out of check. Used in isCheckmate("single")

If in a single check, see if piece can defend the king, capture attacking piece, or move the king out of check. Used in Chess::isCheckmate("single")

Parameters
pieceThe piece that is attacking the king currently
kingThe king that is being attacked currently
Precondition
The chess object is created. A move was made.
Postcondition
None
Returns
True if no legal moves found (checkmate), else False and make the move

◆ doubleCheckPieceIterator()

bool Chess::doubleCheckPieceIterator ( Piece king)
private

If in a double check, see if the king can move out of check as this is the only valid move option. Used in isCheckmate("double").

If in a double check, see if the king can move out of check as this is the only valid move option. Used in Chess::isCheckmate("double").

Parameters
kingThe king that is being attacked currently
Precondition
The chess object is created.
Postcondition
None
Returns
True if no legal moves found (checkmate), else False and make the move

◆ switchTurn()

pieceColor Chess::switchTurn ( )
private

Decides whose turn it is currently and updates the private member variable ('turn') accordingly.

Precondition
The chess object is created.
Postcondition
None
Returns
'chess.turn' is set to the correct player

Friends And Related Function Documentation

◆ operator<<

ostream& operator<< ( ostream &  out,
const Chess chess_object 
)
friend

Overloaded extraction operator.

Parameters
outThe output type (ex. ofstream or cout)
[in]chess_objectThe chess object
Returns
The output stream type

◆ operator>>

istream& operator>> ( istream &  in,
Chess chess_object 
)
friend

Overlaoded insertion operator.

Parameters
inThe input type (ex. ifstream or cin)
chess_objectThe chess object
Returns
The input stream type

Member Data Documentation

◆ board

vector<Piece*> Chess::board
private

The current board representation of the pieces in the correct positions

◆ check_pieces

vector<Piece*> Chess::check_pieces
private

The pieces involved in a check

◆ flags

vector<bool> Chess::flags
private

Check, Double Check, Checkmate, Stalemate flags

◆ reservoir

vector<pair<int, char> > Chess::reservoir
private

Piece reservoir information - Unique to chessCAMO

◆ turn

pieceColor Chess::turn
private

For deciding whose turn it is to make a move for a given board representation

◆ num_moves

int Chess::num_moves
private

The number of moves made already


The documentation for this class was generated from the following files: