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

Base Code: #include <iostream> #include <string> #include <assert.h> using names

ID: 3757634 • Letter: B

Question

Base Code:

#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);

    bool movePieceOneStep(string, string, int&, int&);

    bool cellEmpty(int, int);

    string look(int, int);

    void turn(int, int, string, int);

    bool moveRook(int row, int col, string direction, int steps);

    bool movePawn(int row, int col, string direction, int steps);

    bool moveQueen(int row, int col, string direction, int steps);

    bool moveKing(int row, int col, string direction, int steps);

    bool moveKnight(int row, int col, string direction, int steps);

    bool moveBishop(int row, int col, string direction, int steps);

};

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;

}

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

    }

    return true;

}

void Board::turn(int row, int col, string direction, int space){

    string piece = board[row][col].getPiece();

    if(piece.at(1) == 'R'){

        if(moveRook(row, col, direction, space)){

            //output message

        }

        else{

            //output message

        }

    }

    else if(piece.at(1) == 'K'){

        if(moveKing(row, col, direction, space)){

            //output message

        }

        else{

            //output message

        }

    }

    else if(piece.at(1) == 'P'){

        if(movePawn(row, col, direction, space)){

          //output message

        }

        else{

            //output message

        }

    }

    else if(piece.at(1) == 'Q'){

        if(moveQueen(row, col, direction, space)){

            //output message

        }

        else{

            //output message

        }

    }

    else if(piece.at(1) == 'N'){

        if(moveKnight(row, col, direction, space)){

            //output message

        }

        else{

            //output message

        }

    }

    else if(piece.at(1) == 'B'){

        if(moveBishop(row, col, direction, space)){

            //output message

        }

        else{

            //output message

        }

    }

    else{

        //error message

    }

    //for(int i = 0; i < space; i++) {

    // movePieceOneStep(piece, direction, row, col) ;

}

int main() {

    Board board;

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

    board.displayBoard();

    // BLACK

    board.turn(1, 4, "S", 1);               // PAWN

    board.turn(2, 4, "S", 1);               // PAWN

    board.turn(3, 4, "S", 1); // PAWN

    board.turn(4, 4, "S", 1); // PAWN

    board.turn(1, 7, "S", 1); // PAWN

    board.turn(2, 7, "S", 1); // PAWN

    board.turn(3, 7, "S", 1); // PAWN

    board.turn(4, 7, "S", 1); // PAWN

    board.turn(1, 3, "S", 1); // PAWN

    board.turn(2, 3, "S", 1); // PAWN

    board.turn(3, 3, "S", 1); // PAWN

    board.turn(4, 3, "S", 1); // PAWN

    board.turn(0, 2, "SE", 4);            // BISHOP

    board.turn(0, 3, "S", 4);               // QUEEN

    board.turn(4, 3, "W", 3);            // QUEEN

    board.turn(4, 0, "NE", 3);           // QUEEN

    board.turn(0, 4, "S", 1);               // KING

    board.turn(1, 4, "S", 1);               // KING

    board.turn(2, 4, "S", 1);               // KING

    board.turn(3, 4, "S", 1);               // KING

    board.turn(4, 4, "W", 1);            // KING

    board.turn(4, 3, "NE", 1);           // KING

    board.turn(0, 7, "S", 2);               // ROOK

    board.turn(2, 7, "W", 5);            // ROOK

    board.turn(0, 6, "SSW", 1);       // KNIGHT

    board.turn(2, 5, "SEE", 1);          // KNIGHT

   

    // WHITE

    board.turn(6, 0, "N", 1);              // PAWN

    board.turn(6, 1, "N", 1);              // PAWN

    board.turn(6, 2, "N", 1);              // PAWN

    board.turn(5, 0, "N", 1);              // PAWN

    board.turn(5, 1, "N", 1);              // PAWN

    board.turn(5, 2, "N", 1);              // PAWN

    board.turn(4, 0, "N", 1);              // PAWN

    board.turn(4, 1, "N", 1);              // PAWN

    board.turn(4, 2, "N", 1);              // PAWN

    board.turn(6, 5, "N", 1);              // PAWN

    board.turn(7, 2, "NW", 1);         // BISHOP

    board.turn(6, 1, "NE", 2);           // BISHOP

    board.turn(7, 0, "N", 3);              // ROOK

    board.turn(4, 0, "E", 2);               // ROOK

    board.turn(7, 3, "NW", 3);         // QUEEN

    board.turn(7, 4, "NE", 1);           // KING

    board.turn(7, 1, "NNE", 1);        // KNIGHT

    board.turn(5, 2, "NEE", 1);         // KNIGHT

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

    board.displayBoard();

    return 0;

}

