-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
170 lines (155 loc) · 4.58 KB
/
main.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
char temp[100]; // ok
int PlayerPos=1; // ok
int W =21; // ok
int H = 11; // ok
int startX =1; // ok
int TotalElements = 231; // ok
char move; // var to take the move that the user want to do
char map[232]= "I.IIIIIIIIIIIIIIIIIII"
"I....I....I.......I.I"
"III.IIIII.I.I.III.I.I"
"I.I.....I..I..I.....I"
"I.I.III.II...II.I.III"
"I...I...III.I...I...I"
"IIIII.IIIII.III.III.I"
"I.............I.I...I"
"IIIIIIIIIIIIIII.I.III"
"@...............I..II"
"IIIIIIIIIIIIIIIIIIIII";
//functions needed
void printLabyrinth (void) // searches based on playerPosition and just puts the P sign where the player is
{
int i,j,k=0;
usleep(200000);
printf("Labyrinth:\n");
for (i=0; i<H; i++)
{
for(j=0; j<W; j++)
{
if(k==PlayerPos)
temp[j]='P';
else
temp[j]=map[k];
k++;
}
temp[j+1]='\0';
printf("%s\n", temp);
}
}
int makeMove(int index)// finds the optimal path through trying every path possible by the following algorithm
{
if(index<0 || index>=TotalElements)
return 0;
if(map[index]=='.')
{
map[index]='*';
printLabyrinth();
if(makeMove(index+1)==1)
{
map[index]='#';
printLabyrinth();
return 1;
}
if(makeMove(index+W)==1)
{
map[index]='#';
printLabyrinth();
return 1;
}
if(makeMove(index-1)==1)
{
map[index]='#';
printLabyrinth();
return 1;
}
if(makeMove(index-W)==1)
{
map[index]='#';
printLabyrinth();
return 1;
}
}
else if (map[index]=='@')
{
map[index]='%';
printLabyrinth();
return 1;
}
return 0; // ends the program cause its pointless to begin again having the optimal path already
}
void findPath(void)
{
while(move !='e' && map[PlayerPos] != '@')
{
printf("where you wanna go ");
move = getchar();
switch(move)
{
case 'w':
if(map[PlayerPos - W]=='.')
{
PlayerPos = PlayerPos - W;
printLabyrinth();
}
else if(map[PlayerPos-W] == '@')
printf("Winner Winner Chicken Dinner");
else
{
printf("You cannot do that move\n");
}
break;
case 's':
if(map[PlayerPos + W]=='.')
{
PlayerPos = PlayerPos + W;
printLabyrinth();
}
else if(map[PlayerPos+W] == '@')
printf("Winner Winner Chicken Dinner");
else
{
printf("You cannot do that move\n");
}
break;
case 'a':
if(map[PlayerPos - 1]=='.')
{
PlayerPos = PlayerPos - 1;
printLabyrinth();
}
else if(map[PlayerPos-1] == '@')
{
printf("Winner Winner Chicken Dinner");
}
else
printf("You cannot do that move\n");
break;
case 'd':
if(map[PlayerPos + 1]=='.')
{
PlayerPos = PlayerPos + 1;
printLabyrinth();
}
else if(map[PlayerPos+1] == '@')
printf("Winner Winner Chicken Dinner");
else
{
printf("You cannot do that move\n");
}
break;
}
}
}
int main()
{
printLabyrinth();
findPath();
if(move == 'e')
{
printLabyrinth();
startX = makeMove(PlayerPos);
}
}