improve dfs

This commit is contained in:
quenousimporte 2025-04-23 12:27:06 +01:00
parent becd1b6f60
commit eeb45106ee
1 changed files with 12 additions and 11 deletions

25
w/w.c
View File

@ -440,18 +440,22 @@ void dfs(int x, int y)
setatpos(x, y, TILE_ROOM); setatpos(x, y, TILE_ROOM);
} }
bool nok = false; int directions[4] = { NORTH, EAST, SOUTH, WEST };
bool eok = false; // Shuffle the directions array
bool sok = false; for (int i = 3; i > 0; i--)
bool wok = false;
while (! (nok && eok && sok && wok))
{ {
char orientation = rand() % 4; int j = rand() % (i + 1);
switch (orientation) int temp = directions[i];
directions[i] = directions[j];
directions[j] = temp;
}
// todo refactor
for (int i = 0; i < 4; i++)
{
switch (directions[i])
{ {
case NORTH: case NORTH:
nok = true;
if (charatpos(x, y - 2) == TILE_INIT) if (charatpos(x, y - 2) == TILE_INIT)
{ {
setatpos(x, y - 1, dice() == 6 ? TILE_DOOR : TILE_FREE); setatpos(x, y - 1, dice() == 6 ? TILE_DOOR : TILE_FREE);
@ -459,7 +463,6 @@ void dfs(int x, int y)
} }
break; break;
case SOUTH: case SOUTH:
sok = true;
if (charatpos(x, y + 2) == TILE_INIT) if (charatpos(x, y + 2) == TILE_INIT)
{ {
setatpos(x, y + 1, dice() == 6 ? TILE_DOOR : TILE_FREE); setatpos(x, y + 1, dice() == 6 ? TILE_DOOR : TILE_FREE);
@ -467,7 +470,6 @@ void dfs(int x, int y)
} }
break; break;
case EAST: case EAST:
eok = true;
if (charatpos(x - 2, y) == TILE_INIT) if (charatpos(x - 2, y) == TILE_INIT)
{ {
setatpos(x - 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE); setatpos(x - 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE);
@ -475,7 +477,6 @@ void dfs(int x, int y)
} }
break; break;
case WEST: case WEST:
wok = true;
if (charatpos(x + 2, y) == TILE_INIT) if (charatpos(x + 2, y) == TILE_INIT)
{ {
setatpos(x + 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE); setatpos(x + 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE);