How can we create a visual representation of Wumpus World?

  • MHB
  • Thread starter needOfHelpCMath
  • Start date
In summary, ";for (i = 0; i < numRows; i++){ cout << " ";for (i = 0; i < numCols; i++){ cout << "* ";cout << i << " ";}cout << endl;}return 0;}In summary, you will need to prompt the user for input, add them into the grid, and generate a random location for the wumpus. Finally, you will create a pit and output its location.
  • #1
needOfHelpCMath
72
0
4.12 Lab 5 : Visualizing Wumpus World
In this lab you are going to explore visualizing a grid-based Wumpus World using ASCII art. We will build this program incrementally.

1.) You need to take as input from the user the number of rows and columns, in that order. You also need to check if 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 illegal!" and similarly for the column size. For valid sizes, you print the grid.

For example, where the user enters 3 and 5:
Code:
 --- --- --- --- ---
|   |   |   |   |   |
 --- --- --- --- ---
|   |   |   |   |   |
 --- --- --- --- ---
|   |   |   |   |   |
 --- --- --- --- ---
Another example, where the user enter 1 and 3:
Code:
Row size is illegal!
Each grid element consists of :

'-' for the horizontal (row)
'|' for the vertical (column)
2.) 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)", where i is the row and j is the column. 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.

For example, where the user enters 2 and 4:
Code:
User location: (0, 1)
 --- --- --- ---
|   | * |   |   |
 --- --- --- ---
|   |   |   |   |
 --- --- --- ---
3.) 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.

For example, where the user enters 6 and 3:
Code:
User location: (0, 2)
Wumpus location: (3, 1)
 --- --- ---
|   |   | * |
 --- --- ---
|   |   |   |
 --- --- ---
|   |   |   |
 --- --- ---
|   | W |   |
 --- --- ---
|   |   |   |
 --- --- ---
|   |   |   |
 --- --- ---
4.) 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 4 and 5:
Code:
User location: (2, 1)
Wumpus location: (3, 1)
Pit location: (0, 4)
 --- --- --- --- ---
|   |   |   |   | o |
 --- --- --- --- ---
|   |   |   |   |   |
 --- --- --- --- ---
|   | * |   |   |   |
 --- --- --- --- ---
|   | W |   |   |   |
 --- --- --- --- ---
** MY CODE BELOW**

Code:
#include <iostream>
using namespace std;

int main ()
 
int numRows;
int numCols;

cin >> numRows;
cin >> numCols;

 if (numRows > 9)  {

	cout << "Row size is illegal!" << endl;
 }  
 
 if (numCols > 2) {
	cout << "Row size is illegal!" << endl;
 }
 
Technology news on Phys.org
  • #2
A few pointers:

1. Curly braces for the main function are missing.

2. You need to prompt the user for input. Include appropriate messages.

3. Your range tests for [m]numRows[/m] and [m]numCols[/m] are incomplete. Also, the message for [m]numCols[/m] mentions rows, not columns.

If you need help implementing any of this let us know.
 
  • #3
greg1313 said:
A few pointers:

1. Curly braces for the main function are missing.

2. You need to prompt the user for input. Include appropriate messages.

3. Your range tests for [m]numRows[/m] and [m]numCols[/m] are incomplete. Also, the message for [m]numCols[/m] mentions rows, not columns.

If you need help implementing any of this let us know.
This is so far I have gotten to my code and I am stuck on how to create the grid.
Code:
#include <iostream>
using namespace std;

int main(){
	int i; //This is the Row var
	int j; //This is the Column var
	
	if (i < 1 && j < 1 && i > 13 && j > 13){
	   cout << "Row size is illegal!"<< endl;
	}
	
	else {
   for (i=0;i==3;++i){
      for (i=0;i<5;++i){
      	cout << "---" << endl;
      }
      cout << "_";
           }
	}
	
	//Expected outputs
	cout << "User location: (" << i << "," << j << ")" << endl;
   cout << "Wumpus location: (" << /*rand var x*/" " << "," << /*rand var y*/" " << ")" << endl;
   cout << "Pit location: (" << 1 << "," << 1 <<")" << endl;
   return 0;
}
This is what I got after compile my code and run it :View attachment 5313View attachment 5314
 

Attachments

  • C++.jpg
    C++.jpg
    80 KB · Views: 170
  • C++2.jpg
    C++2.jpg
    78.1 KB · Views: 161
  • #4
