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

This class describes the pieces on the board at any given moment. It contains functions for determining the piece's type, color, possible/legal moves, and more helpful utilities for making decisions regarding move candidates. More...

#include <chess.h>

Inheritance diagram for Piece:
Bishop Empty King Knight Pawn Queen Rook

Public Member Functions

virtual ~Piece ()
 
 Piece ()
 Default constructor with default board parameter initialization - Constructs a new instance. More...
 
 Piece (int square, pieceType type, pieceColor color)
 Constructs a new instance with valid piece information initialization. More...
 
int getPieceSquare () const
 (Accessor) Gets the piece square information. More...
 
void setPieceSquare (int square)
 (Mutator) Sets the piece square information. More...
 
pieceType getPieceType () const
 (Accessor) Gets the piece type information. More...
 
void setPieceType (pieceType type)
 (Mutator) Sets the piece type information. More...
 
pieceColor getPieceColor () const
 (Accessor) Gets the piece color information. More...
 
void setPieceColor (pieceColor color)
 (Mutator) Sets the piece color information. More...
 
bool getPieceMoveInfo () const
 (Accessor) Gets the piece move information useful for pawns, rooks, kings. More...
 
void setPieceMoveInfo (bool moved)
 (Mutator) Sets the piece move information. More...
 
virtual bool getEnPassantLeft () const
 (Accessor) Gets the piece en-passant ability information for left side. More...
 
virtual void setEnPassantLeft (bool en_passant_left)
 (Mutator) Sets the piece en-passant ability information for left side. More...
 
virtual bool getEnPassantRight () const
 (Accessor) Gets the piece en-passant ability information for right side. More...
 
virtual void setEnPassantRight (bool en_passant_right)
 (Mutator) Sets the piece en-passant ability information for right side. More...
 
bool isEmpty ()
 Determines if the square is empty. More...
 
bool isPawn ()
 Determines if the piece is a pawn. More...
 
bool isKnight ()
 Determines if the piece is a knight. More...
 
bool isBishop ()
 Determines if the piece is a bishop. More...
 
bool isRook ()
 Determines if the piece is a rook. More...
 
bool isQueen ()
 Determines if the piece is a queen. More...
 
bool isKing ()
 Determines if the piece is a king. More...
 
bool isPieceWhite ()
 Determines if the piece is white. More...
 
bool isPieceBlack ()
 Determines if the piece is black. More...
 
bool isSameColor (int dest, const Chess &chess)
 Determines if 2 pieces have the same color. More...
 
bool isPinned (int dest, const Chess &chess)
 Determines if a given piece is pinned to the king by opposing piece. More...
 
bool isPathFree (int dest, const Chess &chess)
 Determines if the path from the piece to its destination is empty. More...
 
bool isLegalMove (int dest, Chess &chess)
 Determines if a move is legal based on the rules of chess. More...
 
bool causeCheck (int dest, Chess &chess)
 Did the move cause a check? More...
 
bool causeDoubleCheck (int dest, Chess &chess)
 Did the move cause a double check? More...
 
virtual bool isPossibleMove (int dest, const Chess &chess)
 Determine if the piece has a possible move towards the destination square. More...
 
virtual void enPassantHandling (int src, Chess &chess)
 Pawn attacks opposing pawn with en-passant More...
 
virtual void promotePawn (Chess &chess, istream &in)
 Promotes the pawn if needed. More...
 
virtual bool canCastle (int dest, const Chess &chess)
 Can the king castle? More...
 
virtual bool movedIntoCheck (int dest, Chess &chess)
 Did the king move into check? More...
 

Private Attributes

int square
 
bool moved
 
pieceType type
 
pieceColor color
 

Detailed Description

This class describes the pieces on the board at any given moment. It contains functions for determining the piece's type, color, possible/legal moves, and more helpful utilities for making decisions regarding move candidates.

Constructor & Destructor Documentation

◆ ~Piece()

virtual Piece::~Piece ( )
inlinevirtual

◆ Piece() [1/2]

Piece::Piece ( )
inline

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

Note
Intentionally left blank.

◆ Piece() [2/2]

Piece::Piece ( int  square,
pieceType  type,
pieceColor  color 
)
inline

Constructs a new instance with valid piece information initialization.

