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

Write a pair of classes that can simulate moving pieces on a chess board. The fi

ID: 3745236 • Letter: W

Question

Write a pair of classes that can simulate moving pieces on a chess board.

The first of these classes should be a ChessMove. A ChessMove should store 4 data members: starting row and column and ending row and column. It should have a single constructor that takes in a string, formatted as in the Chess Simulator lab (e.g., "c2-c4"). The constructor should initialize the 4 data members from this string, and if the string is an invalid move, initialize the move to be from (0, 0) to (0, 0), as if the input were "a1-a1". The only other member functions ChessMove should have are getStartRow(), getStartCol(), getEndRow(), and getEndCol(), which return their respective data members.

The first and fourth characters of the move ('a'-'h') represent the column (0-7) of the start and end spaces, while the second and fifth ('1'-'8') represent the row (0-7).

The second class is a ChessBoard. A ChessBoard should store the contents of the 8x8 board. It should have a default constructor that initializes a new game of chess, a makeMove() function that takes in a ChessMove and modifies the contents of the board, and a print() function that prints out the board, as in the previous assignment.

The board should print out rooks, knights, bishops, queens, kings, pawns, and empty spaces as 'r', 'n', 'b', 'q', 'k', 'p', and '.', respectively, capitalizing the letters for white (lowercase for black). Initially, the board would appear as:

**Please use and add to the code given!!!:**

main.cpp:

#include
#include "chess.h"
using namespace std;

int main()
{
   int nmoves;
   string str;
   ChessBoard board;

//Read in # of moves
   cin >> nmoves;

   for (int i = 0; i < nmoves; i++)
   {
      //Read in move
      cin >> str;
    
      //Construct and make that move
      ChessMove move(str);
      board.makeMove(move);
   }

   //Print out the board after making all moves
   board.print();

   return 0;
}


chess.h:

#ifndef __CHESS_H
#define __CHESS_H

#include
using namespace std;

enum piece_t {EMPTY, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING};
enum side_t {WHITE, BLACK};

class ChessMove
{
    //FIXME: define ChessMove
};

class ChessBoard
{
   //FIXME: define ChessBoard
};

#endif



chess.cpp

#include "chess.h"
#include
using namespace std;

//FIXME: implement all member functions for ChessMove and ChessBoard

RNBQKBNR rnbgkbnr

Explanation / Answer

-----------------------------------------------------
//chess.h:

#ifndef __CHESS_H
#define __CHESS_H

#include
using namespace std;

enum piece_t {EMPTY, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING};
enum side_t {WHITE, BLACK};

char ChessMatrix[8][8];

class ChessMove
{
public char sc,sr,ec,er; // We may use this variables for decesion making of the valid / invalid moves

void move(char* t) //get your movement elements
{
sr=getStartRow(t);
sc=getStartCol(t);
er=getEndRow(t);
ec=getEndCol(t);
}
char getStartRow(char* t)
{
return t[1]; // Second element of the string is the firts Row
}
char getStartCol(char* t){
return t[0]; // First element of the string is the firts column
}
char getEndRow(char* t){
return t[4]; // last element of the string is the end Row
}
char getEndCol(char* t){
return t[3]; // second last element of the string is the end column
}
};

class ChessBoard
{
void print()
{
for (i=0;i<8;i++) // This will help into printing each rows for the Chess elements
{
for(j=0;j<8;j++) // This will help into printing each columns for the Chess elements
{

if(!ChessMatrix[i][j])
{
printf('.'); // print '.' if field of matrix is not having any element
}
else
{
printf('%c',ChessMatrix[i][j]); // print element itself
}
}
}
}
};

#endif

============================================================================

chess.cpp
-----------------------------------------------------
#include "chess.h"
#include
using namespace std;

//FIXME: implement all member functions for ChessMove and ChessBoard


ChessMatrix={'R','N','B','Q','K','B','N','R',
'P','P','P','P','P','P','P','P',
'','','','','','','','',
'','','','','','','','',
'','','','','','','','',
'','','','','','','','',
'p','p','p','p','p','p','p','p',
'r','n','b','q','k','b','n','r'}
}

Confirm : How to decide valid/invalid 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