-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
128 lines (107 loc) · 2.79 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include <time.h>
void PrintPath(int i, int j, int color) {
int x,y;
setfillstyle(SOLID_FILL,color);
x = 303+50*i;
y = 152+50*j ;
bar3d(x, y, x+50, y+50, 0, 0);
rectangle(x,y,x+50,y+50);
}
void SetValues(int maze[12][12]) {
int i,j;
for(j=0 ; j<12 ; j++) {
maze[0][j] = 1;
maze[11][j] = 1;
maze[j][0] = 1;
maze[j][11] = 1;
}
for(i=1; i<11 ; i++) {
for(j=1; j<11 ; j++) {
maze[i][j] =((rand()%3)%2);
}
}
maze[1][1] = 0;
maze[10][10] =0;
}
void PrintMaze(int maze[12][12]) {
int i,j;
setfillstyle(1,RED);
for(i = 0; i < 12;i++)
{
for(j = 0;j < 12;j++)
{
rectangle(300+50*(i), 150+50*(j), 300+50*(i)+50, 150+50*(j)+50);
if(maze[i][j] == 1)
{
bar3d(300+50*(i), 150+50*(j), 300+50*(i)+50, 150+50*(j)+50, 0, 3);
}
}
}
PrintPath(1, 1, 14);
PrintPath(10, 10, 2);
delay(1000);
}
main() {
initwindow(1280,853,"Rat in a Maze");
readimagefile("ratinmaze.jpg",0,0,1280,853);
delay(1200);
readimagefile("bg.jpg",0,0,1280,853);
int i,j,l,m;
srand(time(NULL));
int maze[12][12];
SetValues(maze);
PrintMaze(maze);
int mark[12][12];
for(i=0; i<12 ; i++) {
for(j=0 ; j<12 ; j++) {
mark[i][j] = maze[i][j];
}
}
int move[8][2] = {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
int stack[144][3];
int g, h, p, dir, top = 1;
stack[top][0] = 1;
stack[top][1] = 1;
stack[top][2] = 1;
while(top>0) {
i = stack[top][0];
j = stack[top][1];
dir = ((stack[top][2])+1);
top = top-1;
while(dir<8) {
g = i + move[dir][0];
h = j + move[dir][1];
if((g==10) && (h==10)) {
for(p=1 ; p<= top ; p++) {
l = stack[p][0];
m = stack[p][1];
}
readimagefile("found.jpg",0,0,1280,853);
getch();
return(0);
}
if( (maze[g][h]==0) && (mark[g][h]==0)) {
mark[g][h] = 1;
top = top+1;
stack[top][0] = i;
stack[top][1] = j;
stack[top][2] = dir;
i = g;
j = h;
setcolor(BLACK);
PrintPath(g, h, 15);
delay(300);
dir = -1;
}
dir = dir+1;
}
setcolor(WHITE);
PrintPath(i, j, 1);
delay(300);
}
readimagefile("notfound.jpg",0,0,1280,853);
getch();
}