Question
Consider the following class declaration: class Chesspiece { public: Chesspiece(int initRank, initFile); int getRank() const; int getFile() const; void move(int newRank, int newFile); bool validMove(int
Consider the following class declaration:
class Chesspiece
{ public:
Chesspiece(int initRank, initFile);
int getRank() const;
int getFile() const;
void move(int newRank, int newFile);
bool validMove(int newRank, int newFile) const;
private:
int rank;
int file;
};
Chess pieces reside on an 8x8 matrix referred to as a "board". The row occupied by a piece is referred to
as its rank," and the column occupied by the piece is referred to as its "file". (Note file here does not
mean an input or output file). For this problem, we will consider "rank" and "file" as indices (0-7) of the
two-dimensional board array. The constructor initializes rank and file to the given argument values
(you can assume the argument values give a valid board location). The getRank() and getFile()
member functions return the current rank and file of the piece, respectively; the move() member
function sets the rank and file to newRank and newFile, respectively; and the validMove()
member function returns true if moving to the proposed new location would be a legal move (for this
class, a move is considered legal if it is on the board, i.e., the rank and file values are both between 0
and 7, inclusive).
a) Write the implementation file (i.e., Chesspiece.cpp). You may assume the interface (.hpp) file is
as given above, but with any necessary includes, defines, etc.
b) Write two classes (.hpp and .cpp files) derived from Chesspiece, named King and Rook, to
represent king and rook pieces, respectively. However, do not write the implementation of the
constructors yet, and do not add any member variables or member functions except the following: each
class simply redefines the validMove()member function to check a move:
King: a king can move to any board location 1 row and/or 1 column in any direction from its
current location. Note this includes not only moving one square up, down, left, and right,
but also one square in any diagonal direction.
Rook: a rook can move to any board location that is either any in column on the current row, or
in any row of the current column (but not both).
c) Rewrite the base class definition to include a private member variable named color (type char) to
represent the color ('b' or 'w') of the chess piece. Also, modify the base class constructor to initialize
the color of the piece.
d) Modify the King class definition to include a constructor that will initialize the color of the piece.
[Hint: you will need to explicitly invoke the correct base class constructor.]
e) Write C++ statements that declare and initialize a King object that is located at row 7 column 3, and
has color b, then ask a user to input the row and column of a proposed move. Once the values are
entered, the code should check if it is a valid move. If it is, the code should move the object to that
location. If it is not, the code should print out a message Not a valid move.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started