-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze.c
110 lines (102 loc) · 3.76 KB
/
Maze.c
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
#include <stdio.h>
#include <malloc.h>
#include <windows.h>
typedef struct node{
int x, y;
struct node *link;
}stack;
void PUSH(stack **move, int x, int y);
void POP(stack **move);
void ALL_POP(stack **move);
void gotoxy(int x, int y);
void setcolor(int color, int bgcolor);
int main() {
int i, j;
int map[10][10] = {
{1,0,1,0,1,1,1,1,1,1},
{1,0,1,0,1,1,1,0,1,1},
{1,0,1,0,1,1,0,0,0,1},
{1,0,1,0,0,0,1,1,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,1,0,1,1,1},
{1,1,1,0,0,0,0,1,1,1},
{1,0,0,0,1,0,1,1,0,1},
{1,0,1,1,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
stack *move = NULL;
for(i=0; i<10; i++){ //map
for(j=0; j<10; j++){
if(map[i][j]==1){printf("■");}
else{printf(" ");}
}
printf("\n");
}
PUSH(&move, 2, 0); // initial starting point
gotoxy(move->x, move->y); //move to where the stack position
setcolor(10, 0);
printf("Ω");
while(1){
Sleep(200);
gotoxy(move->x, move->y);
printf(" ");
map[move->y][move->x/2] = 2; //change it it already pased it
if(map[0][3]==2){ //get off from the maze
ALL_POP(&move); //pop all
gotoxy(12,12);
printf("You are Free!\n");
system("pause");
return;
}
else if(map[move->y+1][move->x/2]==0){ //move downward
PUSH(&move,move->x,move->y+1);
gotoxy(move->x, move->y);
printf("Ω");
}
else if(map[move->y-1][move->x/2]==0){ //move upward
PUSH(&move,move->x,move->y-1);
gotoxy(move->x, move->y);
printf("Ω");
}
else if(map[move->y][move->x/2+1]==0){ //rightward
PUSH(&move,move->x+2,move->y);
gotoxy(move->x, move->y);
printf("Ω");
}
else if(map[move->y][move->x/2-1]==0){ //leftward
PUSH(&move,move->x-2,move->y);
gotoxy(move->x, move->y);
printf("Ω");
}
else{ //if its blocked
POP(&move); //pop current position and move to the one before
gotoxy(move->x, move->y);
printf("Ω");
}
}
return 0;
}
void PUSH(stack **move, int x, int y){
stack *tmp = *move;
*move = (stack*)malloc(sizeof(stack));
(*move)->x = x;
(*move)->y = y;
(*move)->link = tmp;
}
void POP(stack **move){
stack *tmp = *move;
*move = (*move)->link;
free(tmp);
}
void ALL_POP(stack **move){
for(;*move!=NULL;){POP(move);}
}
void gotoxy(int x, int y){
COORD Pos={x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Pos);
}
void setcolor(int color, int bgcolor){
color &= 0xf;
bgcolor &= 0xf;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (bgcolor << 4) | color);
}