### Make a random generated map within 5 and 50

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";

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): ";
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;
}
}
`if (userAnswer == "rand") {`

 ``123`` ``````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.

 ``12345678910`` ``````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.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` ``````#include #include #include // tolower #include // srand/rand #include // 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
Topic archived. No new replies allowed.