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;
}
}
#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.