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

you are going to explore visualizing a grid-based Wumpus World using ASCII art.

ID: 672011 • Letter: Y

Question

you are going to explore visualizing a grid-based Wumpus World using ASCII art. in C++

1.) You need to take as input from the user the number of columns and rows, in that order. You also need to check if the inputs are within the accepted range of 2 to 9 inclusive. If the column size is invalid, output an error message: "Column size is illegal!" and similarly for the row size. For valid sizes, you then output the grid.

You need to add the user into the grid. We will use '*' to represent the user. The user's starting location should be random. You will output this location in the form "User location: (i, j)". When you display the grid, you should output '*' in the user's location. In this lab, we will use a fixed seed value of 13 for the random generator.

You are probably wondering what in the world is a wumpus. A wumpus is a monster that will devour the poor user! Of course, at the moment, Wumpus World does not actually contain a wumpus. So let's change that! Generate a random location on the grid for the wumpus. The user and the wumpus cannot start at the same location. You will output this location in the form "Wumpus location: (i, j)". For output, use 'W' to represent the wumpus' location.

The last part of Wumpus World is adding in a pit. Create a random starting location for the pit. Again, ensure that the pit, the wumpus and the user are not starting on the same spot. You will output this location in the form "Pit location: (i, j)". For output, use 'o' to represent the pit.

For example, where the user enters 5 and 4:

Explanation / Answer

//Please note I have intialized array with charater '-'. You can remove it If you want.

// Header Files

#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
using namespace std;

int main()
{

// These are the variables to store position of user, wumpus, pit.

int i,j,r,c,m,n,l,k;

cout<<"Enter number of rows and columns separated by space or new line";

cin>>r>>c;

// Logic to check rows be in given range
if((r<2 && r>9))
{
cout<<"Row Size is illegal";
return 0;
      
}

// Logic to check columns be in given range

if((c<2 && c>9))
{
cout<<"Column Size is illegal";
return 0;
      
}

// declaration of array
char a[r][c];

// intialization of array. you can remove it if you want
for(i=0;i<r;i++)
{
   for(j=0;j<c;j++)
   {
      
       a[i][j]='-';
      
   }
  
}

// this is used to seed the psuedo random generator with given value
srand (13);

// I have used "output = min + (rand() % (int)(max - min + 1))" to genarate number in particular range. Min is starting value and Max is highest value. So It will generate random numbers in [Min, Max] range.

//for example suppose user enter value of r=2 and c=5. so in array rows are in [0-1] and columns are in

// [0-4]. so min is 0 and max is 4. Thats why I use max= r-1 for rows and c-1 for columns.

i= 0 + (rand() % (int)((r-1) - 0 + 1));
j=0 + (rand() % (int)((c-1) - 0 + 1));
a[i][j]='*';

// similarly this is for wumpus location

m= 0 + (rand() % (int)((r-1) - 0 + 1));
n=0 + (rand() % (int)((c-1) - 0 + 1));

// this logic is checking whether location for wumpus should not match with user location
while(m==i && n==j)
{
m= 0 + (rand() % (int)((r-1) - 0 + 1));
n=0 + (rand() % (int)((c-1) - 0 + 1));

}
a[m][n]='W';

// this is calculating PIT Location

l= 0 + (rand() % (int)((r-1) - 0 + 1));
k=0 + (rand() % (int)((c-1) - 0 + 1));

// this logic is checking whether location for pit should not match with user location and Wumpus Location

while((l==i && k==j)||(l==m && k==n))
{
l= 0 + (rand() % (int)((r-1) - 0 + 1));
k=0 + (rand() % (int)((c-1) - 0 + 1));

}

a[l][k]='o';

// This is used to print the final grid.

for(i=0;i<r;i++)
{
   for(j=0;j<c;j++)
   {
      
       cout<<a[i][j];
      
   }
   cout<<endl;
  
}
return 0;
}