The first thing you need to do is to input the number of rows and columns. This should ask the user for these numbers, and continue asking until the user enters valid numbers (or she gets tired and goes home). Here is such a loop that will result in numRows valid:
Code:
   bool valid = false;
   while (!valid) {
      cout << "Enter number of rows (2 to 9):";
      cin >> numRows;
      valid = numRows >= 2 && numRows <= 9;
      if (!valid) {
         cout << "Invalid number of rows!\n";
      }
   }

You could write a second loop to validly input the number of columns. However, the specification is that the user enter both numbers on the same line. So try and modify the above so that this is done. Hint: add additional bools validRow and validCol.

Next, there is no indication of a way to represent the grid. I would use a 2 dimensional array of strings, say board. Then board has 2*numRows+1 rows and numCols+1 columns. Each component of board is a 4 character string. Every even row of board has components " ---" (blank, 3 minuses) , and every odd row of board has components "| " (vertical bar followed by 3 blanks). You need to construct such a board; the printing of such a board should be straight forward.

Then to put the user into board, just replace the string "| " with the string "| * "; similarly for the wumpus and the pit.
 
Last edited:
  • #5
My code I have so far:
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(){
	int i; //This is the Row var
	int j; 
	
	  int Inrow, Incol;
    int row;
    int col;///This is the Column var
    int userRow;
    int userCol;
    
	 j = 2;
	 i = 0;
	//Expected outputs
	cout << "User location: (" << i << ", " << j << ")" << endl;
    cout << "Wumpus location: (" <<  rand()%1 << ", " <<  ((rand()%13)+1)  << ")" << endl;
    cout << "Pit location: (" << 1 << ", " << 1 <<")" << endl;
   
   
   
    cin >> Inrow;
    cin >> Incol;
    for  (row = 0; row < Inrow; row++){
      for (col = 0;  col < Incol; col++) {
       cout << "---";
       }
       cout << endl;
       for (col = 0;  col < Incol; col++) {
       cout << "|  ";
       }
       cout <<"|";
       cout <<endl;
       }
     for (col = 0;  col < Incol; col++) {
       cout << "---";
       }
    
    
   return 0; }
*** THIS IS MY TEST***

Input
3 3
Your output :

Code:
User location: (0, 2)
Wumpus location: (0, 1)
Pit location: (1, 1)
---------
|  |  |  |
---------
|  |  |  |
---------
|  |  |  |
---------

Expected output:

Code:
User location: (0, 2)
Wumpus location: (0, 1)
Pit location: (1, 1)
 --- --- ---
|   | W | * |
 --- --- ---
|   | o |   |
 --- --- ---
|   |   |   |
 --- --- ---
So far I know that I need to use whitespace to fix my spacing but I am stuck on how to get the '*', 'W' , and 'o'. My lab instructor told me to use while loops; help guide me.
 
  • #6
I've posted some working code below. Try to figure it out and (possibly) improve it.

