Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following class declaration: class Chesspiece { public: Chesspiece(

ID: 3919360 • Letter: C

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 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”.

Explanation / Answer

a) hpp file

#include<iostream>
#include<string>

using namespace std;


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;
};

cpp file

#include "Chesspiece.h"

Chesspiece::Chesspiece(int r, int f){
   rank = r;
   file = f;
}

int Chesspiece::getRank() const{
   return rank;
  
}

int Chesspiece::getFile() const{
   return file;
  
}

bool Chesspiece::validMove(int a, int b) const{
   if ( a < 0 || a > 7 || b < 0 || b >7)
      return false;
   else
      return true;
  
}

void move(int a, int b){

       rank = a;
       file = b;

}

b)

King.h

#include "Chesspiece.h"

class King : public Chesspiece {
       
};

King.cpp

#include "King.h"

bool King::validMove(int a, int b){

    if (!Chesspiece::validMove(a,b))
       return false;

    int diffr = rank - a;
    int difff = file - b;
   
    if (diffr == 0 && difff == 0)
        return false;

    if ( diffr < -1 || diffr > 1 || difff < -1 || difff > 1)
       return false;
    else
       return true;
}

Rook.h

#include "Chesspiece.h"

class Rook : public Chesspiece {
       
};

Rook.cpp

#include "Rook.h"

bool Rook::validMove(int a, int b){

    if (!Chesspiece::validMove(a,b))
       return false;

    int diffr = rank - a;
    int difff = file - b;
   
    if (diffr == 0 && difff == 0)
        return false;
   
    if ( (diffr == 0 && difff > -8 && difff < 8) || difff == 0 && diffr > -8 && diffr < 8)
       return true;
    else
       return false;
}

c)

hpp

class Chesspiece
{ public:
Chesspiece(int initRank, initFile, char c);
int getRank() const;
int getFile() const;
void move(int newRank, int newFile);
bool validMove(int newRank, int newFile) const;
private:
int rank;
int file;
char color;
};

cpp

Chesspiece::Chesspiece(int r, int f, char c){
   rank = r;
   file = f;
   color = c;
}

int Chesspiece::getRank() const{
   return rank;
  
}

int Chesspiece::getFile() const{
   return file;
  
}

d)

#include "Chesspiece.h"

class King : public Chesspiece {
     public:
         King(int a, int b, char c): Chesspiece(a,b,c){
         }
};

e)

    King k(7,3,'b');
    cout << "Enter a proposed move (rank file):";
    int a,b;
    cin >> a >> b;
    if (k.validMove(a,b))
       k.move(a,b)
    else
       cout << "Not a valid move ";

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote