part 2:
void displayMazeSpot(int x, int y)
{
//display what can be seen from this spot in the maze
//the 8 surounding leds can always be seen
setDot1(x,y, -1, -1, dim1);
setDot1(x,y, 0, -1, dim1);
setDot1(x,y, 1, -1, dim1);
setDot1(x,y, -1, 0, dim1);
setDot1(x,y, 1, 0, dim1);
setDot1(x,y, -1, 1, dim1);
setDot1(x,y, 0, 1, dim1);
setDot1(x,y, 1, 1, dim1);
if(getMaze(x,y-1)==1)
{
setDot1(x,y, 0, -2, dim2);
setDot1(x,y, -1, -2, dim2);
setDot1(x,y, 1, -2, dim2);
if(getMaze(x,y-2)==1)
{
setDot1(x,y, 0, -3, dim3);
setDot1(x,y, -1, -3, dim3);
setDot1(x,y, 1, -3, dim3);
}
}
if(getMaze(x,y+1)==1)
{
setDot1(x,y, 0, 2, dim2);
setDot1(x,y, -1, 2, dim2);
setDot1(x,y, 1, 2, dim2);
if(getMaze(x,y+2)==1)
{
setDot1(x,y, 0, 3, dim3);
setDot1(x,y, -1, 3, dim3);
setDot1(x,y, 1, 3, dim3);
}
}
if(getMaze(x-1,y)==1)
{
setDot1(x,y, -2, 0, dim2);
setDot1(x,y, -2, -1, dim2);
setDot1(x,y, -2, 1, dim2);
if(getMaze(x-2,y)==1)
{
setDot1(x,y, -3, 0, dim3);
setDot1(x,y, -3, -1, dim3);
setDot1(x,y, -3, 1, dim3);
}
}
if(getMaze(x+1,y)==1)
{
setDot1(x,y, 2, 0, dim2);
setDot1(x,y, 2, -1, dim2);
setDot1(x,y, 2, 1, dim2);
if(getMaze(x+2,y)==1)
{
setDot1(x,y, 3, 0, dim3);
setDot1(x,y, 3, -1, dim3);
setDot1(x,y, 3, 1, dim3);
}
}
}
void setDot(int x, int y, int dim)
{
LedSign::Set(x,y,(1-maze[x][y])*dim);
}
void setDot1(int x, int y, int xo, int yo, int dim)
{
//include bounds detection & dispay to spot
x=x+xo;
y=y+yo;
if(x>=0 && x<=xgrid && y>=0 && y<=ygrid)
LedSign::Set(xMazeDisplay+xo,yMazeDisplay+yo,(1-maze[x][y])*dim);
}
bool getMaze(int x, int y)
{
if(x>=0 && x<=xgrid && y>=0 && y<=ygrid)
return maze[x][y];
else
return 0; //if outside maze it is a wall
}
void displayMaze()
{
int x,y;
x=xgrid;
if(xgrid>14)
x=14;
y=ygrid;
if(ygrid>9)
y=9;
for(int xx=0; xx< x; xx++) //print maze
{
for(int yy=0; yy< y; yy++)
{
LedSign::Set(xx,yy,(1-maze[xx][yy])*dim1);
}
}
}
void buildMaze()
{
/*
in what direction to go?
1=5= right x+1
2=6= down Y+1
3=7= left x-1
4= up y-1
direction = old direction + rnd(3)
Is the new direction correct?
coordinate assignments for up move see table below.
the E & F fields could be ommited but the maze would then seem to have many diagonal moves.
0 E D F 0
0 A B C 0
0 0 Q 0 0
0 0 0 0 0
0 0 0 0 0
From Q A is not used or the limit of the board, I not used K not used. then A is next Q
Same goes for the others.
All directions are filled?
Test all locations until a spot is found that is used Q where A is not used or the limit of the board, I not used K not used. then A is next Q
Same goes for the others.
Relative positions to check from Q in relation to grid:
UP Right Down Left
A(-1,-1) (1,-1) (-1,1) (-1,-1)
B(0 ,-1) (1, 0) ( 0,1) (-1, 0)
C(1 ,-1) (1, 1) ( 1,1) (-1, 1)
D(0 ,-2) (2, 0) ( 0,2) (-2, 1)
E(-1,-2) (2,-1) ( 1,2) (-2, 1)
F( 1,-2) (2, 1) (-1,2) (-2,-1)
First choose rnd(direction) then test if direcrion is ok
Any used spot is 1 so 1 is road 0 is wall
*/
char dir, olddir=1;
int x=0,y=0;
//char runs=0;
char tries=0;
// for(int runs=0;runs<5;runs++)
maze[0][0]=1; //start position
for(int x1=0; x1< xgrid; x1++) //find empty sports and build maze
{
for(int y1=0; y1< ygrid; y1++)
{// try to build maze from this spot
// Serial.print("X=");
// Serial.print(x,DEC);
// Serial.print(" Y=");
// Serial.print(y,DEC);
// Serial.print(" tries=");
// Serial.println(tries,DEC);
tries=0;
x=x1;
y=y1;
if(maze[x][y]==1)
{
while(tries<5) //try this until solution found
{
dir=olddir+random(3);
// Serial.print(" Dir=");
// Serial.println(dir,DEC);
switch (dir)
{
case 1:
case 5:
//right
olddir=1;
tries++;
if(rightMove(x,y))
{
x++;
maze[x][y]=1;
tries=0;
}
break;
case 2:
case 6:
//down
olddir=2;
tries++;
if(downMove(x,y))
{
y++;
maze[x][y]=1;
tries=0;
}
break;
case 3:
case 7:
//left
olddir=3;
tries++;
if(leftMove(x,y))
{
x--;
maze[x][y]=1;
tries=0;
}
break;
case 4:
//up
tries=0;
if(upMove(x,y))
{
y--;
maze[x][y]=1;
tries=0;
}
break;
}
}
}
}
}
findMazeEnd();
}
bool rightMove(int x,int y)
{
char ax, bx, cx, dx, ex, fx, ay, by, cy, dy, ey ,fy;
ax=x+1;
bx=x+1;
cx=x+1;
dx=x+2;
ex=x+2;
fx=x+2;
ay=y-1;
by=y;
cy=y+1;
dy=y;
ey=y-1;
fy=y+1;
if(ax == xgrid) //test grid limits
return 0; //right move not possible
if (ay < 0)
{
ay=0;
ey=0;
}
if (cy == ygrid) //test grid limits
{
cy=y;
fy=y;
}
if (dx >= xgrid)
{
dx=cx;
ex=cx;
fx=cx;
}
if(maze[ax][ay]==0 && maze[bx][by]==0 && maze[cx][cy]==0 && maze[dx][dy]==0 && maze[ex][ey]==0 && maze[fx][fy]==0)
{
x++;
maze[x][y]=1;
return 1;
}
return 0;
}
bool downMove(int x, int y)
{
char ax, bx, cx, dx, ex, fx, ay, by, cy, dy, ey ,fy;
ax=x-1;
bx=x;
cx=x+1;
dx=x;
ex=x+1;
fx=x-1;
ay=y+1;
by=y+1;
cy=y+1;
dy=y+2;
ey=y+2;
fy=y+2;
if(ay == ygrid) //test grid limits
return 0; //down is not possible
if (ax<0)
{
ax=0;
fx=0;
}
if (cx=xgrid)
{
cx=x;
ex=x;
}
if(dy >= ygrid)
{
dy=cy;
ey=cy;
fy=cy;
}
if(maze[ax][ay]==0 && maze[bx][by]==0 && maze[cx][cy]==0 && maze[dx][dy]==0 && maze[ex][ey]==0 && maze[fx][fy]==0)
{
y++;
maze[x][y]=1;
return 1;
}
return 0;
}
bool leftMove(int x, int y)
{
char ax, bx, cx, dx, ex, fx, ay, by, cy, dy, ey ,fy;
ax=x-1;
bx=x-1;
cx=x-1;
dx=x-2;
ex=x-2;
fx=x-2;
ay=y-1;
by=y;
cy=y+1;
dy=y;
ey=y+1;
fy=y-1;
if(ax<0)
return 0;
if(ay<0)
{
ay=0;
fy=0;
}
if(cy==ygrid)
{
cy=y;
ey=y;
}
if(dx <= 0)
{
dx=0;
ex=0;
fx=0;
}
if(maze[ax][ay]==0 && maze[bx][by]==0 && maze[cx][cy]==0 && maze[dx][dy]==0 && maze[ex][ey]==0 && maze[fx][fy]==0)
{
x--;
maze[x][y]=1;
return 1;
}
return 0;
}
bool upMove(int x, int y)
{
char ax, bx, cx, dx, ex, fx, ay, by, cy, dy, ey ,fy;
ax=x-1;
bx=x;
cx=x+1;
dx=x;
ex=x-1;
fx=x+1;
ay=y-1;
by=y-1;
cy=y-1;
dy=y-2;
ey=y-2;
fy=y-2;
if(ay<0)
return 0;
if(ax<0)
{
ax=0;
ex=0;
}
if(cx==xgrid)
{
cx=x;
fx=x;
}
if (dy <= 0)
{
dy=0;
ey=0;
fy=0;
}
if(maze[ax][ay]==0 && maze[bx][by]==0 && maze[cx][cy]==0 && maze[dx][dy]==0 && maze[ex][ey]==0 && maze[fx][fy]==0)
{
y--;
maze[x][y]=1;
return 1;
}
return 0;
}
void findMazeEnd()
{
// the maze will fill completely so only look in the lower right corner
for(int y=ygrid-1; y>(ygrid-4); y--)
{
for(int x=xgrid-1; x>(xgrid-4); x--)
{
if(maze[x][y]==1)
{
mazeEndX=x;
mazeEndY=y;
return;
}
}
}
}