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

Using the code given in class as a base, write functions to move each of the che

ID: 674898 • Letter: U

Question

Using the code given in class as a base, write functions to move each of the chess pieces. Do them in the following order to maximize your grade:

Piece

% grade

Rook

20%

Bishop

20%

Queen

20%

King

20%

Knight

10%

Pawn

10%

You do not have to concern yourself with:

En passante

Castling

Moving into check

Any piece capturing another piece; assume that they can not collide for now.

All of these must be accessed as shown below:

int main() {

      Board board;

      cout << "BEGINNING BOARD:" << endl;

      board.displayBoard();

      board.turn(1, 0, "S", 1);

      board.turn(2, 0, "N", 1);

      board.turn(2, 0, "S", 1);

      board.turn(3, 0, "S", 1);

      board.turn(0, 0, "S", 3);

      board.turn(3, 0, "E", 5);

      board.turn(3, 5, "N", 3);

      cout << "ENDING BOARD:" << endl;

      board.displayBoard();

      return 0;

}

REMEMBER:

Catch out-of-bounds errors!

Do not use any global variables!

SAMPLE RUN (FROM ABOVE CODE IN main) ON THE FOLLOWING PAGES!

BEGINNING BOARD:

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 |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

MORE Þ

*** MOVING BP FROM [1, 0] Direction:S #steps: 1

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 |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

MORE Þ

*** MOVING BP FROM [2, 0] Direction:N #steps: 1

Black pawns can not move N!

Failed to move the piece the entire way!

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 |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

MORE Þ

*** MOVING BP FROM [2, 0] Direction:S #steps: 1

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 |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

MORE Þ

*** MOVING BP FROM [3, 0] Direction:S #steps: 1

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 |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

MORE Þ

*** MOVING BR FROM [0, 0] Direction:S #steps: 3

CURRENT BOARD:

    | BN | BB | BQ | BK | BB | BN | BR |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

    | BP | BP | BP | BP | BP | BP | BP |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

    |     |     |     |     |     |     |     |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

BR |     |     |     |     |     |     |     |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

BP |     |     |     |     |     |     |     |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

    |     |     |     |     |     |     |     |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

WP | WP | WP | WP | WP | WP | WP | WP |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

WR | WN | WB | WQ | WK | WB | WN | WR |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

MORE Þ

*** MOVING BR FROM [3, 0] Direction:E #steps: 5

CURRENT BOARD:

    | BN | BB | BQ | BK | BB | BN | BR |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

    | BP | BP | BP | BP | BP | BP | BP |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

    |     |     |     |     |     |     |     |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

    |     |     |     |     | BR |     |     |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

BP |     |     |     |     |     |     |     |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

    |     |     |     |     |     |     |     |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

WP | WP | WP | WP | WP | WP | WP | WP |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

WR | WN | WB | WQ | WK | WB | WN | WR |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

MORE Þ

*** MOVING BR FROM [3, 5] Direction:SE #steps: 2

Rooks can not move SE!

Failed to move the piece the entire way!

CURRENT BOARD:

    | BN | BB | BQ | BK | BB | BN | BR |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

    | BP | BP | BP | BP | BP | BP | BP |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

    |     |     |     |     |     |     |     |

    |     |     |     |     |     |    |     |

----| ----| ----| ----| ----| ----| ----| ----|

    |     |     |     |     | BR |     |     |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

BP |     |     |     |     |     |     |     |

    |    |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

    |     |     |     |     |     |     |     |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

WP | WP | WP | WP | WP | WP | WP | WP |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

WR | WN | WB | WQ | WK | WB | WN | WR |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

MORE Þ

*** MOVING BR FROM [3, 5] Direction:N #steps: 3

Space [ 1, 5] Contains [BP]

Failed to move the piece the entire way!

CURRENT BOARD:

    | BN | BB | BQ | BK | BB | BN | BR |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

    | BP | BP | BP | BP | BP | BP | BP |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

    |     |     |     |     | BR |     |     |

    |     |     |    |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

    |     |     |     |     |     |     |     |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

BP |     |     |     |     |     |     |     |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

    |     |     |     |     |     |     |     |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

