Using rand(), create a 20 by 20 block that prints "[#]" for solid and "[ ]" for open.

Had a homework question a few weeks back that I never got to finish because I couldn't for the life of me figure out how to do it. Now that it is past due, I was hoping I could get some help to see the final product. Here were the requirements:

Create a program that prints a 20 by 20 block that represent a dungeon / labyrinth in a roleplaying game. It should print a "[#]" for a solid space within the dungeon and an empty space between braces "[ ]" for an open space. Modify your assignment, adding the features below:

The application should run -- producing maps one at a time until the user tells it to stop.

The size of the map should now be flexible and determined by user entry. Either dimension of the map can range from a size of 5 to a size of 50. If the user enters numbers outside of those ranges, they should be asked to re-enter until such a time as they enter a value that is in the correct range.

Include an option to generate a random map -- it will be of random size and the ratio of open spaces to closed spaces will also be randomly determined. The size of the map will be within the ranges listed above (5 to 50), and the random ratio of open spaces to filled spaces will be within the range of the first assignment (ii.e. 1:1, 1:2, 1:3).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <cstdlib>
using namespace std;


int main() {

	int i;
	string cont = "Yes";
	string userAnswer = "";

	while (cont == "Yes") {
		cout << "Would you like a random map or non random map?" << endl <<
			"Enter rand for random or nonrand for non random (Case sensitive): ";
		if (userAnswer == "rand") {
			for(int x = 0; )
		}
		else {
			cout << "Please enter desired map size between 5 and 50: ";
			cin >> i;

			while (i < 5 || i > 50) {
				cout << "Error! Please enter a number between 5 and 50: ";
				cin >> i;
			}

			for (int x = 0; x < i; x++) {
				for (int y = 0; y < i; y++) {
					if (rand() % 2) {
						cout << "[#]";
					}
					else {
						cout << "[ ]";
					}
				}
			}
		}
		cout << "Do you wish to continue? Enter Yes (case sensitive) to do so: ";
		cin >> cont;
		continue;
	}
	}
Last edited on
Please edit your with code formatting by adding "[code]" and "[/code]" around your code.

If you want to make a particular 1:integer, open:closed ratio with rand(), e.g. 1:N, this means that given (1+N) spots, 1 will be open, and N will be filled.
In other words, you want a 1 / (N+1) chance of a spot being open
rand() % (N+1) == 0 would mean open.
rand() % (N+1) != 0 would mean filled.

e.g. ratio = 1:3
rand() % 4 can be {0, 1, 2, 3}. So that's a 1/4 chance of it being 0, 3/4 chance of it being non-zero -- ratio of chances 1:3.
Last edited on
Topic archived. No new replies allowed.