Start with the base code given below. Get it to compile and run as shown. THEN, make the modifications shown below.
Important notes:
- The modifications are the assignment!
- This assignment will require you to think, research and explore, it is not a step-by-step!
- Work on this assignment on your own!
BASE CODE:
#include
#include
#include
using namespace std;
const int NUM_ROWS = 8;
const int NUM_COLS = 8;
// **************** CLASS: CELL *******************
class Cell {
char piece;
char color;
public:
Cell();
void place(char color, char piece);
string getPiece();
};
Cell::Cell() {
piece = ' ';
color = ' ';
}
void Cell::place(char newColor, char newPiece) {
assert((newColor == 'W') || (newColor == 'B'));
color = newColor;
assert((newPiece == 'R') || (newPiece == 'K') ||
(newPiece == 'B') || (newPiece == 'Q') ||
(newPiece == 'K') || (newPiece == 'N') ||
(newPiece == 'P'));
piece = newPiece;
}
string Cell::getPiece() {
string result = "";
result = result.append(1, color);
result = result.append(1, piece);
return result;
}
// **************** CLASS: BOARD *******************
class Board {
Cell board[NUM_ROWS][NUM_COLS]; // <-- Not a good idea in the long run
void displayLine();
public:
Board();
void displayBoard();
};
Board::Board() {
board[0][0].place('B', 'R');
board[0][1].place('B', 'N');
board[0][2].place('B', 'B');
board[0][3].place('B', 'Q');
board[0][4].place('B', 'K');
board[0][5].place('B', 'B');
board[0][6].place('B', 'N');
board[0][7].place('B', 'R');
for (int c = 0; c < NUM_COLS; c++) {
board[1][c].place('B', 'P');
}
board[NUM_COLS - 1][0].place('W', 'R');
board[NUM_COLS - 1][1].place('W', 'N');
board[NUM_COLS - 1][2].place('W', 'B');
board[NUM_COLS - 1][4].place('W', 'K');
board[NUM_COLS - 1][3].place('W', 'Q');
board[NUM_COLS - 1][5].place('W', 'B');
board[NUM_COLS - 1][6].place('W', 'N');
board[NUM_COLS - 1][7].place('W', 'R');
for (int c = 0; c < NUM_COLS; c++) {
board[NUM_COLS-2][c].place('W', 'P');
}
}
void Board::displayLine() {
cout << endl;
for (int x = 0; x < NUM_COLS; x++) {
cout << " | ";
}
cout << endl;
for (int x = 0; x < NUM_COLS; x++) {
cout << "----| ";
}
cout << endl;
}
void Board::displayBoard() {
cout << endl << "CURRENT BOARD:" << endl << endl;
for (int r = 0; r < NUM_ROWS; r++) {
for (int c = 0; c < NUM_COLS; c++) {
cout << " " << board[r][c].getPiece() << " | ";
}
displayLine();
}
cout << endl << endl;
}
int main() {
Board board;
board.displayBoard();
return 0;
}
OUTPUT FROM BASE CODE:
CURRENT BOARD:
BR | BN | BB | BQ | BK | BB | BN | BR |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
BP | BP | BP | BP | BP | BP | BP | BP |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
| | | | | | | |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
| | | | | | | |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
| | | | | | | |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
| | | | | | | |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
WP | WP | WP | WP | WP | WP | WP | WP |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
WR | WN | WB | WQ | WK | WB | WN | WR |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
MODIFICATIONS:
Add three lines to main as shown below. Modify the program by adding whatever is necessary to get it to work as shown in the output below.
NEW MAIN:
int main() {
Board board;
board.displayBoard();
string piece = board.take(0, 1);
board.place(2, 2, piece.at(0), piece.at(1));
board.displayBoard();
return 0;
}
NEW OUPUT:
CURRENT BOARD:
BR | BN | BB | BQ | BK | BB | BN | BR |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
BP | BP | BP | BP | BP | BP | BP | BP |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
| | | | | | | |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
| | | | | | | |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
| | | | | | | |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
| | | | | | | |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
WP | WP | WP | WP | WP | WP | WP | WP |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
WR | WN | WB | WQ | WK | WB | WN | WR |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
CURRENT BOARD:
BR | | BB | BQ | BK | BB | BN | BR |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
BP | BP | BP | BP | BP | BP | BP | BP |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
| | BN | | | | | |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
| | | | | | | |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
| | | | | | | |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
| | | | | | | |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
WP | WP | WP | WP | WP | WP | WP | WP |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|
WR | WN | WB | WQ | WK | WB | WN | WR |
| | | | | | | |
----| ----| ----| ----| ----| ----| ----| ----|