MHB How can we create a visual representation of Wumpus World?

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
AI Thread Summary
The discussion focuses on creating a visual representation of Wumpus World using ASCII art, emphasizing the need for user input to define grid dimensions within a specified range. The program should randomly place a user, a Wumpus, and a pit on the grid, ensuring they do not occupy the same location. Participants are guided through coding challenges, including proper input validation, grid construction, and the representation of elements using specific characters. The conversation highlights the importance of using loops and arrays to manage the grid and the placement of characters effectively. Overall, the thread serves as a collaborative effort to troubleshoot and improve the implementation of the Wumpus World visualization.
needOfHelpCMath
Messages
70
Reaction score
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
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.
 
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: 192
  • C++2.jpg
    C++2.jpg
    78.1 KB · Views: 182
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:
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.
 
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top