-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobots_bsd.c
More file actions
512 lines (435 loc) · 12 KB
/
robots_bsd.c
File metadata and controls
512 lines (435 loc) · 12 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//******************************************************************************
#include "robots_bsd.h"
#include "robots.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define TO_X 0
#define TO_N 1
#define TO_NW 2
#define TO_W 3
#define TO_SW 4
#define TO_S 5
#define TO_SE 6
#define TO_E 7
#define TO_NE 8
//******************************************************************************
// >>> bsd code
#define ROBOTS_MAX 32
typedef struct
{
int y, x;
} position;
position player;
position robot[ROBOTS_MAX];
position junk[ROBOTS_MAX];
int robots;
int junks;
const char* valid;
//******************************************************************************
int sign(int n)
{
if (n < 0)
return -1;
else if (n > 0)
return 1;
else
return 0;
}
#define ABS(a) (((a)>0)?(a):-(a))
#define MIN(a,b) (((a)>(b))?(b):(a))
#define MAX(a,b) (((a)<(b))?(b):(a))
// return "move" number distance of the two coordinates
static int distance(int x1, int y1, int x2, int y2)
{
return MAX(ABS(ABS(x1) - ABS(x2)), ABS(ABS(y1) - ABS(y2)));
}
//******************************************************************************
// return x coordinate moves
static int x_inc(int dir)
{
switch(dir)
{
case ROBOTS_KEY_NE:
case ROBOTS_KEY_E:
case ROBOTS_KEY_SE:
return 1;
case ROBOTS_KEY_N:
case ROBOTS_KEY_X:
case ROBOTS_KEY_S:
return 0;
case ROBOTS_KEY_NW:
case ROBOTS_KEY_W:
case ROBOTS_KEY_SW:
return -1;
default:
assert(0);
}
}
// return y coordinate moves
static int y_inc(int dir)
{
switch(dir)
{
case ROBOTS_KEY_SW:
case ROBOTS_KEY_S:
case ROBOTS_KEY_SE:
return 1;
case ROBOTS_KEY_W:
case ROBOTS_KEY_X:
case ROBOTS_KEY_E:
return 0;
case ROBOTS_KEY_NW:
case ROBOTS_KEY_N:
case ROBOTS_KEY_NE:
return -1;
default:
assert(0);
}
}
//******************************************************************************
// find possible moves
static const char* find_moves()
{
static char ans[10];
char *p = ans;
if (valid[TO_X])
*p++ = ROBOTS_KEY_X;
if (valid[TO_W])
*p++ = ROBOTS_KEY_W;
if (valid[TO_S])
*p++ = ROBOTS_KEY_S;
if (valid[TO_N])
*p++ = ROBOTS_KEY_N;
if (valid[TO_E])
*p++ = ROBOTS_KEY_E;
if (valid[TO_NW])
*p++ = ROBOTS_KEY_NW;
if (valid[TO_NE])
*p++ = ROBOTS_KEY_NW;
if (valid[TO_SW])
*p++ = ROBOTS_KEY_SW;
if (valid[TO_SE])
*p++ = ROBOTS_KEY_SE;
if (p == ans)
*p++ = ROBOTS_KEY_TELE;
*p = '\0';
return ans;
}
//******************************************************************************
// return the robot closest to us and put in dist its distance
static position* closest_robot(int *dist)
{
position *minrob = NULL;
int mindist = 1000000;
for (position *rob = robot; rob < &robot[robots]; rob++)
{
int tdist = distance(player.x, player.y, rob->x, rob->y);
if (tdist < mindist)
{
minrob = rob;
mindist = tdist;
}
}
*dist = mindist;
return minrob;
}
// return the heap closest to us and put in dist its distance
static position *closest_junk(int *dist)
{
position *minhp = NULL;
int mindist = 1000000;
for (position *hp = junk; hp < &junk[ROBOTS_MAX]; hp++)
{
if (hp->x == 0 && hp->y == 0)
break;
int tdist = distance(player.x, player.y, hp->x, hp->y);
if (tdist < mindist)
{
minhp = hp;
mindist = tdist;
}
}
*dist = mindist;
return minhp;
}
//******************************************************************************
// move as close to the given direction as possible
static int move_towards(int dx, int dy)
{
char valid_moves[10], best_move;
strcpy(valid_moves, find_moves());
best_move = valid_moves[0];
if (best_move != ROBOTS_KEY_TELE)
{
int mv_x = x_inc(best_move);
int mv_y = y_inc(best_move);
int move_judge = ABS(mv_x - dx) + ABS(mv_y - dy);
for (char *ptr = &valid_moves[1]; *ptr != '\0'; ptr++)
{
mv_x = x_inc(*ptr);
mv_y = y_inc(*ptr);
int cur_judge = ABS(mv_x - dx) + ABS(mv_y - dy);
if (cur_judge < move_judge)
{
move_judge = cur_judge;
best_move = *ptr;
}
}
}
return best_move;
}
// move away form the robot given
static int move_away(position *rob)
{
int dx = sign(player.x - rob->x);
int dy = sign(player.y - rob->y);
return move_towards(dx, dy);
}
//******************************************************************************
// move the closest heap between us and the closest robot
static int move_between(position *rob, position *hp)
{
int dx;
int dy;
// equation of the line between us and the closest robot
if (player.x == rob->x)
{
// me and the robot are aligned in x
// change my x so I get closer to the heap
// and my y far from the robot
dx = -sign(player.x - hp->x);
dy = sign(player.y - rob->y);
}
else if (player.y == rob->y)
{
// me and the robot are aligned in y
// change my y so I get closer to the heap
// and my x far from the robot
dx = sign(player.x - rob->x);
dy = -sign(player.y - hp->y);
}
else
{
float slope = (player.y - rob->y) / (player.x - rob->x);
float cons = slope * rob->y;
if (ABS(player.x - rob->x) > ABS(player.y - rob->y))
{
// we are closest to the robot in x
// move away from the robot in x and
// close to the junk in y
dx = sign(player.x - rob->x);
dy = sign(((slope * ((float) hp->x)) + cons) - ((float) hp->y));
}
else
{
dx = sign(((slope * ((float) hp->x)) + cons) - ((float) hp->y));
dy = sign(player.y - rob->y);
}
}
return move_towards(dx, dy);
}
//******************************************************************************
// return true if the heap is between us and the robot
static int between(position *rob, position *hp)
{
// I = @
if (hp->x > rob->x && player.x < rob->x)
return 0;
// @ = I
if (hp->x < rob->x && player.x > rob->x)
return 0;
// @
// =
// I
if (hp->y < rob->y && player.y > rob->y)
return 0;
// I
// =
// @
if (hp->y > rob->y && player.y < rob->y)
return 0;
return 1;
}
//******************************************************************************
// find and do the best move if flag else get the first move
static int automove()
{
int robot_dist;
position *robot_close = closest_robot(&robot_dist);
if (robot_dist > 1)
return ROBOTS_KEY_X;
if (!junks)
// no junk heaps just run away
return move_away(robot_close);
int heap_dist;
position *heap_close = closest_junk(&heap_dist);
int robot_heap = distance(robot_close->x, robot_close->y,
heap_close->x, heap_close->y);
if (robot_heap <= heap_dist && !between(robot_close, heap_close))
// robot is closest to us from the heap. Run away!
return move_away(robot_close);
return move_between(robot_close, heap_close);
}
// <<<
//******************************************************************************
// >>> validity
static const char* validity(const char board[SCREEN_ROWS][SCREEN_COLS], int row, int col)
{
static char valid[9];
valid[TO_X] = valid[TO_N] = valid[TO_NW] = valid[TO_W] = valid[TO_SW]
= valid[TO_S] = valid[TO_SE] = valid[TO_E] = valid[TO_NE] = 1;
// borders
if (row == 0)
valid[TO_N] = valid[TO_NW] = valid[TO_NE] = 0;
if (row == SCREEN_ROWS - 1)
valid[TO_SW] = valid[TO_S] = valid[TO_SE] = 0;
if (col == 0)
valid[TO_NW] = valid[TO_W] = valid[TO_SW] = 0;
if (col == SCREEN_COLS - 1)
valid[TO_SE] = valid[TO_E] = valid[TO_NE] = 0;
// 8 primary neighbors
if (row > 0)
{
if (board[row - 1][col] == ROBOTS_ROBOT)
valid[TO_X] = valid[TO_N] = valid[TO_NW] = valid[TO_W]
= valid[TO_E] = valid[TO_NE] = 0;
else if (board[row - 1][col] == ROBOTS_JUNK)
valid[TO_N] = 0;
}
if (row > 0 && col > 0)
{
if (board[row - 1][col - 1] == ROBOTS_ROBOT)
valid[TO_X] = valid[TO_N] = valid[TO_NW] = valid[TO_W] = 0;
else if (board[row - 1][col - 1] == ROBOTS_JUNK)
valid[TO_NW] = 0;
}
if (col > 0)
{
if (board[row][col - 1] == ROBOTS_ROBOT)
valid[TO_X] = valid[TO_N] = valid[TO_NW] = valid[TO_W]
= valid[TO_SW] = valid[TO_S] = 0;
else if (board[row][col - 1] == ROBOTS_JUNK)
valid[TO_W] = 0;
}
if (row < SCREEN_ROWS - 1 && col > 0)
{
if (board[row + 1][col - 1] == ROBOTS_ROBOT)
valid[TO_X] = valid[TO_W] = valid[TO_SW] = valid[TO_S] = 0;
else if (board[row + 1][col - 1] == ROBOTS_JUNK)
valid[TO_SW] = 0;
}
if (row < SCREEN_ROWS - 1)
{
if (board[row + 1][col] == ROBOTS_ROBOT)
valid[TO_X] = valid[TO_W] = valid[TO_SW] = valid[TO_S]
= valid[TO_SE] = valid[TO_E] = 0;
else if (board[row + 1][col] == ROBOTS_JUNK)
valid[TO_S] = 0;
}
if (row < SCREEN_ROWS - 1 && col < SCREEN_COLS - 1)
{
if (board[row + 1][col + 1] == ROBOTS_ROBOT)
valid[TO_X] = valid[TO_S] = valid[TO_SE] = valid[TO_E] = 0;
else if (board[row + 1][col + 1] == ROBOTS_JUNK)
valid[TO_SE] = 0;
}
if (col < SCREEN_COLS - 1)
{
if (board[row][col + 1] == ROBOTS_ROBOT)
valid[TO_X] = valid[TO_N] = valid[TO_S] = valid[TO_SE]
= valid[TO_E] = valid[TO_NE] = 0;
else if (board[row][col + 1] == ROBOTS_JUNK)
valid[TO_E] = 0;
}
if (row > 0 && col < SCREEN_COLS - 1)
{
if (board[row - 1][col + 1] == ROBOTS_ROBOT)
valid[TO_X] = valid[TO_N] = valid[TO_E] = valid[TO_NE] = 0;
else if (board[row - 1][col + 1] == ROBOTS_JUNK)
valid[TO_NE] = 0;
}
// 16 secondary neightbors
if (row > 1
&& board[row - 2][col] == ROBOTS_ROBOT)
valid[TO_N] = valid[TO_NW] = valid[TO_NE] = 0;
if (row > 1 && col > 0
&& board[row - 2][col - 1] == ROBOTS_ROBOT)
valid[TO_N] = valid[TO_NW] = 0;
if (row > 1 && col > 1
&& board[row - 2][col - 2] == ROBOTS_ROBOT)
valid[TO_NW] = 0;
if (row > 0 && col > 1
&& board[row - 1][col - 2] == ROBOTS_ROBOT)
valid[TO_NW] = valid[TO_W] = 0;
if (col > 1
&& board[row][col - 2] == ROBOTS_ROBOT)
valid[TO_NW] = valid[TO_W] = valid[TO_SW] = 0;
if (row < SCREEN_ROWS - 1 && col > 1
&& board[row + 1][col - 2] == ROBOTS_ROBOT)
valid[TO_W] = valid[TO_SW] = 0;
if (row < SCREEN_ROWS - 2 && col > 1
&& board[row + 2][col - 2] == ROBOTS_ROBOT)
valid[TO_SW] = 0;
if (row < SCREEN_ROWS - 2 && col > 0
&& board[row + 2][col - 1] == ROBOTS_ROBOT)
valid[TO_SW] = valid[TO_S] = 0;
if (row < SCREEN_ROWS - 2
&& board[row + 2][col] == ROBOTS_ROBOT)
valid[TO_SW] = valid[TO_S] = valid[TO_SE] = 0;
if (row < SCREEN_ROWS - 2 && col < SCREEN_COLS - 1
&& board[row + 2][col + 1] == ROBOTS_ROBOT)
valid[TO_S] = valid[TO_SE] = 0;
if (row < SCREEN_ROWS - 2 && col < SCREEN_COLS - 2
&& board[row + 2][col + 2] == ROBOTS_ROBOT)
valid[TO_SE] = 0;
if (row < SCREEN_ROWS - 1 && col < SCREEN_COLS - 2
&& board[row + 1][col + 2] == ROBOTS_ROBOT)
valid[TO_SE] = valid[TO_E] = 0;
if (col < SCREEN_COLS - 2
&& board[row][col + 2] == ROBOTS_ROBOT)
valid[TO_SE] = valid[TO_E] = valid[TO_NE] = 0;
if (row > 0 && col < SCREEN_COLS - 2
&& board[row - 1][col + 2] == ROBOTS_ROBOT)
valid[TO_E] = valid[TO_NE] = 0;
if (row > 1 && col < SCREEN_COLS - 2
&& board[row - 2][col + 2] == ROBOTS_ROBOT)
valid[TO_NE] = 0;
if (row > 1 && col < SCREEN_COLS - 1
&& board[row - 2][col + 1] == ROBOTS_ROBOT)
valid[TO_N] = valid[TO_NE] = 0;
return valid;
}
// <<<
//******************************************************************************
// >>> strategy
int robots_strategy_bsd(const char board[SCREEN_ROWS][SCREEN_COLS])
{
robots = 0;
junks = 0;
for (int row = 0; row < SCREEN_ROWS; row++)
for (int col = 0; col < SCREEN_COLS; col++)
{
if (board[row][col] == ROBOTS_PLAYER)
{
player.x = col;
player.y = row;
}
else if (board[row][col] == ROBOTS_ROBOT)
{
robot[robots].x = col;
robot[robots].y = row;
robots++;
}
else if (board[row][col] == ROBOTS_JUNK)
{
junk[junks].x = col;
junk[junks].y = row;
junks++;
}
}
valid = validity(board, player.y, player.x);
return automove();
}
// <<<
//******************************************************************************