Parameters
[in]squareThe square of the piece
[in]typeThe type of the piece
[in]colorThe color of the piece
Note
Has 'moved' initialization.

Member Function Documentation

◆ getPieceSquare()

int Piece::getPieceSquare ( ) const
inline

(Accessor) Gets the piece square information.

Returns
The piece square information.

◆ setPieceSquare()

void Piece::setPieceSquare ( int  square)
inline

(Mutator) Sets the piece square information.

Parameters
[in]squareThe piece square information

◆ getPieceType()

pieceType Piece::getPieceType ( ) const
inline

(Accessor) Gets the piece type information.

Returns
The piece type information.

◆ setPieceType()

void Piece::setPieceType ( pieceType  type)
inline

(Mutator) Sets the piece type information.

Parameters
[in]typeThe piece type information

◆ getPieceColor()

pieceColor Piece::getPieceColor ( ) const
inline

(Accessor) Gets the piece color information.

Returns
The piece color information.

◆ setPieceColor()

void Piece::setPieceColor ( pieceColor  color)
inline

(Mutator) Sets the piece color information.

Parameters
[in]colorThe color information

◆ getPieceMoveInfo()

bool Piece::getPieceMoveInfo ( ) const
inline

(Accessor) Gets the piece move information useful for pawns, rooks, kings.

Returns
The piece move information.

◆ setPieceMoveInfo()

void Piece::setPieceMoveInfo ( bool  moved)
inline

(Mutator) Sets the piece move information.

Parameters
[in]movedIndicates if piece moved

◆ getEnPassantLeft()

virtual bool Piece::getEnPassantLeft ( ) const
inlinevirtual

(Accessor) Gets the piece en-passant ability information for left side.

Returns
The piece en-passant ability information in left direction.

Reimplemented in Pawn.

◆ setEnPassantLeft()

virtual void Piece::setEnPassantLeft ( bool  en_passant_left)
inlinevirtual

(Mutator) Sets the piece en-passant ability information for left side.

Parameters
[in]en_passant_leftThe piece en-passant ability information in left direction
Note
Intentionally left blank.

Reimplemented in Pawn.

◆ getEnPassantRight()

virtual bool Piece::getEnPassantRight ( ) const
inlinevirtual

(Accessor) Gets the piece en-passant ability information for right side.

Returns
The piece en-passant ability information in right direction.

Reimplemented in Pawn.

◆ setEnPassantRight()

virtual void Piece::setEnPassantRight ( bool  en_passant_right)
inlinevirtual

(Mutator) Sets the piece en-passant ability information for right side.

Parameters
[in]en_passant_rightThe piece en-passant ability information in right direction
Note
Intentionally left blank.

Reimplemented in Pawn.

◆ isEmpty()

bool Piece::isEmpty ( )
inline

Determines if the square is empty.

Returns
True if the square is empty, False otherwise.

◆ isPawn()

bool Piece::isPawn ( )
inline

Determines if the piece is a pawn.

Returns
True if the piece is a pawn, False otherwise.
Note
Color of the piece is not considered

◆ isKnight()

bool Piece::isKnight ( )
inline

Determines if the piece is a knight.

Returns
True if the piece is a knight, False otherwise.
Note
Color of the piece is not considered

◆ isBishop()

bool Piece::isBishop ( )
inline

Determines if the piece is a bishop.

Returns
True if the piece is a bishop, False otherwise.
Note
Color of the piece is not considered

◆ isRook()

bool Piece::isRook ( )
inline

Determines if the piece is a rook.

Returns
True if the piece is a rook, False otherwise.
Note
Color of the piece is not considered

◆ isQueen()

bool Piece::isQueen ( )
inline

Determines if the piece is a queen.

Returns
True if the piece is a queen, False otherwise.
Note
Color of the piece is not considered

◆ isKing()

bool Piece::isKing ( )
inline

Determines if the piece is a king.

Returns
True if the piece is a king, False otherwise.
Note
Color of the piece is not considered

◆ isPieceWhite()

bool Piece::isPieceWhite ( )
inline

Determines if the piece is white.

Returns
True if the piece is white, False otherwise.

◆ isPieceBlack()

bool Piece::isPieceBlack ( )
inline

Determines if the piece is black.

