I am making a program to generate random dungeons. So far I have it where the program can fill the map with solid earth and then carve out a room at a random position on the map as the starting point.
This function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void buildDung(int low, int range)
{
/// Finds a suitable area to build a feature ///
int xCoord = randGen(2,67);
int yCoord = randGen(2,42);
while(map[xCoord][yCoord] != " ")
{
xCoord = randGen(2,67);
yCoord = randGen(2,42);
}
choose:
goto choose;
}
Is giving me a good amount of trouble. This function is supposed to the search spots on the map until it finds a spot where there is no earth. It seems like it would work, but it keeps crashing my program... I have no clue as to why. Any help would be great!
Suppose that this is the dungeon that my program made. The earth is made of * and the room is just spaces. If I have the while loop search for what character makes up the room (whether is be spaces, &, etc) it crashes. If the while loop search for something on the walls it won't. I don't understand.
That I could tell, but you'd need to post the function for us to see what's going on. Is it guaranteed to return only values in the range of map[][]? Is map[][] an array of strings? Can you post its declaration?
int randGen(int low, int range)
{
/// Generates random numbers in the range that is specified ///
return low+int(range*rand()/(RAND_MAX + 1.0));
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void firstRoom()
{
/// Carves out the first room of the dungeon ///
int down = randGen(10,21);
int left = randGen(10,21);
int width = randGen(3,6);
int length = randGen(3,6);
int a = x-down;
int aa = a+length;
int b = y-left;
int bb = b+width;
for (int xCoord = a; xCoord < aa; xCoord++) {
for (int yCoord = b; yCoord < bb; yCoord++) {
map[xCoord][yCoord] = " ";
}
}
}
Assuming that the definition of map is something like string **map; or string map[70][45];
make sure that the parts of the array exist. exactly how big is map?
OH MY GOD YES!! I FIXED IT!! Thank you for asking about the dimensions! You got me thinking and now I fixed it. I had the random numbers generating opposite what they should be.