### Strange random dungeon generation wrapping

I wrote a very simple dungeon generator today. I am having a strange issue where if a room goes too high or low, it wraps to the bottom or top. I have no idea at all why this would happen, as I error check to make sure its not too high/low, and the result is out of the bounds of the array. Also, to make things even weirder, it only happens vertically. Any thoughts?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134`` ``````using namespace std; #include #include #include //Constants const int cwidth = 140; const int cheight = 70; //Enumerations enum DIRECTION { D_NULL = -1, LEFT = 0, UP, RIGHT, DOWN }; //Map Array int map[cwidth][cheight]; //The file fstream map_file; //Functions int random (int i); void fill (int x, int y, int w, int h, int i); void show (void); bool collision (int x, int y, int w, int h); void randomize_map (int room_size_min, int room_size_max, int room_count, int hallway_max); int main(int argc, char **argv) { srand((unsigned)time(0)); start: randomize_map(2, 20, 40, 5); show(); int test; cin >> test; goto start; } int random (int n) { if (n == 0) return 0; return (rand() % n) + 1; } void fill (int x, int y, int w, int h, int i) { for (int dy = y; dy < h + y; dy++) { for (int dx = x; dx < w + x; dx++) { map[dx][dy] = i; } } } void show (void) { for (int y = 0; y < cheight; y++) { for (int x = 0; x < cwidth; x++) { cout << map[x][y]; } cout << '\n'; } } bool collision (int x, int y, int w, int h) { for (int dy = y - 1; dy <= h + y + 1; dy++) { for (int dx = x - 1; dx <= w + x + 1; dx++) { if (map[dx][dy] != 0) return true; } } return false; } void randomize_map (int room_size_min, int room_size_max, int room_count, int hallway_max) { ////Create the basic map fill(0, 0, cwidth, cheight, 0); //Create the first room fill(((cwidth / 2) - (room_size_max / 2)), ((cheight / 2) - (room_size_max / 2)), room_size_max, room_size_max, 1); unsigned int q = 0; for (int i = 1; i < room_count; i) { DIRECTION direction; bool loop = true; direction = D_NULL; int rw = room_size_min + random(room_size_max - room_size_min); if (rw < 2) rw = 2; int rh = room_size_min + random(room_size_max - room_size_min); if (rh < 2) rh = 2; int rx = random(cwidth); int ry = random(cheight); int offset_x = random(rw - 1); int offset_y = random(rh - 1); int hallway = random(hallway_max); if (map[rx][ry] == 1) { if (map[rx - 1][ry] == 0) direction = LEFT; else if (map[rx][ry - 1] == 0) direction = UP; else if (map[rx + 1][ry] == 0) direction = RIGHT; else if (map[rx][ry + 1] == 0) direction = DOWN;``````

I think this is where the problem occurs:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` `````` switch (direction) { case LEFT: if (rx - hallway - rw <= 0 || ry - offset_y <= 0) break; if (collision(rx - hallway - rw, ry - offset_y, rw, rh)) break; fill(rx - hallway - rw, ry - offset_y, rw, rh, 1); fill(rx - hallway, ry, hallway, 1, 1); i++; break; case UP: if (rx - offset_x <= 0 || ry - hallway - rh <= 0) break; if (collision(rx - offset_x, ry - hallway - rh, rw, rh)) break; fill(rx - offset_x, ry - hallway - rh, rw, rh, 1); fill(rx, ry - hallway, 1, hallway, 1); i++; break; case RIGHT: if (rx + hallway + rw >= cwidth || ry + offset_y >= cheight) break; if (collision(rx + hallway, ry - offset_y, rw, rh)) break; fill(rx + hallway, ry - offset_y, rw, rh, 1); fill(rx, ry, hallway, 1, 1); i++; break; case DOWN: if (rx + offset_x >= cwidth || ry + hallway + rh >= cheight) break; if (collision(rx - offset_x, ry + hallway, rw, rh)) break; fill(rx - offset_x, ry + hallway, rw, rh, 1); fill(rx, ry, 1, hallway, 1); i++; break; } } q++; if (q > 1000000000) i = room_count; } }``````
Last edited on
Are you sure your output window is at least 140 characters wide?
Yes, my problem is that the array is having a block of 1's filled from the bottom to the top, having a wrap effect. I am guessing it has something to do with the compiler handling out of bounds errors with my map array.
Topic archived. No new replies allowed.