Returns
True if the piece is black, False otherwise.

◆ isSameColor()

bool Piece::isSameColor ( int  dest,
const Chess chess 
)

Determines if 2 pieces have the same color.

Parameters
[in]destThe destination square of the piece
chessThe chess object
Returns
True if source piece color matches destination piece color, False otherwise.

◆ isPinned()

bool Piece::isPinned ( int  dest,
const Chess chess 
)

Determines if a given piece is pinned to the king by opposing piece.

Parameters
[in]destThe destination square of the piece
chessThe chess object
Returns
True if piece is pinned to the king and moving to 'dest' will cause the path (pinning piece -> king from pinned piece side) to be free, False otherwise.

◆ isPathFree()

bool Piece::isPathFree ( int  dest,
const Chess chess 
)

Determines if the path from the piece to its destination is empty.

Parameters
[in]destThe destination square of the piece
chessThe chess object
Returns
True if squares along the path (src, dest) are empty, False otherwise.

◆ isLegalMove()

bool Piece::isLegalMove ( int  dest,
Chess chess 
)

Determines if a move is legal based on the rules of chess.

Parameters
[in]destThe destination square of the piece
chessThe chess object
Returns
True if moving the piece to 'dest' is legal from any type of move and piece, False otherwise.
Note
A possible move, is not necessarily legal.

◆ causeCheck()

bool Piece::causeCheck ( int  dest,
Chess chess 
)

Did the move cause a check?

Parameters
[in]destThe destination square of the piece
chessThe chess object
Returns
True if moving the piece to 'dest' now threatens the opposing king. False otherwise.

◆ causeDoubleCheck()

bool Piece::causeDoubleCheck ( int  dest,
Chess chess 
)

Did the move cause a double check?

Parameters
[in]destThe destination square of the piece
chessThe chess object
Returns
True if moving the piece to 'dest' now threatens the opposing king, and an additional piece from the same side also has a legal move towards the opposing king. False otherwise.

◆ isPossibleMove()

virtual bool Piece::isPossibleMove ( int  dest,
const Chess chess 
)
inlinevirtual

Determine if the piece has a possible move towards the destination square.

Parameters
[in]destThe destination square of the piece
chessThe chess object
Returns
True if moving the piece to 'dest' is possible since the path is free, or the piece is capable of making the move. False otherwise.

Reimplemented in King, Queen, Rook, Bishop, Knight, and Pawn.

◆ enPassantHandling()

virtual void Piece::enPassantHandling ( int  src,
Chess chess 
)
inlinevirtual

Pawn attacks opposing pawn with en-passant

Parameters
[in]srcThe source square of the piece
chessThe chess object
Precondition
None
Postcondition
En-passant private member is set to true if a pawn meets the criteria, else all pawns have their en-passant abilities set to false.

Reimplemented in Pawn.

◆ promotePawn()

virtual void Piece::promotePawn ( Chess chess,
istream &  in 
)
inlinevirtual

Promotes the pawn if needed.

Parameters
chessThe chess object
ininput stream type (stdin or file)
Precondition
Pawn must be on a rank that can promote next move
Postcondition
Changes the piece (pawn) to a stronger piece according to user input

Reimplemented in Pawn.

◆ canCastle()

virtual bool Piece::canCastle ( int  dest,
const Chess chess 
)
inlinevirtual

Can the king castle?

Parameters
[in]destThe destination square of the piece
chessThe chess object
Returns
True if able to castle, False otherwise.

Reimplemented in King.

◆ movedIntoCheck()

virtual bool Piece::movedIntoCheck ( int  dest,
Chess chess 
)
inlinevirtual

Did the king move into check?

Parameters
[in]destThe destination square of the piece
chessThe chess object
Returns
True if a king moves into a square that another opposing piece also move into. False otherwise.

Reimplemented in King.

Member Data Documentation

◆ square

int Piece::square
private

position of the piece on the board [0, 63] -> [top left, bottom right]

◆ moved

bool Piece::moved
private

has the piece been moved yet?

◆ type

pieceType Piece::type
private

PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING, OR EMPTY

See also
pieceType

◆ color

pieceColor Piece::color
private

BLACK, NEUTRAL, OR WHITE

See also
pieceColor

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