-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.c
More file actions
364 lines (310 loc) · 10.2 KB
/
robot.c
File metadata and controls
364 lines (310 loc) · 10.2 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "headers/constants.h"
#include "headers/map.h"
#include "headers/robot.h"
/*
* Create a new robot
*
* @return Robot structure
*/
Robot new_robot(char name, int speed) {
Robot bot;
bot.name = name;
bot.speed = speed;
bot.posX = ROBOT_DEFAULT_X;
bot.posY = ROBOT_DEFAULT_Y;
bot.is_stuck = 0;
bot.moves = 0;
bot.direction = 0;
bot.out = 0;
return bot;
}
/*
* Move the robot on the map
* by calculating the availables moves
* regarding the exit coordinates of the map
*
* @return Robot structure containing the updated position of the robot
*/
Robot move_robot(Map map, Robot bot, Exit exit) {
// Checking in which direction we should go...
int horizontalMove = 0;
int verticalMove = 0;
// Horizontal
if (bot.posX == exit.x) horizontalMove = -1;
else if (bot.posX > exit.x) horizontalMove = LEFT;
else horizontalMove = RIGHT;
// Vertical
if (bot.posY == exit.y) verticalMove = -1;
else if (bot.posY > exit.y) verticalMove = TOP;
else verticalMove = BOTTOM;
// Now verifying where we can move...
int availableMoves[4] = {0};
int is_stuck = 1;
// Top
if (
map.map[bot.posY - bot.speed][bot.posX] != 'x'
&& bot.m[bot.posY - bot.speed][bot.posX] != 1
&& !will_be_stuck(map, bot, TOP)
//&& !is_corridor(map, bot, TOP)
) { availableMoves[TOP] = 1; is_stuck = 0; }
// Right
if (
map.map[bot.posY][bot.posX + bot.speed] != 'x'
&& bot.m[bot.posY][bot.posX + bot.speed] != 1
&& !will_be_stuck(map, bot, RIGHT)
//&& !is_corridor(map, bot, RIGHT)
) { availableMoves[RIGHT] = 1; is_stuck = 0; }
// Bottom
if (
map.map[bot.posY + bot.speed][bot.posX] != 'x'
&& bot.m[bot.posY + bot.speed][bot.posX] != 1
&& !will_be_stuck(map, bot, BOTTOM)
//&& !is_corridor(map, bot, BOTTOM)
) { availableMoves[BOTTOM] = 1; is_stuck = 0; }
// Left
if (
map.map[bot.posY][bot.posX - bot.speed] != 'x'
&& bot.m[bot.posY][bot.posX - bot.speed] != 1
&& !will_be_stuck(map, bot, LEFT)
//&& !is_corridor(map, bot, LEFT)
) { availableMoves[LEFT] = 1; is_stuck = 0; }
bot.is_stuck = is_stuck;
printf("\nAvailable moves: %d, %d, %d, %d\n",
availableMoves[TOP], availableMoves[RIGHT], availableMoves[BOTTOM], availableMoves[LEFT]);
printf("Is stuck: %d\n", bot.is_stuck);
printf("Moves: %d, %d\n", horizontalMove, verticalMove);
fflush(stdout);
// Now chosing the best move...
// Starting by the directions where we should go
if (horizontalMove != -1 && availableMoves[horizontalMove] == 1) {
printf("Horizontal Move: OK\n");
printf("Random: NONE\n");
fflush(stdout);
switch (horizontalMove) {
case RIGHT:
bot.posX++;
bot.direction = RIGHT;
break;
case LEFT:
bot.posX--;
bot.direction = LEFT;
break;
}
}
else if (verticalMove != -1 && availableMoves[verticalMove] == 1) {
printf("Vertical Move: OK\n");
printf("Random: NONE\n");
fflush(stdout);
switch (verticalMove) {
case TOP:
bot.posY--;
bot.direction = TOP;
break;
case BOTTOM:
bot.posY++;
bot.direction = BOTTOM;
break;
}
}
// Doing it randomly
else {
printf("Random: OK\n");
fflush(stdout);
int randomMove = 0, search = 1, firstLap = 1;
// Searching for a good move...
while (search) {
randomMove = (rand() % 4);
if (firstLap) randomMove = bot.direction;
switch (randomMove) {
case TOP:
if (
map.map[bot.posY - bot.speed][bot.posX] != 'x'
&& bot.m[bot.posY - bot.speed][bot.posX] != 1
&& !will_be_stuck(map, bot, TOP)
//&& !is_corridor(map, bot, TOP)
) { search = 0; bot.direction = TOP; printf("Random move: TOP\n"), fflush(stdout); }
else if (
map.map[bot.posY - bot.speed][bot.posX] != 'x'
//&& !will_be_stuck(map, bot, TOP)
//&& !is_corridor(map, bot, TOP)
&& bot.is_stuck == 1
) { search = 0; bot.direction = TOP; printf("Random move: TOP\n"), fflush(stdout); }
break;
case RIGHT:
if (
map.map[bot.posY][bot.posX + bot.speed] != 'x'
&& bot.m[bot.posY][bot.posX + bot.speed] != 1
&& !will_be_stuck(map, bot, RIGHT)
//&& !is_corridor(map, bot, RIGHT)
) { search = 0; bot.direction = RIGHT; printf("Random move: RIGHT\n"), fflush(stdout); }
else if (
map.map[bot.posY][bot.posX + bot.speed] != 'x'
//&& !will_be_stuck(map, bot, RIGHT)
//&& !is_corridor(map, bot, RIGHT)
&& bot.is_stuck == 1
) { search = 0; bot.direction = RIGHT; printf("Random move: RIGHT\n"), fflush(stdout); }
break;
case BOTTOM:
if (
map.map[bot.posY + bot.speed][bot.posX] != 'x'
&& bot.m[bot.posY + bot.speed][bot.posX] != 1
&& !will_be_stuck(map, bot, BOTTOM)
//&& !is_corridor(map, bot, BOTTOM)
) { search = 0; bot.direction = BOTTOM; printf("Random move: BOTTOM\n"), fflush(stdout); }
else if (
map.map[bot.posY + bot.speed][bot.posX] != 'x'
//&& !will_be_stuck(map, bot, BOTTOM)
//&& !is_corridor(map, bot, BOTTOM)
&& bot.is_stuck == 1
) { search = 0; bot.direction = BOTTOM; printf("Random move: BOTTOM\n"), fflush(stdout); }
break;
case LEFT:
if (
map.map[bot.posY][bot.posX - bot.speed] != 'x'
&& bot.m[bot.posY][bot.posX - bot.speed] != 1
&& !will_be_stuck(map, bot, LEFT)
//&& !is_corridor(map, bot, LEFT)
) { search = 0; bot.direction = LEFT; printf("Random move: LEFT\n"), fflush(stdout); }
else if (
map.map[bot.posY][bot.posX - bot.speed] != 'x'
//&& !will_be_stuck(map, bot, LEFT)
//&& !is_corridor(map, bot, LEFT)
&& bot.is_stuck == 1
) { search = 0; bot.direction = LEFT; printf("Random move: LEFT\n"), fflush(stdout); }
break;
}
firstLap = 0;
}
// And doing it!
switch (randomMove) {
case TOP:
bot.posY--;
break;
case RIGHT:
bot.posX++;
break;
case BOTTOM:
bot.posY++;
break;
case LEFT:
bot.posX--;
break;
}
}
// Remembering the position
bot.m[bot.posY][bot.posX] = 1;
bot.moves++;
printf("Exit: %dx%d\n", exit.y, exit.x);
printf("#moves: %d\n", bot.moves);
// Is the bot out?
if (map.map[bot.posY][bot.posX] == 'S') {
bot.out = 1;
}
return bot;
}
/*
* Check if the next move in the given direction
* will stuck the robot
*
* @return int 0 = not stuck, 1 = stuck
*/
int will_be_stuck(Map map, Robot bot, int direction) {
int stuck = 0;
if (
map.map[bot.posY][bot.posX + 1] == 'S'
|| map.map[bot.posY + 1][bot.posX] == 'S'
|| map.map[bot.posY][bot.posX + 1] == 'S'
|| map.map[bot.posY - 1][bot.posX] == 'S'
) return stuck;
switch (direction) {
case TOP:
if (
(map.map[bot.posY - bot.speed - 1][bot.posX] == 'x'
|| bot.m[bot.posY - bot.speed - 1][bot.posX] == 1)
&& (map.map[bot.posY - bot.speed][bot.posX - bot.speed] == 'x'
|| bot.m[bot.posY - bot.speed][bot.posX - bot.speed] == 1)
&& (map.map[bot.posY - bot.speed][bot.posX + bot.speed] == 'x'
|| bot.m[bot.posY - bot.speed][bot.posX + bot.speed] == 1)
) stuck = 1;
break;
case RIGHT:
if (
(map.map[bot.posY][bot.posX + bot.speed + 1] == 'x'
|| bot.m[bot.posY][bot.posX + bot.speed + 1] == 1)
&& (map.map[bot.posY - 1][bot.posX + bot.speed] == 'x'
|| bot.m[bot.posY - 1][bot.posX + bot.speed] == 1)
&& (map.map[bot.posY + 1][bot.posX + bot.speed] == 'x'
|| bot.m[bot.posY + 1][bot.posX + bot.speed] == 1)
) stuck = 1;
break;
case BOTTOM:
if (
(map.map[bot.posY + bot.speed + 1][bot.posX] == 'x'
|| bot.m[bot.posY + bot.speed + 1][bot.posX] == 1)
&& (map.map[bot.posY + bot.speed][bot.posX - bot.speed] == 'x'
|| bot.m[bot.posY + bot.speed][bot.posX - bot.speed] == 1)
&& (map.map[bot.posY + bot.speed][bot.posX + bot.speed] == 'x'
|| bot.m[bot.posY + bot.speed][bot.posX + bot.speed] == 1)
) stuck = 1;
break;
case LEFT:
if (
(map.map[bot.posY][bot.posX - bot.speed - 1] == 'x'
|| bot.m[bot.posY][bot.posX - bot.speed - 1] == 1)
&& (map.map[bot.posY - 1][bot.posX - bot.speed] == 'x'
|| bot.m[bot.posY - 1][bot.posX - bot.speed] == 1)
&& (map.map[bot.posY + 1][bot.posX - bot.speed] == 'x'
|| bot.m[bot.posY + 1][bot.posX - bot.speed] == 1)
) stuck = 1;
break;
}
return stuck;
}
/*
* Check if the next move in the given direction
* will engage the bot in a corridor
*
* @return int 0 = not a corridor, 1 = corridor
* @deprecated
*/
int is_corridor(Map map, Robot bot, int direction) {
int corridor = 0;
switch (direction) {
case TOP:
if (
(bot.m[bot.posY - bot.speed][bot.posX - bot.speed] == 1
&& bot.m[bot.posY - bot.speed][bot.posX + bot.speed] == 1)
|| (bot.m[bot.posY - bot.speed - 1][bot.posX - bot.speed] == 1
&& bot.m[bot.posY - bot.speed - 1][bot.posX + bot.speed] == 1)
) corridor = 1;
break;
case RIGHT:
if (
(bot.m[bot.posY - 1][bot.posX + bot.speed] == 1
&& bot.m[bot.posY + 1][bot.posX + bot.speed] == 1)
|| (bot.m[bot.posY - 1][bot.posX + bot.speed + 1] == 1
&& bot.m[bot.posY + 1][bot.posX + bot.speed + 1] == 1)
) corridor = 1;
break;
case BOTTOM:
if (
(bot.m[bot.posY + bot.speed][bot.posX - bot.speed] == 1
&& bot.m[bot.posY + bot.speed][bot.posX + bot.speed] == 1)
|| (bot.m[bot.posY + bot.speed + 1][bot.posX - bot.speed] == 1
&& bot.m[bot.posY + bot.speed + 1][bot.posX + bot.speed] == 1)
) corridor = 1;
break;
case LEFT:
if (
(bot.m[bot.posY - 1][bot.posX - bot.speed] == 1
&& bot.m[bot.posY + 1][bot.posX - bot.speed] == 1)
|| (bot.m[bot.posY - 1][bot.posX - bot.speed - 1] == 1
&& bot.m[bot.posY + 1][bot.posX - bot.speed - 1] == 1)
) corridor = 1;
break;
}
return corridor;
}