#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip.h>
#include <graphics.h>
void chright();
int chway();
void circle();
void move();
void draw(void);
int search(void);
void delay(void);
int strcheck(void);
void setgraph(void);
const n=13;
const m=13;
char x[m][n]={{"#############"},
{"#####..######"},
{"....##.#....#"},
{"###.##.#.##.#"},
{"###.#..#.##.#"},
{"#...#.##.##.#"},
{"#.###....##.#"},
{"#.#.#.##..#.#"},
{"#.#.#.###.#.#"},
{"#.....###.#.#"},
{"###.#####.#.#"},
{"#.........#.."},
{"#############"}};
int gapi[10];
int gapj[10];
int walli[10];
int wallj[10];
int count,stp;
int wi,wj,si,sj,i,j;
void main() // main program
{
cout<<" Welcome to Maza, \n how much delay you need? ";
cin>>stp;
setgraph();
draw();
i=search();
if (i<=1)
{
cout<< "\n\n Sorry , \n Maze couldn't find any way ... ";
// exit(0);
}
else
cout<<" \n\n there is "<< i <<" ways existing , press any key to try ...";
getch();
si=gapi[0];wi=walli[0];
sj=gapj[0];wj=wallj[0];
x[si][sj]='M';
draw();
while (!strcheck())
{
chright();
draw();
if (chway() && !strcheck())
move();
else if (!strcheck())
circle();
draw();
}
if (strcheck()==1)
cout<< "\n\n Congratulate , \n Maze find the way ... ";
else
cout<< "\n\n Sorry , \n Maze couldn't find any way ... ";
getch();
closegraph();
}
void chright()
{
x[si][sj]='.';
if (si==wi && sj>wj && x[si][sj-1]=='.')
{sj--;wi--;}
else if (si==wi && sj<wj && x[si][sj+1]=='.')
{sj++;wi++;}
else if (sj==wj && si>wi && x[si-1][sj]=='.')
{si--;wj++;}
else if (sj==wj && si<wi && x[si+1][sj]=='.')
{si++;wj--;}
x[si][sj]='M';
}
int chway()
{
int k=1;
if (si==wi && sj>wj && x[si+1][sj]=='#')
k=0;
else if (si==wi && sj<wj && x[si-1][sj]=='#')
k=0;
else if (sj==wj && si>wi && x[si][sj-1]=='#')
k=0;
else if (sj==wj && si<wi && x[si][sj+1]=='#')
k=0;
return(k);
}
void circle()
{
x[si][sj]='.';
if (si==wi && sj>wj)
{wi++;wj++;}
else if (si==wi && sj<wj)
{wi--;wj--;}
else if (sj==wj && si>wi)
{wj--;wi++;}
else if (sj==wj && si<wi)
{wj++;wi--;}
x[si][sj]='M';
}
void move()
{
count++;
x[si][sj]='.';
if (si==wi && sj>wj)
{
si++;wi++;
}
else if (si==wi && sj<wj)
{
si--;wi--;
}
else if (sj==wj && si>wi)
{
sj--;wj--;
}
else if (sj==wj && si<wi)
{
sj++;wj++;
}
x[si][sj]='M';
}
int search(void)
{
int k=0;
int i,j;
for (i=0;i<m;i++)
if (x[i][0]=='.')
{
gapi[k]=i; gapj[k]=0;
walli[k]=i+1; wallj[k]=0; k++;
}
for (i=0;i<m;i++)
if (x[i][n-1]=='.')
{
gapi[k]=i;gapj[k]=n-1;
walli[k]=i-1; wallj[k]=n-1; k++;
}
for (j=0;j<n;j++)
if (x[0][j]=='.')
{
gapi[k]=0;gapj[k]=j;
walli[k]=0; wallj[k]=j-1; k++;
}
for (j=0;j<n;j++)
if (x[m-1][j]=='.')
{
gapi[k]=m-1;gapj[k]=j;
walli[k]=m-1; wallj[k]=j+1; k++;
}
return(k);
}
int strcheck(void) // check if maze finish traveling
{
int k=0;
if (count>1)
if (si==0 || si==m-1 || sj==0 || sj==n-1)
if(si==gapi[0] && sj==gapj[0])
k=2;
else
k=1;
else
k=0;
return(k);
}
void delay(void)
{
for (int p=0;p<stp;p++)
for(int q=0;q<stp;q++)
for(int r=0;r<stp;r++);
}
/*
void draw (void) // draw current board
{
clrscr();
for (int p=0;p<m;p++)
{
for (int q=0;q<n;q++)
cout<<x[p][q]<<setw(5);
cout<<endl<<endl;
}
delay();
}
*/
void draw(void)
{
// clrscr();
int mx=getmaxx()/n;
int my=getmaxy()/m;
for (int p=0;p<m;p++)
{
for (int q=0;q<n;q++)
{
switch (x[p][q]){
case '.': setfillstyle(11,DARKGRAY);break;
case '#': setfillstyle(1,BLUE);break;
case 'M': setfillstyle(1,YELLOW);break;
}
bar(q*mx,p*my,q*mx+mx,p*my+my);
}
}
delay();
}
void setgraph(void)
{
int gd=DETECT, gm;
initgraph (&gd,&gm,"..\\bgi");
}