////

bool Board::moveRook(int row, int col, string direction, int steps) {

   

    string piece = look(row, col);

    assert(piece.at(1) == 'R');

   

    if (!((direction[0] == 'N') || (direction[0] == 'S') || (direction[0] == 'W') || (direction[0] == 'E'))) {

        cout << "Rooks can not move " << direction << "!" << endl;

        return false;

    }

    for (int x = 0; x < steps; x++) {

        if (!movePieceOneStep(piece, direction, row, col))

            return false;

    }

    return true;

}

bool Board::movePawn(int row, int col, string direction, int steps) {

                  //int newRow;

                  //assert((row >= 0) && (row < NUM_ROWS));

                  //assert((col >= 0) && (col < NUM_COLS));

                  string piece = look(row, col);

                  assert(piece.at(1) == 'P');

                  //cout << "Advancing " << piece << " from position ["

                  //             << row << "," << col <O< "]" << endl;

                  /*if (piece.at(1) != 'P') {

     cout << "Not a pawn! [" << piece.at(1) << "]" << endl;

     return;

     }*/

                  if(!((piece.at(0)=='B' && direction[0] == 'S') || (piece.at(0)=='W' && direction[0] == 'N'))){

                      cout<<piece.at(0)<<" Pawn can not move "<<direction<<"!"<<endl;

                      return false;

                  }

                  if(steps>1){

                      cout<<"Pawn cannot move more than 1 step!"<<endl;

                      return false;

                  }

                  if (piece.at(0) == 'B'){

                      if(movePieceOneStep(piece, "S", row, col))

                          return true;

                      else

                          return false;

    }

   

    else if (piece.at(0) == 'W'){

        if(movePieceOneStep(piece, "N", row, col))

            return true;

        else

            return false;

    }

    else{

        return false;

    }

                  /*if (piece.at(0) == 'B')

     newRow = row + 1;

     else

     newRow = row - 1;

     if (!cellEmpty(newRow, col)) {

     cout << "Space [" << newRow << ", " << col

     << "] is not empty! Contains [" << piece << "]" << endl;

     return;

     }

     piece = take(row, col);

     place(newRow, col, piece.at(0), piece.at(1));

     row = newRow;*/

   

}

bool Board::moveQueen(int row, int col, string direction, int steps){

    string piece = look(row, col);

    assert(piece.at(1) == 'Q');

    //no if statements for directions, since queen can move anywhere

    for(int x = 0; x<steps; x++){

        if(!movePieceOneStep(piece, direction, row, col))

            return false;

    }

    return true;

}

bool Board::moveBishop(int row, int col, string direction, int steps){

    string piece = look(row, col);

    assert(piece.at(1) == 'B');

    if(direction.size()<2){

        //output error

        cout << "Bishops can not move " << direction << "!" << endl;

        return false;

    }

    for(int x = 0; x<steps; x++){

        if(!movePieceOneStep(piece, direction, row, col))

            return false;

    }

    return true;

}

bool Board::moveKing(int row, int col, string direction, int steps){

    string piece = look(row, col);

    assert(piece.at(1) == 'K');

    //no if statements for directions, since king can move anywhere

    if(steps>1){

                      cout<<"King cannot move more than 1 step!"<<endl;

                      return false;

       

    }

    if(!movePieceOneStep(piece, direction, row, col))

        return false;

    return true;

}

