-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.c
86 lines (66 loc) · 2.09 KB
/
agent.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <time.h>
#define BOARDSIZE 15
#define EMPTY 0
#define BLACK 1
#define WHITE 2
typedef struct { int8_t x, y; } Position;
typedef struct { int8_t conti; bool empty; } Peeked;
Peeked* peek(int8_t board[], int8_t i, int8_t j, int8_t stone);
Position run(int8_t board[], int8_t stone, double time);
void parse(int8_t board[], int8_t* stone, double* time);
Position run(int8_t board[], int8_t stone, double time) {
Position pos = { 0, 0 };
while (board[pos.y * BOARDSIZE + pos.x] != EMPTY) {
pos.x = rand() % BOARDSIZE;
pos.y = rand() % BOARDSIZE;
}
return pos;
}
int main() {
srand(time(NULL));
int8_t* board = calloc(225, sizeof(int8_t));
int8_t stone = 0;
double time = 0;
parse(board, &stone, &time);
Position selected = run(board, stone, time);
printf("%" PRId8 " %" PRId8 "\n", selected.y, selected.x);
return EXIT_SUCCESS;
}
Peeked* peek(int8_t board[], int8_t i, int8_t j, int8_t stone) {
if (i < 0 || i >= BOARDSIZE || j < 0 || j >= BOARDSIZE) {
exit(EXIT_FAILURE);
}
Peeked* ret = calloc(8, sizeof(Peeked));
int8_t delX = 0, delY = 0;
for (size_t k = 0; k < 8; k++) {
int8_t y = i, x = j;
if (k == 2 || k == 6) delY = 0;
else delY = k > 2 && k < 6 ? 1 : -1;
if (k == 0 || k == 4) delX = 0;
else delX = k > 0 && k < 4 ? 1 : -1;
while (true) {
y += delY;
x += delX;
if (y < 0 || y >= BOARDSIZE) break;
if (x < 0 || x >= BOARDSIZE) break;
if (board[y * BOARDSIZE + x] != stone) break;
(ret + k)->conti++;
}
if (y < 0 || y >= BOARDSIZE) continue;
if (x < 0 || x >= BOARDSIZE) continue;
(ret + k)->empty = board[y * BOARDSIZE + x] == EMPTY;
}
return ret;
}
void parse(int8_t board[], int8_t* stone, double* time) {
for (size_t i = 0; i < 225; i++) {
scanf("%" SCNd8 ", ", board + i);
}
scanf("%" SCNd8 ", %lf", stone, time);
return;
}