From 27ece09577e6dfc27c0edfd1540ff2713a69e84b Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Wed, 23 Apr 2025 13:29:59 +0100 Subject: [PATCH] refactor --- w/w.c | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/w/w.c b/w/w.c index 7b5ae89..ec974ed 100644 --- a/w/w.c +++ b/w/w.c @@ -440,8 +440,8 @@ void dfs(int x, int y) setatpos(x, y, TILE_ROOM); } - int directions[4] = { NORTH, EAST, SOUTH, WEST }; // Shuffle the directions array + int directions[4] = { NORTH, EAST, SOUTH, WEST }; for (int i = 3; i > 0; i--) { int j = rand() % (i + 1); @@ -450,40 +450,35 @@ void dfs(int x, int y) directions[j] = temp; } - // todo refactor for (int i = 0; i < 4; i++) { + Position close = { x, y, 0 }; + Position far = { x, y, 0 }; switch (directions[i]) { case NORTH: - if (charatpos(x, y - 2) == TILE_INIT) - { - setatpos(x, y - 1, dice() == 6 ? TILE_DOOR : TILE_FREE); - dfs(x, y - 2); - } + close.y -= 1; + far.y -= 2; break; case SOUTH: - if (charatpos(x, y + 2) == TILE_INIT) - { - setatpos(x, y + 1, dice() == 6 ? TILE_DOOR : TILE_FREE); - dfs(x, y + 2); - } + close.y += 1; + far.y += 2; break; case EAST: - if (charatpos(x - 2, y) == TILE_INIT) - { - setatpos(x - 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE); - dfs(x - 2, y); - } + close.x -= 1; + far.x -= 2; break; case WEST: - if (charatpos(x + 2, y) == TILE_INIT) - { - setatpos(x + 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE); - dfs(x + 2, y); - } + close.x += 1; + far.x += 2; break; } + + if (charatpos(far.x, far.y) == TILE_INIT) + { + setatpos(close.x, close.y, dice() == 6 ? TILE_DOOR : TILE_FREE); + dfs(far.x, far.y); + } } }