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

In C++ . Anyone can help me with this problem. I already have the code but the p

ID: 3690684 • Letter: I

Question

In C++ . Anyone can help me with this problem. I already have the code but the problem is to modify the old code to have the Wall 'W'

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'). 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.

This is the code need to modify

Explanation / Answer

#include <iostream>
#include <string>
#include <stdio>


const int SIZE = 10;
void move(char line[SIZE][SIZE], char a);
void find(char line[SIZE][SIZE], char c, int &row, int &col);
void print(char line[SIZE][SIZE]);

int main()
{
   char line[SIZE][SIZE];
   FILE *ptr_file;
   char buf[10];
   int fr=0,fc=0;
   int i=0;

       ptr_file =fopen("grid.txt","r");
       if (!ptr_file)
           return 1;

       while (fgets(buf,10, ptr_file)!=NULL)
       {
       for(int j=0; j < SIZE; j++)
       {
           line[i][j] = buf[j];
       }
       i++;
   }


   fclose(ptr_file);

   int row, col;
   find(line, 'O', row, col);
   fr=row;
   fc=col;

   print(line);

   char action;
   while(true)
{
cout << "Do you want to move left, right, up or down? (l / r / u / d) ";
cin >> action;
  
move(line, action);
print(line);
row=0;
       col=0;
find(line, 'X', row, col);
if(row == fr && col ==    fc)
{
cout << "You win! ";
return 0;
}
  
}
}

void print(char line[SIZE][SIZE])
{
  
cout << " ";
for(int i=0; i < SIZE; i++)
{
for(int j=0; j < SIZE; j++)
{
cout << line[i][j];
}
cout << endl;
}
cout << endl;
}
void move(char line[SIZE][SIZE], char a)
{
int row, col;
find(line, 'X', row, col);
   if(a == 'l' && col > 0 && line[row][col-1] != 'W' )
{
line[row][col] = '-';
       line[row][col-1] = 'X';
}
   else if (a == 'r' && col < SIZE-1 && line[row][col+1] != 'W')
{
line[row][col] = '-';
       line[row][col+1] = 'X';
}
   else if (a == 'u' && row > 0 && line[row-1][col] != 'W')
{
line[row][col] = '-';
line[row-1][col] = 'X';
}
   else if (a == 'd' && row < SIZE-1 && line[row+1][col] != 'W')
{
line[row][col] = '-';
       line[row+1][col] = 'X';
}
}

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;
}
}
}
  
row=-1;
col=-1;
  
}