Code:
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(){
	
	int rows, cols = 0;	
	int boardRows, boardCols;
	int userRowLocation, userColLocation, wumpusRowLocation, wumpusColLocation, pitRowLocation, pitColLocation, userRow, userCol, wumpusRow, wumpusCol, pitRow, pitCol;
	char board[19][37];

	// seed random number generator
	srand(13);

	// get number of rows
	while (!(rows > 1 && rows < 10)) {

		cout << "Enter number of rows (between 2 and 9 inclusive): ";
		cin >> rows;
	}

	// get number of columns
	while (!(cols > 1 && cols < 10)) {

		cout << "Enter number of columns (between 2 and 9 inclusive): ";
		cin >> cols;
	}

	// translate rows and cols to workable values
	boardRows = rows * 2 + 1;
	boardCols = cols * 4 + 1;

	// initialize board	
	for (rows = 0; rows < boardRows; rows++)
		for (cols = 0; cols < boardCols; cols++)
			if (!(rows % 2) && !(cols % 4))
				board[rows][cols] = ' ';
			else if (!(rows % 2) && cols % 4)
				board[rows][cols] = '-';
			     else if (rows % 2 && !(cols % 4))
					board[rows][cols] = '|';
				  else board[rows][cols] = ' ';

	// get user location
	int foundUserLocation = 0;
	while (!foundUserLocation) {

		userRowLocation = rand() % boardRows;
		userColLocation = rand() % boardCols;

		if (board[userRowLocation][userColLocation] == ' ' && userRowLocation % 2 != 0) {
			if (board[userRowLocation][userColLocation - 1] == '|')
				userColLocation = userColLocation + 1;
			else if (board[userRowLocation][userColLocation + 1] == '|')
				userColLocation = userColLocation - 1;

			board[userRowLocation][userColLocation] = '*';
			userRow = (userRowLocation - 1)/2;
			userCol = (userColLocation + 2)/4 - 1;
			foundUserLocation = 1;
		}
	}

	// get Wumpus location
	int foundWumpusLocation = 0;
	while (!foundWumpusLocation) {

		wumpusRowLocation = rand() % boardRows;
		wumpusColLocation = rand() % boardCols;

		if (board[wumpusRowLocation][wumpusColLocation] == ' ' && wumpusRowLocation % 2 != 0) {
			if (board[wumpusRowLocation][wumpusColLocation - 1] == '|')
				wumpusColLocation = wumpusColLocation + 1;
			else if (board[wumpusRowLocation][wumpusColLocation + 1] == '|')
				wumpusColLocation = wumpusColLocation - 1;

			wumpusRow = (wumpusRowLocation - 1)/2;
			wumpusCol = (wumpusColLocation + 2)/4 - 1;

			if (!(wumpusRow == userRow && wumpusCol == userCol)) {
				board[wumpusRowLocation][wumpusColLocation] = 'W';
				foundWumpusLocation = 1;
			}
		}
	}

	// get pit location
	int foundPitLocation = 0;
	while (!foundPitLocation) {

		pitRowLocation = rand() % boardRows;
		pitColLocation = rand() % boardCols;

		if (board[pitRowLocation][pitColLocation] == ' ' && pitRowLocation % 2 != 0) {
			if (board[pitRowLocation][pitColLocation - 1] == '|')
				pitColLocation = pitColLocation + 1;
			else if (board[pitRowLocation][pitColLocation + 1] == '|')
				pitColLocation = pitColLocation - 1;

			pitRow = (pitRowLocation - 1)/2;
			pitCol = (pitColLocation + 2)/4 - 1;

			if (!((pitRow == userRow && pitCol == userCol) || (pitRow == wumpusRow && pitCol == wumpusCol))) {
				board[pitRowLocation][pitColLocation] = 'o';
				foundPitLocation = 1;
			}
		}
	}

	// print location of user, Wumpus and pit
	cout << "User location: (" << userRow << ", " << userCol << ')' << endl;
	cout << "Wumpus location: (" << wumpusRow << ", " << wumpusCol << ')' << endl;
	cout << "Pit location: (" << pitRow << ", " << pitCol << ')' << endl;

	// print board
	for (rows = 0; rows < boardRows; rows++) {
		for (cols = 0; cols < boardCols; cols++)
			cout << board[rows][cols];
		cout << endl;
	}
}
 
Last edited:

FAQ: How can we create a visual representation of Wumpus World?

What is "Visualizing Wumpus World"?

"Visualizing Wumpus World" is a computer program designed to simulate the classic game "Hunt the Wumpus". It allows users to explore a 2D grid world, encountering hazards and searching for the elusive Wumpus creature.

How does "Visualizing Wumpus World" work?

The program uses algorithms and rules to generate a random grid world, where the user can move around and interact with different elements. The user can make decisions and take actions based on their surroundings, and the program will respond accordingly.

What are the main objectives of "Visualizing Wumpus World"?

The main objective of the game is to find and eliminate the Wumpus creature, while avoiding other hazards such as pits and super bats. The player must also collect gold and reach the designated exit point to win the game.

Is "Visualizing Wumpus World" educational?

Yes, "Visualizing Wumpus World" can be used as a learning tool for students to understand concepts like logic, problem-solving, and spatial reasoning. It can also be used to teach programming, as the program involves using algorithms and coding to control the game.

Can "Visualizing Wumpus World" be played by multiple players?

Yes, the game can be played by multiple players, either taking turns or simultaneously. This can add an element of competition and collaboration to the game, making it more engaging and interactive.

Similar threads

Replies
1
Views
6K
Replies
6
Views
3K
Replies
1
Views
1K
Replies
6
Views
1K
Replies
1
Views
3K
Replies
2
Views
3K
Replies
2
Views
2K
Back
Top