#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip.h>
#include <graphics.h>
#include <dos.h>
#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80
#define ESC 27
int move(int);
void draw(void);
int search(void);
int strcheck(void);
void setgraph(void);
void message(char *);
const n=13;
const m=13;
char x[m][n]={{"#############"},
{"#####..######"},
{"....##.#....#"},
{"###.##.#.##.#"},
{"###.#..#.##.#"},
{"#...#.##.##.#"},
{"#.###....##.#"},
{"#.#.#.##..#.#"},
{"#.#.#.###.#.#"},
{"#.....###.#.#"},
{"###.#####.#.#"},
{"#.........#.."},
{"#############"}};
int gapi[10];
int gapj[10];
int si,sj;
void main()
{
setgraph();
draw();
int i=search();
if (i<=1)
{
message("Sorry , Maze couldn't find any way ... ");
exit(0);
}
else
{
char *temp="There is ways ,Press any key to try...";
temp[9]=i+0x30;
message(temp);
}
si=gapi[0];
sj=gapj[0];
x[si][sj]='M';
draw();
int key;
do{
key=getch();
if(move(key))
draw();
}while (!strcheck() && key!=ESC);
if (strcheck()==1)
message("Congratulate, Maze find the way ... ");
else
message("Sorry, Maze couldn't find any way ... ");
closegraph();
}
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;}
for (i=0;i<m;i++)
if (x[i][n-1]=='.')
{gapi[k]=i;gapj[k++]=n-1;}
for (j=0;j<n;j++)
if (x[0][j]=='.')
{gapi[k]=0;gapj[k++]=j;}
for (j=0;j<n;j++)
if (x[m-1][j]=='.')
{gapi[k]=m-1;gapj[k++]=j;}
return(k);
}
int strcheck(void)
{
int k=0;
if ((si==0 || si==m-1 || sj==0 || sj==n-1) && (si!=gapi[0] && sj!=gapj[0]))
k=1;
return(k);
}
void draw(void)
{
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);
}
}
}
void setgraph(void)
{
int gd=DETECT, gm;
initgraph (&gd,&gm,"..\\bgi");
}
void message(char *str)
{
setfillstyle(1,GREEN);
bar(130,200,550,250);
setcolor(WHITE);
rectangle(130,200,550,250);
setcolor(BLACK);
settextstyle(3,0,1);
outtextxy(140,210,str);
delay(1500);
getch();
}
int move(int dir)
{
int k=0;
switch (dir)
{
case LEFT: if(x[si][sj-1]!='#'){x[si][sj]='.'; sj--; x[si][sj]='M'; } k=1; break;
case RIGHT: if(x[si][sj+1]!='#'){x[si][sj]='.'; sj++; x[si][sj]='M'; } k=1; break;
case UP: if(x[si-1][sj]!='#'){x[si][sj]='.'; si--; x[si][sj]='M'; } k=1; break;
case DOWN: if(x[si+1][sj]!='#'){x[si][sj]='.'; si++; x[si][sj]='M'; } k=1; break;
}
return k;
}