Add files via upload
This commit is contained in:
295
sub_maze.c
295
sub_maze.c
@@ -4,6 +4,11 @@
|
||||
|
||||
/* Change constant RAND_SHIFT to change the maze */
|
||||
|
||||
#define MAZE_TYPE_SQUARE 0 /* maze with square cells */
|
||||
#define MAZE_TYPE_CIRCLE 1 /* circular maze */
|
||||
#define MAZE_TYPE_HEX 2 /* honeycomb maze */
|
||||
#define MAZE_TYPE_OCT 3 /* maze with octagonal and square cells */
|
||||
|
||||
typedef struct
|
||||
{
|
||||
short int nneighb; /* number of neighbours */
|
||||
@@ -488,6 +493,208 @@ void init_hex_maze_graph(t_maze maze[NXMAZE*NYMAZE])
|
||||
}
|
||||
}
|
||||
|
||||
void init_oct_maze_graph(t_maze maze[NXMAZE*NYMAZE])
|
||||
/* initialise graph of maze made of octagons and squares */
|
||||
{
|
||||
int i, j, k, n, p, q;
|
||||
|
||||
printf("Initializing maze\n");
|
||||
if (MAZE_MAX_NGBH < 8)
|
||||
{
|
||||
printf("Error: MAZE_MAX_NGBH should be at least 8 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 = 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;
|
||||
|
||||
if ((i+j)%2 == 0)
|
||||
{
|
||||
maze[n].nneighb = 8;
|
||||
maze[n].neighb[4] = nmaze(i+1, j+1);
|
||||
maze[n].neighb[5] = nmaze(i+1, j-1);
|
||||
maze[n].neighb[6] = nmaze(i-1, j-1);
|
||||
maze[n].neighb[7] = nmaze(i-1, j+1);
|
||||
for (k=4; k<8; 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;
|
||||
|
||||
if (j%2 == 0)
|
||||
{
|
||||
maze[n].nneighb = 5;
|
||||
maze[n].neighb[3] = nmaze(1, j+1);
|
||||
maze[n].neighb[4] = nmaze(1, j-1);
|
||||
for (k=3; k<5; k++) maze[n].directions[k] = k+1;
|
||||
}
|
||||
}
|
||||
/* 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;
|
||||
|
||||
if ((NXMAZE-1+j)%2 == 0)
|
||||
{
|
||||
maze[n].nneighb = 5;
|
||||
maze[n].neighb[3] = nmaze(NXMAZE-2, j-1);
|
||||
maze[n].neighb[4] = nmaze(NXMAZE-2, j+1);
|
||||
for (k=3; k<5; k++) maze[n].directions[k] = k+3;
|
||||
}
|
||||
}
|
||||
/* 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;
|
||||
|
||||
if (i%2 == 0)
|
||||
{
|
||||
maze[n].nneighb = 5;
|
||||
maze[n].neighb[3] = nmaze(i+1, 1);
|
||||
maze[n].neighb[4] = nmaze(i-1, 1);
|
||||
maze[n].directions[3] = 4;
|
||||
maze[n].directions[4] = 7;
|
||||
}
|
||||
}
|
||||
/* 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;
|
||||
|
||||
if ((i+NXMAZE-1)%2 == 0)
|
||||
{
|
||||
maze[n].nneighb = 5;
|
||||
maze[n].neighb[3] = nmaze(i+1, NYMAZE-2);
|
||||
maze[n].neighb[4] = nmaze(i-1, NYMAZE-2);
|
||||
maze[n].directions[3] = 5;
|
||||
maze[n].directions[4] = 6;
|
||||
}
|
||||
}
|
||||
/* corners */
|
||||
n = nmaze(0,0);
|
||||
maze[n].nneighb = 3;
|
||||
maze[n].neighb[0] = nmaze(1,0);
|
||||
maze[n].neighb[1] = nmaze(0,1);
|
||||
maze[n].neighb[2] = nmaze(1,1);
|
||||
maze[n].directions[0] = 1;
|
||||
maze[n].directions[1] = 0;
|
||||
maze[n].directions[2] = 4;
|
||||
|
||||
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;
|
||||
if ((NXMAZE-1)%2 == 0)
|
||||
{
|
||||
maze[n].nneighb = 3;
|
||||
maze[n].neighb[2] = nmaze(NXMAZE-2,1);
|
||||
maze[n].directions[2] = 7;
|
||||
}
|
||||
|
||||
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;
|
||||
if ((NYMAZE-1)%2 == 0)
|
||||
{
|
||||
maze[n].nneighb = 3;
|
||||
maze[n].neighb[2] = nmaze(1,NYMAZE-2);
|
||||
maze[n].directions[2] = 5;
|
||||
}
|
||||
|
||||
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;
|
||||
if ((NXMAZE+NYMAZE)%2 == 0)
|
||||
{
|
||||
maze[n].nneighb = 3;
|
||||
maze[n].neighb[2] = nmaze(NXMAZE-2,NYMAZE-2);
|
||||
maze[n].directions[2] = 6;
|
||||
}
|
||||
|
||||
/* 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].east = 1;
|
||||
maze[n].south = 1;
|
||||
maze[n].west = 1;
|
||||
maze[n].northeast = 1;
|
||||
maze[n].southeast = 1;
|
||||
maze[n].southwest = 1;
|
||||
maze[n].northwest = 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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int find_maze_path(t_maze maze[NXMAZE*NYMAZE], int n0, int *path, int *pathlength, int mazetype)
|
||||
/* find a random walk path in the maze */
|
||||
/* returns 0 or 1 depending on whether path reaches a tested cell or a deadend */
|
||||
@@ -531,7 +738,7 @@ int find_maze_path(t_maze maze[NXMAZE*NYMAZE], int n0, int *path, int *pathlengt
|
||||
inext = next_table[rand()%nnext];
|
||||
nextcell = maze[n].neighb[inext];
|
||||
/* square and circular maze */
|
||||
if (mazetype < 2) switch(maze[n].directions[inext]){
|
||||
if (mazetype < MAZE_TYPE_HEX) switch(maze[n].directions[inext]){
|
||||
case(0):
|
||||
{
|
||||
printf("Moving north\n");
|
||||
@@ -571,7 +778,7 @@ int find_maze_path(t_maze maze[NXMAZE*NYMAZE], int n0, int *path, int *pathlengt
|
||||
}
|
||||
}
|
||||
/* case of hexagonal maze */
|
||||
else switch(maze[n].directions[inext]){
|
||||
else if (mazetype == MAZE_TYPE_HEX) switch(maze[n].directions[inext]){
|
||||
case(0):
|
||||
{
|
||||
printf("Moving north\n");
|
||||
@@ -615,7 +822,65 @@ int find_maze_path(t_maze maze[NXMAZE*NYMAZE], int n0, int *path, int *pathlengt
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* case of octagonal maze */
|
||||
else if (mazetype == MAZE_TYPE_OCT) switch(maze[n].directions[inext]){
|
||||
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;
|
||||
maze[nextcell].east = 0;
|
||||
break;
|
||||
}
|
||||
case(4):
|
||||
{
|
||||
printf("Moving north-east\n");
|
||||
maze[n].northeast = 0;
|
||||
maze[nextcell].southwest = 0;
|
||||
break;
|
||||
}
|
||||
case(5):
|
||||
{
|
||||
printf("Moving south-east\n");
|
||||
maze[n].southeast = 0;
|
||||
maze[nextcell].northwest = 0;
|
||||
break;
|
||||
}
|
||||
case(6):
|
||||
{
|
||||
printf("Moving south-west\n");
|
||||
maze[n].southwest = 0;
|
||||
maze[nextcell].northeast = 0;
|
||||
break;
|
||||
}
|
||||
case(7):
|
||||
{
|
||||
printf("Moving north-west\n");
|
||||
maze[n].northwest = 0;
|
||||
maze[nextcell].southeast = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
n = nextcell;
|
||||
if (maze[n].tested) npaths = 0;
|
||||
else npaths = maze[n].nneighb;
|
||||
@@ -672,26 +937,32 @@ void init_maze_oftype(t_maze maze[NXMAZE*NYMAZE], int type)
|
||||
newpath = (int *)malloc(2*NXMAZE*NYMAZE*sizeof(short int));
|
||||
|
||||
switch (type) {
|
||||
case (0):
|
||||
case (MAZE_TYPE_SQUARE):
|
||||
{
|
||||
init_maze_graph(maze);
|
||||
break;
|
||||
}
|
||||
case (1):
|
||||
case (MAZE_TYPE_CIRCLE):
|
||||
{
|
||||
init_circular_maze_graph(maze);
|
||||
break;
|
||||
}
|
||||
case (2):
|
||||
case (MAZE_TYPE_HEX):
|
||||
{
|
||||
init_hex_maze_graph(maze);
|
||||
break;
|
||||
}
|
||||
case (MAZE_TYPE_OCT):
|
||||
{
|
||||
init_oct_maze_graph(maze);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (i=0; i<RAND_SHIFT; i++) rand();
|
||||
|
||||
find_maze_path(maze, 0, path, &pathlength, type);
|
||||
|
||||
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))
|
||||
@@ -723,7 +994,7 @@ void init_maze_oftype(t_maze maze[NXMAZE*NYMAZE], int type)
|
||||
void init_maze(t_maze maze[NXMAZE*NYMAZE])
|
||||
/* init a maze */
|
||||
{
|
||||
init_maze_oftype(maze, 0);
|
||||
init_maze_oftype(maze, MAZE_TYPE_SQUARE);
|
||||
}
|
||||
|
||||
void init_circular_maze(t_maze maze[NXMAZE*NYMAZE])
|
||||
@@ -731,7 +1002,7 @@ void init_circular_maze(t_maze maze[NXMAZE*NYMAZE])
|
||||
{
|
||||
// int i, j, n, q;
|
||||
|
||||
init_maze_oftype(maze, 1);
|
||||
init_maze_oftype(maze, MAZE_TYPE_CIRCLE);
|
||||
|
||||
/* for debugging */
|
||||
// for (i=0; i<NXMAZE; i++)
|
||||
@@ -756,7 +1027,13 @@ void init_circular_maze(t_maze maze[NXMAZE*NYMAZE])
|
||||
void init_hex_maze(t_maze maze[NXMAZE*NYMAZE])
|
||||
/* init a maze with hexagonal cells */
|
||||
{
|
||||
init_maze_oftype(maze, 2);
|
||||
init_maze_oftype(maze, MAZE_TYPE_HEX);
|
||||
}
|
||||
|
||||
void init_oct_maze(t_maze maze[NXMAZE*NYMAZE])
|
||||
/* init a maze with hexagonal cells */
|
||||
{
|
||||
init_maze_oftype(maze, MAZE_TYPE_OCT);
|
||||
}
|
||||
|
||||
void init_maze_exit(int nx, int ny, t_maze maze[NXMAZE*NYMAZE])
|
||||
|
||||
Reference in New Issue
Block a user