From eeb45106ee97efe32e8c5d6d78c3c7fdc049712b Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Wed, 23 Apr 2025 12:27:06 +0100 Subject: [PATCH] improve dfs --- w/w.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/w/w.c b/w/w.c index f2b7d7e..7b5ae89 100644 --- a/w/w.c +++ b/w/w.c @@ -440,18 +440,22 @@ void dfs(int x, int y) setatpos(x, y, TILE_ROOM); } - bool nok = false; - bool eok = false; - bool sok = false; - bool wok = false; + int directions[4] = { NORTH, EAST, SOUTH, WEST }; + // Shuffle the directions array + for (int i = 3; i > 0; i--) + { + int j = rand() % (i + 1); + int temp = directions[i]; + directions[i] = directions[j]; + directions[j] = temp; + } - while (! (nok && eok && sok && wok)) + // todo refactor + for (int i = 0; i < 4; i++) { - char orientation = rand() % 4; - switch (orientation) + switch (directions[i]) { case NORTH: - nok = true; if (charatpos(x, y - 2) == TILE_INIT) { setatpos(x, y - 1, dice() == 6 ? TILE_DOOR : TILE_FREE); @@ -459,7 +463,6 @@ void dfs(int x, int y) } break; case SOUTH: - sok = true; if (charatpos(x, y + 2) == TILE_INIT) { setatpos(x, y + 1, dice() == 6 ? TILE_DOOR : TILE_FREE); @@ -467,7 +470,6 @@ void dfs(int x, int y) } break; case EAST: - eok = true; if (charatpos(x - 2, y) == TILE_INIT) { setatpos(x - 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE); @@ -475,7 +477,6 @@ void dfs(int x, int y) } break; case WEST: - wok = true; if (charatpos(x + 2, y) == TILE_INIT) { setatpos(x + 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE);