void DrawPath(const Maze & m)
{
int i,j;
int mw,mh,left,top;
mw=m.GetWidth();
mh=m.GetHeight();
left=LEFT;
top=TOP; setcolor(PATHCOLOR);
for (j=0;j<mh;j++) /* horz */
for (i=0;i<mw-1;i++)
{
if (m.GetPath(i,j)&&m.GetPath(i+1,j)&& (m.GetMaze(i,j) & 0x2 ) )
line(left+(i+0)*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
top+(j+0)*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
left+(i+1)*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
top+(j+0)*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2);
}
for (j=0;j<mh-1;j++)
for (i=0;i<mw;i++)
{
if (m.GetPath(i,j)&&m.GetPath(i,j+1)&& (m.GetMaze(i,j) & 0x1 ) )
line(left+(i+0)*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
top+(j+0)*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
left+(i+0)*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
top+(j+1)*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2);
}
}int TraceMaze(TwoDimensionArray & tr,Point & cur,const Maze & m,direction dir)
{
int left,top;
left=(639-m.GetWidth()*MAZE_CELL_SIZE)/2;
top=(479-m.GetHeight()*MAZE_CELL_SIZE)/2+2;;
switch(dir)
{
case west:
if (cur.x>0 && (m.GetMaze(cur.x-1,cur.y) & 0x2))
{
if (tr.Get(cur.x-1,cur.y)==0)
{
tr.Set(cur.x-1,cur.y,1);
setcolor(TRACECOLOR);
}
else
{
tr.Set(cur.x,cur.y,0);
setcolor(BADCOLOR);
}
line(left+((cur.x-1))*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
top+cur.y*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
left+cur.x*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
top+cur.y*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2);
cur.x-=1;
}
break;
case north:
if (cur.y>0 && (m.GetMaze(cur.x,cur.y-1) & 0x1))
{
if (tr.Get(cur.x,cur.y-1)==0)
{
tr.Set(cur.x,cur.y-1,1);
setcolor(TRACECOLOR);
}
else
{
tr.Set(cur.x,cur.y,0);
setcolor(BADCOLOR);
}
line(left+cur.x*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
top+(cur.y-1)*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
left+cur.x*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
top+cur.y*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2);
cur.y-=1;
}
break;
case east:
if (cur.x<m.GetWidth() && (m.GetMaze(cur.x,cur.y) & 0x2))
{
if (tr.Get(cur.x+1,cur.y)==0)
{
tr.Set(cur.x+1,cur.y,1);
setcolor(TRACECOLOR);
}
else
{
tr.Set(cur.x,cur.y,0);
setcolor(BADCOLOR);
}
line(left+(cur.x+1)*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
top+cur.y*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
left+cur.x*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
top+cur.y*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2);
cur.x+=1;
}
break;
case south:
if (cur.y<m.GetHeight() && (m.GetMaze(cur.x,cur.y) & 0x1))
{
if (tr.Get(cur.x,cur.y+1)==0)
{
tr.Set(cur.x,cur.y+1,1);
setcolor(TRACECOLOR);
}
else
{
tr.Set(cur.x,cur.y,0);
setcolor(BADCOLOR);
}
line(left+cur.x*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
top+(cur.y+1)*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
left+cur.x*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2,
top+cur.y*MAZE_CELL_SIZE+MAZE_CELL_SIZE/2);
cur.y+=1;
}
break;
}
if (cur.x==m.GetEnd().x&&cur.y==m.GetEnd().y)
{
sound(700);
delay(100);
nosound();
return 1;
}
return 0;
}
int main(void)
{
int mazewidth=8,mazeheight=6,success=0;
int oldw,oldh;
randomize();
TwoDimensionArray trace(mazewidth,mazeheight);
Point cur(0,0);
Maze m(mazewidth,mazeheight);
int i,j,key; trace.Set(0,0,1); Draw();
DrawMaze(m);
DrawSize(m.GetWidth(),m.GetHeight());
for(;;)
{
oldw=mazewidth;
oldh=mazeheight;
while (bioskey(1)==0);
key=bioskey(0);
switch(key)
{
case ESC:
goto exit;
case F1:
DrawPath(m);
break;
case F2:
m.SetSize(mazewidth,mazeheight);
case F3:
DrawMaze(m);
trace.SetSize(mazewidth,mazeheight);
trace.Set(0,0,1);
cur.x=0;
cur.y=0;
success=0;
break;
case HOME:
if (mazewidth>2) mazewidth--;
break;
case END:
if (mazewidth<MAZE_MAX_WIDTH) mazewidth++;
break;
case PAGEUP:
if (mazeheight>2) mazeheight--;
break;
case PGDOWN:
if (mazeheight<MAZE_MAX_HEIGHT) mazeheight++;
break;
case LEFTKEY:
if (!success)
success=TraceMaze(trace,cur,m,west);
break;
case UPKEY:
if (!success)
success=TraceMaze(trace,cur,m,north);
break;
case RIGHTKEY:
if (!success)
success=TraceMaze(trace,cur,m,east);
break;
case DOWNKEY:
if (!success)
success=TraceMaze(trace,cur,m,south);
break;
}
if (oldw!=mazewidth||oldh!=mazeheight)
DrawSize(mazewidth,mazeheight);
}
exit: return 0;
}