YouTube-simulations/sub_maze.c

804 lines
25 KiB
C
Raw Normal View History

2023-01-22 16:49:04 +01:00
/* The function init_maze has been improved and should return a maze with a solution */
2022-10-18 23:28:20 +02:00
/* The current algorithm uses a self-avoiding random walk. A better option may be */
/* to give random weights to the dual graph, and finite a maximal spanning tree */
/* Change constant RAND_SHIFT to change the maze */
typedef struct
{
short int nneighb; /* number of neighbours */
int neighb[MAZE_MAX_NGBH]; /* neighbour cells */
short int directions[MAZE_MAX_NGBH]; /* direction of neighbours */
short int north, east, south, west; /* closed walls */
2023-01-22 16:49:04 +01:00
short int northeast, northwest, southeast, southwest; /* closed walls */
2022-10-18 23:28:20 +02:00
short int active; /* takes value 1 if currently active in RW path */
short int tested; /* takes value 1 if tested */
2022-11-20 23:17:39 +01:00
short int connected; /* takes value 1 if connected to exit */
short int closed; /* takes value 1 if no untested neighbours */
2022-10-18 23:28:20 +02:00
} t_maze;
int nmaze(int i, int j)
{
return(NXMAZE*j + i);
}
void init_maze_graph(t_maze maze[NXMAZE*NYMAZE])
{
int i, j, k, n;
printf("Initializing maze\n");
/* initialize neighbours */
/* in the bulk */
for (i=1; i<NXMAZE-1; i++)
for (j=1; j<NYMAZE-1; j++)
{
n = nmaze(i, j);
maze[n].nneighb = 4;
maze[n].neighb[0] = nmaze(i, j+1);
maze[n].neighb[1] = nmaze(i+1, j);
maze[n].neighb[2] = nmaze(i, j-1);
maze[n].neighb[3] = nmaze(i-1, j);
for (k=0; k<4; k++) maze[n].directions[k] = k;
}
/* left side */
for (j=1; j<NYMAZE-1; j++)
{
n = nmaze(0, j);
maze[n].nneighb = 3;
maze[n].neighb[0] = nmaze(0, j+1);
maze[n].neighb[1] = nmaze(1, j);
maze[n].neighb[2] = nmaze(0, j-1);
for (k=0; k<3; k++) maze[n].directions[k] = k;
}
/* right side */
for (j=1; j<NYMAZE-1; j++)
{
n = nmaze(NXMAZE-1, j);
maze[n].nneighb = 3;
maze[n].neighb[0] = nmaze(NXMAZE-1, j+1);
maze[n].neighb[1] = nmaze(NXMAZE-2, j);
maze[n].neighb[2] = nmaze(NXMAZE-1, j-1);
maze[n].directions[0] = 0;
maze[n].directions[1] = 3;
maze[n].directions[2] = 2;
}
/* bottom side */
for (i=1; i<NXMAZE-1; i++)
{
n = nmaze(i, 0);
maze[n].nneighb = 3;
maze[n].neighb[0] = nmaze(i, 1);
maze[n].neighb[1] = nmaze(i+1, 0);
maze[n].neighb[2] = nmaze(i-1, 0);
maze[n].directions[0] = 0;
maze[n].directions[1] = 1;
maze[n].directions[2] = 3;
}
/* top side */
for (i=1; i<NXMAZE-1; i++)
{
n = nmaze(i, NYMAZE-1);
maze[n].nneighb = 3;
maze[n].neighb[0] = nmaze(i, NYMAZE-2);
maze[n].neighb[1] = nmaze(i+1, NYMAZE-1);
maze[n].neighb[2] = nmaze(i-1, NYMAZE-1);
maze[n].directions[0] = 2;
maze[n].directions[1] = 1;
maze[n].directions[2] = 3;
}
/* corners */
n = nmaze(0,0);
maze[n].nneighb = 2;
maze[n].neighb[0] = nmaze(1,0);
maze[n].neighb[1] = nmaze(0,1);
maze[n].directions[0] = 1;
maze[n].directions[1] = 0;
n = nmaze(NXMAZE-1,0);
maze[n].nneighb = 2;
maze[n].neighb[0] = nmaze(NXMAZE-2,0);
maze[n].neighb[1] = nmaze(NXMAZE-1,1);
maze[n].directions[0] = 3;
maze[n].directions[1] = 0;
n = nmaze(0,NYMAZE-1);
maze[n].nneighb = 2;
maze[n].neighb[0] = nmaze(1,NYMAZE-1);
maze[n].neighb[1] = nmaze(0,NYMAZE-2);
maze[n].directions[0] = 1;
maze[n].directions[1] = 2;
n = nmaze(NXMAZE-1,NYMAZE-1);
maze[n].nneighb = 2;
maze[n].neighb[0] = nmaze(NXMAZE-2,NYMAZE-1);
maze[n].neighb[1] = nmaze(NXMAZE-1,NYMAZE-2);
maze[n].directions[0] = 3;
maze[n].directions[1] = 2;
/* initialize other parameters */
for (i=0; i<NXMAZE; i++)
for (j=0; j<NYMAZE; j++)
{
n = nmaze(i, j);
maze[n].active = 0;
maze[n].tested = 0;
2022-11-20 23:17:39 +01:00
maze[n].connected = 0;
maze[n].closed = 0;
2022-10-18 23:28:20 +02:00
maze[n].north = 1;
maze[n].east = 1;
maze[n].south = 1;
maze[n].west = 1;
}
}
2023-01-22 16:49:04 +01:00
void init_circular_maze_graph(t_maze maze[NXMAZE*NYMAZE])
/* initialise graph of circular maze */
/* NXMAZE should be a power of 2, and NYMAZE a multiple of NXMAZE */
/* row number i represents the radial coordinate, and column number j the angular one */
/* the maze is split into square blocks of size NXMAZE times NXMAZE */
/* in each block, the first column has 1 cell, the second has 2 cells */
/* then there are 2 columns of 4 cells, 4 columns of 8 cells, etc */
{
int i, j, k, n, p, q, block, nblocks, width, nextblock, prevblock;
printf("Initializing maze\n");
if (MAZE_MAX_NGBH < 5)
{
printf("Error: MAZE_MAX_NGBH should be at least 5 for circular maze\n");
exit(0);
}
if (NYMAZE%NXMAZE != 0) printf("Warning: NYMAZE should be a multiple of NXMAZE\n");
/* set dummy variables for potentially unused cells */
for (i=0; i<NXMAZE*NYMAZE; i++) maze[i].nneighb = 0;
nblocks = NYMAZE/NXMAZE;
/* initialize neighbours */
for (block=0; block<nblocks; block++)
{
nextblock = (block+1)%nblocks;
prevblock = block-1; if (prevblock < 0) prevblock = nblocks-1;
/* first column */
j = block*NXMAZE;
n = nmaze(0,j);
maze[n].nneighb = 4;
maze[n].neighb[0] = nmaze(1, j);
maze[n].neighb[1] = nmaze(1, j+1);
maze[n].neighb[2] = nmaze(0, nextblock*NXMAZE);
maze[n].neighb[3] = nmaze(0, prevblock*NXMAZE);
maze[n].directions[0] = 1;
maze[n].directions[1] = 4;
maze[n].directions[2] = 0;
maze[n].directions[3] = 2;
/* second column */
for (q=0; q<2; q++)
{
j = block*NXMAZE + q;
n = nmaze(1,j);
maze[n].nneighb = 5;
maze[n].neighb[0] = nmaze(2, j + q);
maze[n].neighb[1] = nmaze(2, j + q + 1);
if (q == 1) maze[n].neighb[2] = nmaze(1, nextblock*NXMAZE);
else maze[n].neighb[2] = nmaze(1, j+1);
if (q == 0) maze[n].neighb[3] = nmaze(1, prevblock*NXMAZE + 1);
else maze[n].neighb[3] = nmaze(1, j-1);
maze[n].neighb[4] = nmaze(0, block*NXMAZE);
maze[n].directions[0] = 1;
maze[n].directions[1] = 4;
maze[n].directions[2] = 0;
maze[n].directions[3] = 2;
maze[n].directions[4] = 3;
}
/* other columns */
width = 2;
i = 2;
while (width < NXMAZE)
{
/* left column of block */
for (q = 0; q < 2*width; q++)
{
j = block*NXMAZE + q;
n = nmaze(i,j);
maze[n].nneighb = 4;
maze[n].neighb[0] = nmaze(i+1, j);
if (q == 2*width-1) maze[n].neighb[1] = nmaze(i, nextblock*NXMAZE);
else maze[n].neighb[1] = nmaze(i, j+1);
if (q == 0) maze[n].neighb[2] = nmaze(i, prevblock*NXMAZE + 2*width - 1);
else maze[n].neighb[2] = nmaze(i, j-1);
maze[n].neighb[3] = nmaze(i-1, block*NXMAZE + q/2);
maze[n].directions[0] = 1;
maze[n].directions[1] = 0;
maze[n].directions[2] = 2;
maze[n].directions[3] = 3;
}
/* middle columns of block */
for (p = 1; p < width-1; p++)
{
i++;
for (q = 0; q < 2*width; q++)
{
j = block*NXMAZE + q;
n = nmaze(i,j);
maze[n].nneighb = 4;
maze[n].neighb[0] = nmaze(i+1, j);
if (q == 2*width-1) maze[n].neighb[1] = nmaze(i, nextblock*NXMAZE);
else maze[n].neighb[1] = nmaze(i, j+1);
if (q == 0) maze[n].neighb[2] = nmaze(i, prevblock*NXMAZE + 2*width - 1);
else maze[n].neighb[2] = nmaze(i, j-1);
maze[n].neighb[3] = nmaze(i-1, j);
maze[n].directions[0] = 1;
maze[n].directions[1] = 0;
maze[n].directions[2] = 2;
maze[n].directions[3] = 3;
}
}
/* right column of block */
i++;
for (q = 0; q < 2*width; q++)
{
j = block*NXMAZE + q;
n = nmaze(i,j);
if (i<NXMAZE-1) maze[n].nneighb = 5;
else maze[n].nneighb = 3;
maze[n].neighb[0] = nmaze(i-1, j);
if (q == 2*width-1) maze[n].neighb[1] = nmaze(i, nextblock*NXMAZE);
else maze[n].neighb[1] = nmaze(i, j+1);
if (q == 0) maze[n].neighb[2] = nmaze(i, prevblock*NXMAZE + 2*width - 1);
else maze[n].neighb[2] = nmaze(i, j-1);
maze[n].directions[0] = 3;
maze[n].directions[1] = 0;
maze[n].directions[2] = 2;
if (i<NXMAZE-1)
{
printf("i = %i, q = %i, j+ = %i\n", i, q, block*NXMAZE + 2*q);
maze[n].neighb[3] = nmaze(i+1, block*NXMAZE + 2*q);
printf("i = %i, q = %i, j+ = %i\n", i, q, block*NXMAZE + 2*q + 1);
maze[n].neighb[4] = nmaze(i+1, block*NXMAZE + 2*q + 1);
maze[n].directions[3] = 1;
maze[n].directions[4] = 4;
}
}
i++;
width *= 2;
}
}
/* initialize other parameters */
for (i=0; i<NXMAZE; i++)
for (j=0; j<NYMAZE; j++)
{
n = nmaze(i, j);
maze[n].active = 0;
if (maze[n].nneighb == 0) maze[n].tested = 1;
else maze[n].tested = 0;
maze[n].connected = 0;
maze[n].closed = 0;
maze[n].north = 1;
maze[n].east = 1;
maze[n].south = 1;
maze[n].west = 1;
maze[n].northeast = 1;
}
/* for debugging */
// for (i=0; i<NXMAZE; i++)
// for (j=0; j<NYMAZE; j++)
// {
// n = nmaze(i, j);
// q = maze[n].nneighb;
// if (q > 0) printf("Cell (%i, %i)\n", i, j);
// for (k = 0; k <q; k++)
// {
// p = maze[n].neighb[k];
// printf("Neighbour %i at (%i, %i)\t", k, p%NXMAZE, p/NXMAZE);
// printf("Direction %i\n", maze[n].directions[k]);
// }
// }
// sleep(5);
}
void init_hex_maze_graph(t_maze maze[NXMAZE*NYMAZE])
/* initialise graph of maze with honeycomb cells */
/* NXMAZE and NYMAZE are assumed to be even */
/* directions are: 0 - north, 1 - NE, 2 - SE, 3 - south, 4 - SW, 5 - NW */
{
int i, j, k, n;
printf("Initializing maze\n");
if (MAZE_MAX_NGBH < 6)
{
printf("Error: MAZE_MAX_NGBH should be at least 5 for circular maze\n");
exit(0);
}
/* initialize neighbours */
/* in the bulk */
for (i=1; i<NXMAZE-1; i++)
for (j=1; j<NYMAZE-1; j++)
{
n = nmaze(i, j);
maze[n].nneighb = 6;
maze[n].neighb[0] = nmaze(i, j+1);
maze[n].neighb[3] = nmaze(i, j-1);
if (i%2 == 0)
{
maze[n].neighb[1] = nmaze(i+1, j+1);
maze[n].neighb[2] = nmaze(i+1, j);
maze[n].neighb[4] = nmaze(i-1, j);
maze[n].neighb[5] = nmaze(i-1, j+1);
}
else
{
maze[n].neighb[1] = nmaze(i+1, j);
maze[n].neighb[2] = nmaze(i+1, j-1);
maze[n].neighb[4] = nmaze(i-1, j-1);
maze[n].neighb[5] = nmaze(i-1, j);
}
for (k=0; k<6; k++) maze[n].directions[k] = k;
}
/* left side */
for (j=1; j<NYMAZE-1; j++)
{
n = nmaze(0, j);
maze[n].nneighb = 4;
maze[n].neighb[0] = nmaze(0, j+1);
maze[n].neighb[1] = nmaze(1, j+1);
maze[n].neighb[2] = nmaze(1, j);
maze[n].neighb[3] = nmaze(0, j-1);
for (k=0; k<4; k++) maze[n].directions[k] = k;
}
/* right side */
for (j=1; j<NYMAZE-1; j++)
{
n = nmaze(NXMAZE-1, j);
maze[n].nneighb = 4;
maze[n].neighb[0] = nmaze(NXMAZE-1, j+1);
maze[n].neighb[1] = nmaze(NXMAZE-1, j-1);
maze[n].neighb[2] = nmaze(NXMAZE-2, j-1);
maze[n].neighb[3] = nmaze(NXMAZE-2, j);
maze[n].directions[0] = 0;
maze[n].directions[1] = 3;
maze[n].directions[2] = 4;
maze[n].directions[3] = 5;
}
/* bottom side */
for (i=1; i<NXMAZE-1; i++)
{
n = nmaze(i, 0);
if (i%2 == 0)
{
maze[n].nneighb = 5;
maze[n].neighb[0] = nmaze(i, 1);
maze[n].neighb[1] = nmaze(i+1, 1);
maze[n].neighb[2] = nmaze(i+1, 0);
maze[n].neighb[3] = nmaze(i-1, 0);
maze[n].neighb[4] = nmaze(i-1, 1);
maze[n].directions[0] = 0;
maze[n].directions[1] = 1;
maze[n].directions[2] = 2;
maze[n].directions[3] = 4;
maze[n].directions[4] = 5;
}
else
{
maze[n].nneighb = 3;
maze[n].neighb[0] = nmaze(i, 1);
maze[n].neighb[1] = nmaze(i+1, 0);
maze[n].neighb[2] = nmaze(i-1, 0);
maze[n].directions[0] = 0;
maze[n].directions[1] = 1;
maze[n].directions[2] = 5;
}
}
/* top side */
for (i=1; i<NXMAZE-1; i++)
{
n = nmaze(i, NYMAZE-1);
if (i%2 == 0)
{
maze[n].nneighb = 3;
maze[n].neighb[0] = nmaze(i+1, NYMAZE-1);
maze[n].neighb[1] = nmaze(i, NYMAZE-2);
maze[n].neighb[2] = nmaze(i-1, NYMAZE-1);
maze[n].directions[0] = 2;
maze[n].directions[1] = 3;
maze[n].directions[2] = 4;
}
else
{
maze[n].nneighb = 5;
maze[n].neighb[0] = nmaze(i+1, NYMAZE-1);
maze[n].neighb[1] = nmaze(i+1, NYMAZE-2);
maze[n].neighb[2] = nmaze(i, NYMAZE-2);
maze[n].neighb[3] = nmaze(i-1, NYMAZE-2);
maze[n].neighb[4] = nmaze(i-1, NYMAZE-1);
maze[n].directions[0] = 1;
maze[n].directions[1] = 2;
maze[n].directions[2] = 3;
maze[n].directions[3] = 4;
maze[n].directions[4] = 5;
}
}
/* corners */
n = nmaze(0,0);
maze[n].nneighb = 3;
maze[n].neighb[0] = nmaze(0,1);
maze[n].neighb[1] = nmaze(1,1);
maze[n].neighb[2] = nmaze(1,0);
maze[n].directions[0] = 0;
maze[n].directions[1] = 1;
maze[n].directions[2] = 2;
n = nmaze(NXMAZE-1,0);
maze[n].nneighb = 2;
maze[n].neighb[0] = nmaze(NXMAZE-1,1);
maze[n].neighb[1] = nmaze(NXMAZE-2,0);
maze[n].directions[0] = 0;
maze[n].directions[1] = 5;
n = nmaze(0,NYMAZE-1);
maze[n].nneighb = 2;
maze[n].neighb[0] = nmaze(1,NYMAZE-1);
maze[n].neighb[1] = nmaze(0,NYMAZE-2);
maze[n].directions[0] = 2;
maze[n].directions[1] = 3;
n = nmaze(NXMAZE-1,NYMAZE-1);
maze[n].nneighb = 3;
maze[n].neighb[0] = nmaze(NXMAZE-1,NYMAZE-2);
maze[n].neighb[1] = nmaze(NXMAZE-2,NYMAZE-2);
maze[n].neighb[2] = nmaze(NXMAZE-2,NYMAZE-1);
maze[n].directions[0] = 3;
maze[n].directions[1] = 4;
maze[n].directions[2] = 5;
/* initialize other parameters */
for (i=0; i<NXMAZE; i++)
for (j=0; j<NYMAZE; j++)
{
n = nmaze(i, j);
maze[n].active = 0;
maze[n].tested = 0;
maze[n].connected = 0;
maze[n].closed = 0;
maze[n].north = 1;
maze[n].northeast = 1;
maze[n].southeast = 1;
maze[n].south = 1;
maze[n].southwest = 1;
maze[n].northwest = 1;
}
}
int find_maze_path(t_maze maze[NXMAZE*NYMAZE], int n0, int *path, int *pathlength, int mazetype)
2022-10-18 23:28:20 +02:00
/* find a random walk path in the maze */
2022-11-20 23:17:39 +01:00
/* returns 0 or 1 depending on whether path reaches a tested cell or a deadend */
2022-10-18 23:28:20 +02:00
{
2022-11-20 23:17:39 +01:00
int active_counter = 0, i, n = n0, npaths, inext, nextcell, trial, nnext, deadend = 1, length = 0;
2022-10-18 23:28:20 +02:00
int next_table[4];
/* contruct random walk */
npaths = maze[n].nneighb;
2022-11-20 23:17:39 +01:00
path[0] = n0;
2022-10-18 23:28:20 +02:00
// while ((npaths > 0)&&(!maze[n].tested))
while ((npaths > 0))
{
maze[n].active = 1;
printf("Cell (%i, %i) ", n%NXMAZE, n/NXMAZE);
nnext = 0;
for (i=0; i<npaths; i++)
{
nextcell = maze[n].neighb[i];
2022-11-20 23:17:39 +01:00
if ((!maze[nextcell].active)&&((maze[nextcell].connected)||(!maze[nextcell].tested)))
2022-10-18 23:28:20 +02:00
{
next_table[nnext] = i;
nnext++;
}
}
if (nnext == 0)
{
2022-11-20 23:17:39 +01:00
deadend = 1;
2022-10-18 23:28:20 +02:00
printf("Ended path\n");
// sleep(5);
npaths = 0;
2022-11-20 23:17:39 +01:00
maze[n].closed = 1;
2022-10-18 23:28:20 +02:00
}
else
{
2022-11-20 23:17:39 +01:00
deadend = 0;
2022-10-18 23:28:20 +02:00
inext = next_table[rand()%nnext];
nextcell = maze[n].neighb[inext];
2023-01-22 16:49:04 +01:00
/* square and circular maze */
if (mazetype < 2) switch(maze[n].directions[inext]){
2022-10-18 23:28:20 +02:00
case(0):
{
printf("Moving north\n");
maze[n].north = 0;
maze[nextcell].south = 0;
break;
}
case(1):
{
printf("Moving east\n");
maze[n].east = 0;
maze[nextcell].west = 0;
break;
}
case(2):
{
printf("Moving south\n");
maze[n].south = 0;
maze[nextcell].north = 0;
break;
}
case(3):
{
printf("Moving west\n");
maze[n].west = 0;
2023-01-22 16:49:04 +01:00
/* TODO find which is which */
2022-10-18 23:28:20 +02:00
maze[nextcell].east = 0;
2023-01-22 16:49:04 +01:00
maze[nextcell].northeast = 0;
break;
}
case(4): /* for circular maze */
{
printf("Moving north-east\n");
maze[n].northeast = 0;
maze[nextcell].west = 0;
break;
}
}
/* case of hexagonal maze */
else switch(maze[n].directions[inext]){
case(0):
{
printf("Moving north\n");
maze[n].north = 0;
maze[nextcell].south = 0;
break;
}
case(1):
{
printf("Moving north-east\n");
maze[n].northeast = 0;
maze[nextcell].southwest = 0;
break;
}
case(2):
{
printf("Moving south-east\n");
maze[n].southeast = 0;
maze[nextcell].northwest = 0;
break;
}
case(3):
{
printf("Moving south\n");
maze[n].south = 0;
maze[nextcell].north = 0;
break;
}
case(4):
{
printf("Moving south-west\n");
maze[n].southwest = 0;
maze[nextcell].northeast = 0;
break;
}
case(5):
{
printf("Moving north-west\n");
maze[n].northwest = 0;
maze[nextcell].southeast = 0;
2022-10-18 23:28:20 +02:00
break;
}
}
n = nextcell;
if (maze[n].tested) npaths = 0;
else npaths = maze[n].nneighb;
active_counter++;
2022-11-20 23:17:39 +01:00
if (length < NXMAZE*NYMAZE)
{
length++;
path[length] = n;
}
deadend = 0;
2022-10-18 23:28:20 +02:00
}
}
2022-11-20 23:17:39 +01:00
printf("Reached tested cell (%i, %i)\n", n%NXMAZE, n/NXMAZE);
if (!maze[n].connected) deadend = 1;
2022-10-18 23:28:20 +02:00
/* update cell status */
for (n=0; n<NXMAZE*NYMAZE; n++) if (maze[n].active)
{
maze[n].active = 0;
maze[n].tested = 1;
}
printf("Ended path\n");
2022-11-20 23:17:39 +01:00
if (deadend) printf("Deadend\n");
*pathlength = length;
printf("Path length %i \n", length);
2022-10-18 23:28:20 +02:00
2022-11-20 23:17:39 +01:00
return(deadend);
// return(active_counter);
2022-10-18 23:28:20 +02:00
}
2022-11-20 23:17:39 +01:00
void init_maze_old(t_maze maze[NXMAZE*NYMAZE])
2022-10-18 23:28:20 +02:00
/* init a maze */
{
2022-11-20 23:17:39 +01:00
int i, pathlength, *path;
2022-10-18 23:28:20 +02:00
init_maze_graph(maze);
for (i=0; i<RAND_SHIFT; i++) rand();
2023-01-22 16:49:04 +01:00
for (i=0; i<NXMAZE*NYMAZE; i++) if (!maze[i].tested) find_maze_path(maze, i, path, &pathlength, 0);
2022-10-18 23:28:20 +02:00
}
2023-01-22 16:49:04 +01:00
void init_maze_oftype(t_maze maze[NXMAZE*NYMAZE], int type)
/* init a maze of given type */
2022-11-20 23:17:39 +01:00
{
int i, j, n, deadend, pathlength, newpathlength;
int *path, *newpath;
path = (int *)malloc(2*NXMAZE*NYMAZE*sizeof(short int));
newpath = (int *)malloc(2*NXMAZE*NYMAZE*sizeof(short int));
2023-01-22 16:49:04 +01:00
switch (type) {
case (0):
{
init_maze_graph(maze);
break;
}
case (1):
{
init_circular_maze_graph(maze);
break;
}
case (2):
{
init_hex_maze_graph(maze);
break;
}
}
2022-11-20 23:17:39 +01:00
for (i=0; i<RAND_SHIFT; i++) rand();
2023-01-22 16:49:04 +01:00
find_maze_path(maze, 0, path, &pathlength, type);
2022-11-20 23:17:39 +01:00
for (n=0; n<pathlength; n++) maze[path[n]].connected = 1;
for (i=0; i<NXMAZE*NYMAZE; i++) if ((!maze[i].tested)&&(!maze[i].connected))
{
2023-01-22 16:49:04 +01:00
deadend = find_maze_path(maze, i, path, &pathlength, type);
2022-11-20 23:17:39 +01:00
if (!deadend) for (n=0; n<pathlength; n++) maze[path[n]].connected = 1;
j = 0;
printf("deadend = %i, pathlength = %i\n", deadend, pathlength);
// while ((deadend)&&(j < pathlength))
while (deadend)
{
j++;
if (j > pathlength) j = 0;
printf("j = %i\n", j);
// while (deadend)
2023-01-22 16:49:04 +01:00
if (!maze[path[j]].connected) deadend = find_maze_path(maze, path[j], newpath, &newpathlength, type);
2022-11-20 23:17:39 +01:00
if (!deadend) for (n=0; n<newpathlength; n++) maze[newpath[n]].connected = 1;
}
// for (n=0; n<newpathlength; n++) maze[newpath[n]].connected = 1;
for (j=0; j<NXMAZE*NYMAZE; j++) if (maze[j].tested) maze[j].connected = 1;
}
free(path);
free(newpath);
}
2023-01-22 16:49:04 +01:00
void init_maze(t_maze maze[NXMAZE*NYMAZE])
/* init a maze */
{
init_maze_oftype(maze, 0);
}
void init_circular_maze(t_maze maze[NXMAZE*NYMAZE])
/* init a circular maze */
{
// int i, j, n, q;
init_maze_oftype(maze, 1);
/* for debugging */
// for (i=0; i<NXMAZE; i++)
// for (j=0; j<NYMAZE; j++)
// {
// n = nmaze(i, j);
// q = maze[n].nneighb;
// if (q > 0)
// {
// printf("Cell (%i, %i)\t", i, j);
// if (maze[n].north) printf("North ");
// if (maze[n].northeast) printf("N-E ");
// if (maze[n].east) printf("East ");
// if (maze[n].south) printf("South ");
// if (maze[n].west) printf("West ");
// printf("\n");
// }
// }
// sleep(5);
}
void init_hex_maze(t_maze maze[NXMAZE*NYMAZE])
/* init a maze with hexagonal cells */
{
init_maze_oftype(maze, 2);
}
2022-11-20 23:17:39 +01:00
void init_maze_exit(int nx, int ny, t_maze maze[NXMAZE*NYMAZE])
/* init a maze with exit at (nx, ny) */
{
int i, j, n, deadend, pathlength, newpathlength;
int *path, *newpath;
path = (int *)malloc(2*NXMAZE*NYMAZE*sizeof(short int));
newpath = (int *)malloc(2*NXMAZE*NYMAZE*sizeof(short int));
init_maze_graph(maze);
for (i=0; i<RAND_SHIFT; i++) rand();
2023-01-22 16:49:04 +01:00
find_maze_path(maze, nmaze(nx, ny), path, &pathlength, 0);
2022-11-20 23:17:39 +01:00
for (n=0; n<pathlength; n++) maze[path[n]].connected = 1;
for (i=0; i<NXMAZE*NYMAZE; i++) if ((!maze[i].tested)&&(!maze[i].connected))
{
2023-01-22 16:49:04 +01:00
deadend = find_maze_path(maze, i, path, &pathlength, 0);
2022-11-20 23:17:39 +01:00
if (!deadend) for (n=0; n<pathlength; n++) maze[path[n]].connected = 1;
j = 0;
printf("deadend = %i, pathlength = %i\n", deadend, pathlength);
// while ((deadend)&&(j < pathlength))
while (deadend)
{
j++;
if (j > pathlength) j = 0;
printf("j = %i\n", j);
// while (deadend)
2023-01-22 16:49:04 +01:00
if (!maze[path[j]].connected) deadend = find_maze_path(maze, path[j], newpath, &newpathlength, 0);
2022-11-20 23:17:39 +01:00
if (!deadend) for (n=0; n<newpathlength; n++) maze[newpath[n]].connected = 1;
}
// for (n=0; n<newpathlength; n++) maze[newpath[n]].connected = 1;
for (j=0; j<NXMAZE*NYMAZE; j++) if (maze[j].tested) maze[j].connected = 1;
}
free(path);
free(newpath);
}
2022-10-18 23:28:20 +02:00