4.12 Lab 5 : Visualizing Wumpus World .12 Lab 5: Visualizing Wumpus World lnthis
ID: 3673678 • Letter: 4
Question
4.12 Lab 5 : Visualizing Wumpus World .12 Lab 5: Visualizing Wumpus World lnthis lab you are going to expire vsualang agrd-based Wr pus world using ASC art. We wil buld this program incrementaly 1.) You need to take as input from the user the number of rows and columns, in that order. You also need to check i the inputs are within the accepted range of 2 to 9 inclusive. If the row size is invalid, output an error message:"Row size is legail" and similarly for the column size. For valid sizes, you print the grid. For example, where the user enters 3 and 5: Another example, where the user enter 1 and 3: Row size is illegalt Each grid element consists of: for the horizontal (row) ° .I. for the vertical (column) 2)You need to add the user into the grid. We wa use "-torepresent the user. The user's starting location should be random. You wil output this location in the form "User location: 0D",where i is the row and j is the column. When you display the grid, you should outputin the user's location. In this lab, we will use a fixed seed value of 13 for the random generator. For example, where the user enters 2 and 4 User locations (0, 1) 3.) You are probably wondering what in the world is a wumpus. A wumpus is a monster that will devour the poor userl Of course, at the moment, Wumpus World does not actually contain a wumpus. So let's changa 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: .D. For output, use to represent the wumpus" location. For example, where the user enters 6 and 3 User location: (o, 2) Wumpus location: (3. 1)Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
int main() {
int rows, cols;
bool isValidInput = false;
while (!isValidInput) {
cout << "Enter number of rows and number of columns (both in range 2 - 9 inclusive): " << endl;
cin >> rows >> cols;
if (rows < 2 || rows > 9) {
cout << "Row size is illegal!" << endl;
} else isValidInput = true;
if (cols < 2 || cols > 9) {
cout << "Columns size is illegal!" << endl;
} else isValidInput &= true;
}
srand(13);
int userRow, userCol;
int wupusRow, wupusCol;
int pitRow, pitCol;
userRow = rand() % rows;
userCol = rand() % cols;
do {
wupusRow = rand() % rows;
wupusCol = rand() % cols;
} while (wupusRow == userRow && wupusCol == userCol);
do {
pitRow = rand() % rows;
pitCol = rand() % cols;
} while ((pitRow == userRow && pitCol == userCol) || (pitRow == wupusRow && pitCol == wupusCol));
cout << "User location: (" << userRow << ", " << userCol << ") ";
cout << "Wupus location: (" << wupusRow << ", " << wupusCol << ") ";
cout << "Pit location: (" << pitRow << ", " << pitCol << ") ";
int rowCount = 0;
for (int i = 0; i < 2 * rows + 1; ++i) {
// print |
if (i & 1) {
cout << "|";
for (int j = 0; j < cols; ++j) {
if (rowCount == userRow && j == userCol) {
cout << " * |";
} else if (rowCount == wupusRow && j == wupusCol) {
cout << " W |";
} else if (rowCount == pitRow && j == pitCol) {
cout << " o |";
} else cout << " |";
}
cout << endl;
++rowCount;
} else {
// print ---
cout << ' ';
for (int j = 0; j < cols; ++j) {
cout << "--- ";
}
cout << endl;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.