Make a random generated map within 5 and 50

Oct 7, 2019 at 10:30pm
My goal is to give the user an option of choosing the map size, or choosing random map size. If chose random, I need to keep the map size between 5 and 50. I also need the ratio of open spaces "[ ]" and closed spaces "[X]" to be a random ratio within the range of 1:1, 1:2, or 1:3.Here is my code, the random map code is not finished but the user choice code is finished other than I need to figure out how to change the ratio of open to closed depending on user input.

#include <iostream>
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;
}
}
Oct 7, 2019 at 11:26pm
Please repost your code using code tags.
Oct 8, 2019 at 2:55pm
if (userAnswer == "rand") {
You never read userAnswer.

1
2
3
if (userAnswer == "rand") {
for(int x = 0; )
}

Line 2 should just set i to a random number (between 5 and 50?)

Then you should adjust your braces so that the code that prints the map runs after the if/else instead of inside the else part.

1
2
3
4
5
6
7
8
9
10
for (int x = 0; x < i; x++) {
for (int y = 0; y < i; y++) {
if (rand() % 2) {
cout << "[#]";
}
else {
cout << "[ ]";
}
}
}

You never print a newline, so that will all come out on a single line.

Indent your code to match the braces. Most IDEs will do this for you. You will find that the braces don't line up the way you want them to.
Oct 8, 2019 at 4:17pm
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include <cctype>  // tolower
#include <cstdlib> // srand/rand
#include <ctime>   // time

int main()
{
   // seed the random generator for different random number each run
   srand((unsigned) time(nullptr));

   std::string again { };

   do
   {
      std::string mapChoice { };
      std::cout << "Would you like a random map or non random map?\n";
      std::cout << "Enter rand for random or nonrand for non random: ";
      std::cin >> mapChoice;
      std::cout << '\n';

      int mapSize { };

      if (tolower(mapChoice[0]) == 'r')
      {
         mapSize = rand() % 46 + 5;
      }
      else
      {
         do
         {
            std::cout << "Please enter desired map size between 5 and 50: ";
            std::cin >> mapSize;

            if (mapSize < 5 || mapSize > 50)
            {
               std::cout << "Error! ";
               continue;
            }
         }
         while (mapSize < 5 || mapSize > 50);
         std::cout << '\n';
      }

      for (int row = 0; row < mapSize; row++)
      {
         for (int col = 0; col < mapSize; col++)
         {
            if (rand() % 2)
            {
               std::cout << "[#]";
            }
            else
            {
               std::cout << "[ ]";
            }
         }
         std::cout << '\n';
      }
      std::cout << '\n';

      std::cout << "Do you wish to continue? ";
      std::cin >> again;
      std::cout << '\n';
   }
   while (tolower(again[0]) == 'y');
}

Would you like a random map or non random map?
Enter rand for random or nonrand for non random: n

Please enter desired map size between 5 and 50: 4
Error! Please enter desired map size between 5 and 50: 80
Error! Please enter desired map size between 5 and 50: 6

[ ][ ][#][ ][ ][ ]
[ ][#][ ][ ][#][#]
[#][#][ ][#][#][#]
[#][ ][ ][ ][ ][ ]
[ ][ ][ ][#][ ][ ]
[ ][#][ ][ ][ ][#]

Do you wish to continue? y

Would you like a random map or non random map?
Enter rand for random or nonrand for non random: r

[#][ ][#][ ][#][#][#][#][#][#][ ][ ][ ][#][#]
[ ][ ][#][ ][#][ ][#][#][ ][ ][#][#][#][ ][ ]
[#][ ][#][ ][ ][ ][#][ ][#][ ][#][#][#][#][#]
[ ][#][#][#][#][ ][#][ ][ ][#][ ][#][ ][#][ ]
[#][#][ ][#][#][ ][ ][#][ ][ ][ ][ ][#][#][ ]
[#][#][ ][#][#][ ][ ][ ][#][#][#][#][ ][#][#]
[#][ ][ ][#][#][#][#][#][#][#][ ][ ][ ][ ][#]
[ ][#][#][#][ ][#][#][ ][ ][ ][ ][ ][#][ ][#]
[#][#][#][#][ ][ ][ ][ ][#][#][#][ ][ ][ ][ ]
[#][ ][ ][ ][ ][ ][ ][#][#][ ][#][ ][ ][#][ ]
[ ][ ][ ][#][ ][ ][ ][ ][ ][#][ ][#][ ][#][ ]
[ ][#][ ][#][#][#][ ][#][#][#][#][ ][#][ ][#]
[ ][#][#][#][#][#][ ][#][ ][ ][#][#][#][ ][ ]
[ ][ ][ ][#][#][ ][#][ ][#][#][ ][ ][#][#][ ]
[ ][#][ ][#][#][ ][#][#][ ][ ][#][#][ ][ ][ ]

Do you wish to continue? y

Would you like a random map or non random map?
Enter rand for random or nonrand for non random: r

[#][#][ ][ ][ ][ ][ ][ ][#][#]
[#][ ][ ][ ][#][#][ ][ ][#][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[#][#][#][#][#][#][#][#][ ][#]
[#][#][ ][#][#][ ][#][#][ ][#]
[ ][#][ ][#][#][ ][ ][ ][ ][ ]
[#][ ][ ][#][#][#][#][#][#][#]
[ ][#][ ][#][#][ ][#][ ][#][ ]
[ ][#][#][#][#][ ][#][ ][#][ ]
[ ][ ][#][ ][ ][#][ ][ ][ ][#]

Do you wish to continue? n


Why are you not storing the map you generate in a container? A 2D vector dynamically created with the variable size would work.
Last edited on Oct 8, 2019 at 4:38pm
Topic archived. No new replies allowed.