WP | WP | WP | WP | WP | WP | WP | WP |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

WR | WN | WB | WQ | WK | WB | WN | WR |

    |     |     |     |     |     |     |     |

----| ----| ----| ----| ----| ----| ----| ----|

MORE Þ

*** MOVING BR FROM [2, 5] Direction:E #steps: 30

Assertion failed: (toCol >= 0) && (toCol < NUM_COLS), file c:userssshafferdoc

uments isual studio 2013projectsssignment5ssignment5.cpp, line 196

Press any key to continue . . .

Piece

% grade

Rook

20%

Bishop

20%

Queen

20%

King

20%

Knight

10%

Pawn

10%

Explanation / Answer

#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
const int NUM_ROWS = 8;
const int NUM_COLS = 8;
class Cell {
    char piece;
    char color;
public:
    Cell();
    void place(char color, char piece);
    string take();
    string getPiece();
};

Cell::Cell() {
    piece = ' ';
    color = ' ';
}

string Cell::take() {
    string result = "";
    result = result.append(1, color);
    result = result.append(1, piece);
    piece = ' ';
    color = ' ';
    return result;
}

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 {
    Cell board[NUM_ROWS][NUM_COLS];
    void displayLine();
public:
    Board();
    void displayBoard();
    void place(int, int, char, char);
    string take(int, int);
    void movePieceOneStep(string, string, int&, int&);
    bool cellEmpty(int, int);
    string look(int, int);
    void turn(int, int, string, int);
};
void Board::place(int row, int col, char color, char piece) {
    assert((row >= 0) && (row < NUM_ROWS));
    assert((col >= 0) && (col < NUM_COLS));
    board[row][col].place(color, piece);
}
string Board::take(int row, int col) {
    assert((row >= 0) && (row < NUM_ROWS));
    assert((col >= 0) && (col < NUM_COLS));
    return board[row][col].take();
}
bool Board::cellEmpty(int toRow, int toCol){
    string cell = board[toRow][toCol].getPiece();
if( cell.empty() ){
       return true;
    }
    else {
return false;
    }
}
string Board::look(int toRow, int toCol){
    string result = board[toRow][toCol].getPiece();
    return result;
}
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;
}
void Board::movePieceOneStep(string piece, string direction,
int &row, int &col) {
    assert((row >= 0) && (row < NUM_ROWS));
    assert((col >= 0) && (col < NUM_COLS));
    int toRow = row;
    int toCol = col;
    if (direction == "S")
   toRow = row + 1;
    else if (direction == "N")
   toRow = row - 1;
    else if (direction == "E")
   toCol = col + 1;
    else if (direction == "W")
   toCol = col - 1;
    else if (direction == "NW") {
   toRow = row - 1;
   toCol = col - 1;
    }
    else if (direction == "NE") {
   toRow = row - 1;
   toCol = col + 1;
    }
    else if (direction == "SW") {
   toRow = row + 1;
   toCol = col - 1;
    }
    else if (direction == "SE") {
   toRow = row + 1;
   toCol = col + 1;
    }
    else {
   cout << "INVALID DIRECTION!" << endl;
   assert(false); // force a failure
    }
assert((toRow >= 0) && (toRow < NUM_ROWS));
    assert((toCol >= 0) && (toCol < NUM_COLS));
if (cellEmpty(toRow, toCol)) {
   cout << "Space [ " << toRow << ", " << toCol <<
    "] Contains [" << look(toRow, toCol) << "]" << endl;
    }
else {   
    piece = take(row, col);
    place(toRow, toCol, piece.at(0), piece.at(1));
    row = toRow;
    col = toCol;
}
}
void Board::turn(int row, int col, string direction, int space){
   string piece = board[row][col].getPiece();
    for(int i = 0; i < space; i++) {
       movePieceOneStep(piece, direction, row, col) ;
}
}
int main() {

    Board board;
cout << "BEGINNING BOARD:" << endl;
    board.displayBoard();
board.turn(0, 2, "S", 1);
cout << "ENDING BOARD:" << endl;
    board.displayBoard();
    return 0;
}

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