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);
}
bool nok = false;
bool eok = false;
bool sok = false;
bool wok = false;
while (! (nok && eok && sok && wok))
int directions[4] = { NORTH, EAST, SOUTH, WEST };
// Shuffle the directions array
for (int i = 3; i > 0; i--)
{
char orientation = rand() % 4;
switch (orientation)
int j = rand() % (i + 1);
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:
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);