bool Board::moveKnight(int row, int col, string direction, int steps){

    string piece = look(row, col);

    assert(piece.at(1) == 'N');

    int torow = row, tocol = col;

    if(steps!=1){

        cout<<"Knight always moves 3 steps"<<endl;

        return false;

    }

    if(direction.size()!=3){

        cout << "Knight can not move " << direction << "!" << endl;

        return false;

    }

    if(direction[0] == 'N'){

        torow -=1;

    }

    else if(direction[0] == 'S'){

        torow +=1;

    }

    else if(direction[0] == 'E'){

        tocol +=1;

    }

    else if(direction[0] == 'W'){

        tocol -=1;

    }

    else{

        cout<<"Knight can not move " << direction << "!" << endl;

   }

   

    if(direction[1] == 'N'){

        torow -=1;

    }

    else if(direction[1] == 'S'){

        torow +=1;

    }

    else if(direction[1] == 'E'){

        tocol +=1;

    }

    else if(direction[1] == 'W'){

        tocol -=1;

    }

    else{

        cout<<"Knight can not move " << direction << "!" << endl;

    }

   

    if(direction[2] == 'N'){

        torow -=1;

    }

    else if(direction[2] == 'S'){

        torow +=1;

    }

    else if(direction[2] == 'E'){

        tocol +=1;

    }

    else if(direction[2] == 'W'){

        tocol -=1;

    }

    else{

        cout<<"Knight can not move " << direction << "!" << endl;

    }

    /*

     if(direction[0] == 'N'){

     torow -= 2;

     if(direction[1] == 'W'){

     tocol+=1;

     }

     else if(direction[1] == 'E'){

     tocol-=1;

     }

     else{

     //error message

     cout<<"Knight cannot move "<<direction<<endl;

     return false;

     }

     }

     else if(direction[0] == 'S'){

     torow += 2;

     if(direction[1] == 'W'){

     tocol+=1;

     }

     else if(direction[1] == 'E'){

     tocol-=1;

     }

     else{

     //error message

     cout<<"Knight cannot move "<<direction<<endl;

     return false;

     }

     }

     else if(direction[0] == 'W'){

     tocol+=2;

     if(direction[1] == 'N'){

     torow -=1;

     }

     else if(direction[1] == 'S'){

     torow += 1;

     }

     else{

     //error message

     cout<<"Knight cannot move "<<direction<<endl;

     return false;

     }

     }

     else if(direction[0] == 'E'){

     tocol-=2;

     if(direction[1] == 'N'){

     torow -=1;

     }

     else if(direction[1] == 'S'){

     torow += 1;

     }

     else{

     //error message

     cout<<"Knight cannot move "<<direction<<endl;

     return false;

     }

     }

     else*/

    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;

    }

    return true;

}

C++

Draw a flow chart or create a pseudo code design for each of the program modifications below. You can earn UP TO 5 Points each. Make sure that your document has enough detail to allow someone else to code from the design. 1. Starting with white, alternate between players, prompting for moves and allowing re-do moves when they enter an invalid move. 2. Design how the program will work when a piece is captured. 3. Design how to check if the King is in check. 4. Assuming you already know how to check if the King is in check, design how to check for checkmate.

Explanation / Answer

The pseudocode and flowchart views are fully synchronized so that any changes made in one view are automatically reflected in the other. In Code Rocket Designer, skeleton code is being automatically produced in a variety of languages as designs are manipulated.

In Code Rocket for Visual Studio and Code Rocket for Eclipse, designs are automatically reverse engineered as you move between methods in the code. The pseudocode and flowchart design views are integrated as standard Visual Studio tool windows and Eclipse views. If you make changes to the design of a method, you can commit them back into the code where the new statements will be merged with the existing code. Skeleton code is produced based on the design and is there to be implemented when the developer is ready. When code has been implemented, it is highlighted green in the flowchart and can be indicated in the pseudocode margin.

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