/
mouse.c
executable file
·388 lines (345 loc) · 8.78 KB
/
mouse.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "mouse.h"
#include "movement.h"
#include "sensors.h"
extern int frontWall,rightWall,leftWall;
extern int fr,fl,sr,sl;
extern int gx,gy,gd;
extern int sturning;
#define INVALID -1
typedef enum {
FORWARD,RIGHT,LEFT
} Move;
Move moves[256];
int ind = 0;
int savingPath = 0;
//#define MINIMAZE
void update(Cell maze[][16],int mx,int my,int dir) {
if (frontWall) addFWall(maze,mx,my,dir);
if (rightWall) addRWall(maze,mx,my,dir);
if (leftWall) addLWall(maze,mx,my,dir);
visit(&maze[mx][my]);
}
void flood(Cell maze[][16],Queue *q,int n) {
int d = 0;
int x = 0;
int y = 0;
int n1 = n;
int n2 = 0;
while (!empty(q)) {
floodLevel(n1,d,q);
d++;
n2 = n1;
n1 = 0;
for (;n2 > 0;n2--) { /* Check neighbors of each cell in queue */
x = getx(qfront(q));
y = gety(qfront(q));
if (!(savingPath && !visited(&maze[x][y + 1])) &&
openNorth(maze,x,y) && getDist(&maze[x][y + 1]) == 255 &&
!checked(&maze[x][y + 1])) {
in(q,&maze[x][y + 1]);
n1++;
}
if (!(savingPath && !visited(&maze[x + 1][y])) &&
openEast(maze,x,y) && getDist(&maze[x + 1][y]) == 255 &&
!checked(&maze[x + 1][y])) {
in(q,&maze[x + 1][y]);
n1++;
}
if (!(savingPath && !visited(&maze[x][y - 1])) &&
openSouth(maze,x,y) && getDist(&maze[x][y - 1]) == 255 &&
!checked(&maze[x][y - 1])) {
in(q,&maze[x][y - 1]);
n1++;
}
if (!(savingPath && !visited(&maze[x - 1][y])) &&
openWest(maze,x,y) && getDist(&maze[x - 1][y]) == 255 &&
!checked(&maze[x - 1][y])) {
in(q,&maze[x - 1][y]);
n1++;
}
out(q);
}
}
}
void floodLevel(int n,int d,Queue *q) {
int i;
for (i = 0;i < n;i++) {
setDist(q->contents[(getFront(q) + i) % QSIZE],d);
}
}
void flood2center(Cell maze[][16]) {
Queue q;
int x = 0;
int y = 0;
q_init(&q);
for (x = 0;x < 16;x++)
for (y = 0;y < 16;y++)
clearDist(&maze[x][y]);
#ifndef MINIMAZE
in(&q,&maze[7][8]);
in(&q,&maze[8][8]);
in(&q,&maze[7][7]);
in(&q,&maze[8][7]);
flood(maze,&q,4);
#else
in(&q,&maze[3][3]);
flood(maze,&q,1);
#endif
}
void flood2start(Cell maze[][16]) {
Queue q;
int x = 0;
int y = 0;
q_init(&q);
for (x = 0;x < 16;x++)
for (y = 0;y < 16;y++)
clearDist(&maze[x][y]);
in(&q,&maze[0][0]);
flood(maze,&q,1);
}
void move(Cell maze[][16],int *mx,int *my,int *dir) {
if (goodFront(maze,*mx,*my,*dir)) go(mx,my,*dir);
else if (goodRight(maze,*mx,*my,*dir)) goRight(mx,my,dir);
else if (goodLeft(maze,*mx,*my,*dir)) goLeft(mx,my,dir);
else goBack(mx,my,dir);
}
void turnRight(int *dir) {
virtualRight(dir);
if (!savingPath)
right90();
}
void turnLeft(int *dir) {
virtualLeft(dir);
if (!savingPath) {
left90();
}
}
void virtualRight(int *dir) {
*dir = (*dir + 1) % 4;
}
void virtualLeft(int *dir) {
*dir = (*dir + 3) % 4;
}
void turnAround(int *dir) {
*dir = (*dir + 2) % 4;
aboutFace();
}
void go(int *mx,int *my,int dir) {
virtualForward(mx,my,dir);
if (!savingPath)
cellForward(1);
else
moves[ind] = FORWARD;
}
void virtualForward(int* mx,int* my,int dir) {
switch (dir) {
case NORTH:
*my = *my + 1;
break;
case EAST:
*mx = *mx + 1;
break;
case SOUTH:
*my = *my - 1;
break;
case WEST:
*mx = *mx - 1;
break;
}
}
void goRight(int *mx,int *my,int *dir) {
turnRight(dir);
go(mx,my,*dir);
if (savingPath)
moves[ind] = RIGHT;
}
void goLeft(int *mx,int *my,int *dir) {
turnLeft(dir);
go(mx,my,*dir);
if (savingPath)
moves[ind] = LEFT;
}
void goBack(int *mx,int *my,int *dir) {
turnAround(dir);
go(mx,my,*dir);
}
int goodFront(Cell maze[][16],int mx,int my,int dir) {
switch (dir) {
case NORTH:
return (openNorth(maze,mx,my) && closer(&maze[mx][my],&maze[mx][my + 1]));
break;
case EAST:
return (openEast(maze,mx,my) && closer(&maze[mx][my],&maze[mx + 1][my]));
break;
case SOUTH:
return (openSouth(maze,mx,my) && closer(&maze[mx][my],&maze[mx][my - 1]));
break;
case WEST:
return (openWest(maze,mx,my) && closer(&maze[mx][my],&maze[mx - 1][my]));
break;
default:
return 0;
}
}
int goodRight(Cell maze[][16],int mx,int my,int dir) {
switch (dir) {
case WEST:
return (openNorth(maze,mx,my) && closer(&maze[mx][my],&maze[mx][my + 1]));
break;
case NORTH:
return (openEast(maze,mx,my) && closer(&maze[mx][my],&maze[mx + 1][my]));
break;
case EAST:
return (openSouth(maze,mx,my) && closer(&maze[mx][my],&maze[mx][my - 1]));
break;
case SOUTH:
return (openWest(maze,mx,my) && closer(&maze[mx][my],&maze[mx - 1][my]));
break;
default:
return 0;
}
}
int goodLeft(Cell maze[][16],int mx,int my,int dir) {
switch (dir) {
case EAST:
return (openNorth(maze,mx,my) && closer(&maze[mx][my],&maze[mx][my + 1]));
break;
case SOUTH:
return (openEast(maze,mx,my) && closer(&maze[mx][my],&maze[mx + 1][my]));
break;
case WEST:
return (openSouth(maze,mx,my) && closer(&maze[mx][my],&maze[mx][my - 1]));
break;
case NORTH:
return (openWest(maze,mx,my) && closer(&maze[mx][my],&maze[mx - 1][my]));
break;
default:
return 0;
}
}
int closer(Cell *current,Cell *target) {
return (getDist(target) < getDist(current));
}
int inCenter(int mx,int my) {
#ifndef MINIMAZE
return ((mx == 7 || mx == 8) && (my == 7 || my == 8));
#else
return (mx == 3 && my == 3);
#endif
}
int inStart(int mx,int my) {
return (mx == 0 && my == 0);
}
void savePath(Cell maze[][16]) {
int x,y,dir;
int i;
for (i = 0;i < 256;i++)
moves[i] = INVALID;
x = y = 0;
dir = NORTH;
savingPath = 1;
flood2center(maze);
for (ind = 0;!inCenter(x,y);ind++) {
move(maze,&x,&y,&dir);
}
savingPath = 0;
}
void executePath() {
int i,j;
double n;
for (i = 0;moves[i] != INVALID;i++) {
switch (moves[i]) {
case FORWARD:
n = 1;
if (i > 0 && moves[i - 1] != FORWARD){// coming out of turn
if (sturning)
n += 0.5;
else
n += 1;
}
for (j = i + 1;moves[j] == FORWARD;i++,j++,n++);
if (moves[i + 1] != INVALID && sturning)// approaching turn
n -= 0.5;
cellForward(n);
break;
case RIGHT:
if (sturning) {
sRight();
}
else {
if (moves[i - 1] != FORWARD) {
cellForward(1);
}
right90();
}
break;
case LEFT:
if (sturning) {
sLeft();
}
else {
if (moves[i - 1] != FORWARD) {
cellForward(1);
}
left90();
}
break;
default:
break;
}
}
if (sturning && moves[i-1]!=FORWARD)
cellForward(0.5);
}
void closeCenter(Cell maze[][16],int x,int y,int dir) {
switch (dir) {
case NORTH:
addLWall(maze,7,7,dir);
addLWall(maze,7,8,dir);
addFWall(maze,7,8,dir);
addFWall(maze,8,8,dir);
addRWall(maze,8,8,dir);
addRWall(maze,8,7,dir);
if (x == 7)
addFWall(maze,8,6,dir);
else
addFWall(maze,7,6,dir);
break;
case EAST:
addLWall(maze,7,8,dir);
addLWall(maze,8,8,dir);
addFWall(maze,8,8,dir);
addFWall(maze,8,7,dir);
addRWall(maze,8,7,dir);
addRWall(maze,7,7,dir);
if (y == 7)
addFWall(maze,6,8,dir);
else
addFWall(maze,6,7,dir);
break;
case SOUTH:
addLWall(maze,8,8,dir);
addLWall(maze,8,7,dir);
addFWall(maze,8,7,dir);
addFWall(maze,7,7,dir);
addRWall(maze,7,7,dir);
addRWall(maze,7,8,dir);
if (x == 7)
addFWall(maze,8,9,dir);
else
addFWall(maze,7,9,dir);
break;
case WEST:
addLWall(maze,8,7,dir);
addLWall(maze,7,7,dir);
addFWall(maze,7,7,dir);
addFWall(maze,7,8,dir);
addRWall(maze,7,8,dir);
addRWall(maze,8,8,dir);
if (y == 7)
addFWall(maze,9,8,dir);
else
addFWall(maze,9,7,dir);
break;
}
}