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

IN C++ Modify the program to include “walls”, which will appear as \'W\' in the

ID: 3690300 • Letter: I

Question

IN C++

Modify the program to include “walls”, which will appear as 'W' in the grid. If you try to move into a wall, you will not go anywhere. You may change where the 'X' (your character) starts, and make a maze that leads to the finish (the 'O').

grid.cpp

#include <iostream>

#include <string>

using namespace std;

const int SIZE = 10;

void print(char line[SIZE][SIZE])

{

// first, clear the screen

cout << " ";

for(int i=0; i < SIZE; i++)

{

for(int j=0; j < SIZE; j++)

{

cout << line[i][j];

}

cout << endl;

}

cout << endl;

}

// row and col will be set with the position of the character c

void find(char line[SIZE][SIZE], char c, int& row, int& col)

{

for(int i=0; i < SIZE; i++)

{

for(int j=0; j < SIZE; j++)

{

if(line[i][j] == c){

row = i;

col = j;

return;

}

}

}

  

// if we get here, we didn't find the character

row=-1;

col=-1;

}

void move(char line[SIZE][SIZE], char a)

{

int row, col;

find(line, 'X', row, col); // get the initial position of the X

if(a == 'l' && col > 0)

{

line[row][col] = '-';

line[row][col-1] = 'X';

}

else if(a == 'r' && col < SIZE-1)

{

line[row][col] = '-';

line[row][col+1] = 'X';

}

else if(a == 'u' && row > 0)

{

line[row][col] = '-';

line[row-1][col] = 'X';

}

else if(a == 'd' && row < SIZE-1)

{

line[row][col] = '-';

line[row+1][col] = 'X';

}

}

int main()

{

char line[SIZE][SIZE]; // declare gridWorld

// initialize gridWorld to all blanks

for(int i=0; i < SIZE; i++)

{

for(int j=0; j < SIZE; j++)

{

line[i][j] = '-';

}

}

// put the robot in the middle

line[SIZE/2][SIZE/2] = 'X';

// put the goal in the top left corner

line[0][0] = 'O';

  

// print gridWorld

print(line);

char action;

while(true)

{

cout << "Do you want to move left, right, up or down? (l / r / u / d) ";

cin >> action; // get an action to perform

  

move(line, action); // move the robot position

print(line); // print the new gridWorld

int row, col;

find(line, 'O', row, col); // find the position of the goal

// if the goal is not found, it means the robot is there

if(row == -1 && col == -1)

{

cout << "You win! ";

return 0;

}

}

}

In addition to the changes above, you must modify gridWorld.cpp to read in the initial grid positions (i.e., what spaces are'-, 'X', 'O', or 'W") from a text file. An example file (grid.txt) is provided, which corresponds to the example below. Example grid.txt: WWWwWWWWw - WWWwWWWWw Example 1 (user input is underlined): WWWwWWWWw - WWWwWWWWw Do you vant to move left, right, up or down ( Do you want to move left, right, up or down? (1 / r / u /u d) u d)

Explanation / Answer

#include <iostream>

#include <string>

using namespace std;

const int SIZE = 10;

void print(char line[SIZE][SIZE])

{

// first, clear the screen

cout << " ";

for(int i=0; i < SIZE; i++)

{

for(int j=0; j < SIZE; j++)

{

cout << line[i][j];

}

cout << endl;

}

cout << endl;

}

// row and col will be set with the position of the character c

void find(char line[SIZE][SIZE], char c, int& row, int& col)

{

for(int i=0; i < SIZE; i++)

{

for(int j=0; j < SIZE; j++)

{

if(line[i][j] == c){

row = i;

col = j;

return;

}

}

}

  

// if we get here, we didn't find the character

row=-1;

col=-1;

}

void move(char line[SIZE][SIZE], char a)

{

int row, col;

find(line, 'X', row, col); // get the initial position of the X

if(a == 'l' && col > 0)

{

line[row][col] = '-';

line[row][col-1] = 'X';

}

else if(a == 'r' && col < SIZE-1)

{

line[row][col] = '-';

line[row][col+1] = 'X';

}

else if(a == 'u' && row > 0)

{

line[row][col] = '-';

line[row-1][col] = 'X';

}

else if(a == 'd' && row < SIZE-1)

{

line[row][col] = '-';

line[row+1][col] = 'X';

}

}

int main()

{

char line[SIZE][SIZE]; // declare gridWorld

// initialize gridWorld to all blanks

for(int i=0; i < SIZE; i++)

{

for(int j=0; j < SIZE; j++)

{

line[i][j] = '-';

}

}

// put the robot in the middle

line[SIZE/2][SIZE/2] = 'X';

// put the goal in the top left corner

line[0][0] = 'O';

  

// print gridWorld

print(line);

char action;

while(true)

{

cout << "Do you want to move left, right, up or down? (l / r / u / d) ";

cin >> action; // get an action to perform

  

move(line, action); // move the robot position

print(line); // print the new gridWorld

int row, col;

find(line, 'O', row, col); // find the position of the goal

// if the goal is not found, it means the robot is there

if(row == -1 && col == -1)

{

cout << "You win! ";

return 0;

}

}

}