Write a pair of classes that can simulate moving pieces on a chess board. The fi
ID: 3745093 • 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 rnbgkbnrExplanation / Answer
//Assumptions for a valid move -> If both the rows and columns of a move are within the dimension of
// chess board then the move is valid else it's invalid.
#include<string.h>
class ChessMove
{
int startRow,startCol,endRow,endCol;
public:
ChessMove(string move ){
startRow = move[1] - 49;
startCol = move[0] - 97;
endRow = move[4] - 49;
endCol = move[3] - 97;
//check whether all the rows and cols of a move are within chess board dimensions
if( startRow < 0 || startRow >7 || startCol < 0 || startCol > 7 || endRow <0 || endRow >7 || endCol <0 || endCol >7) {
startRow = 0;startCol = 0; endRow = 0; endCol = 0;
}
}
int getStartRow() {
return startRow;
}
int getStartCol() {
return startCol;
}
int getEndRow() {
return endRow;
}
int getEndCol() {
return endCol;
}
};
class ChessBoard {
char board[8][9];
public:
ChessBoard() {
for(int i = 0;i < 8;i++) {
if(i==0)
strcpy(board[i],"RNBQKBNR");
else if(i == 1)
strcpy(board[i],"PPPPPPPP");
else if(i==6)
strcpy(board[i],"pppppppp");
else if(i==7)
strcpy(board[i],"rnbqkbnr");
else
strcpy(board[i],"........");
}
}
void makeMove(ChessMove move){
//assuming that for each chess piece,the move is a valid one
int startRow = move.getStartRow();
int startCol = move.getStartCol();
int endRow = move.getEndRow();
int endCol = move.getEndCol();
if( !(startRow == 0 && startCol == 0 && endRow == 0 && endCol == 0 )) {
//swap the contents at the start position with end position
char temp = board[startRow][startCol];
board[startRow][startCol] = board[endRow][endCol];
board[endRow][endCol] = temp;
}
}
void print() {
for(int i=0;i<8; i++)
cout << board[i] << endl;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.