-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMyBot.c
177 lines (137 loc) · 4.14 KB
/
MyBot.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
#include "ants.h"
// returns the absolute value of a number; used in distance function
int abs(int x) {
if (x >= 0)
return x;
return -x;
}
// returns the distance between two items on the grid accounting for map wrapping
int distance(int row1, int col1, int row2, int col2, struct game_info *Info) {
int dr, dc;
int abs1, abs2;
abs1 = abs(row1 - row2);
abs2 = Info->rows - abs(row1 - row2);
if (abs1 > abs2)
dr = abs2;
else
dr = abs1;
abs1 = abs(col1 - col2);
abs2 = Info->cols - abs(col1 - col2);
if (abs1 > abs2)
dc = abs2;
else
dc = abs1;
return sqrt(pow(dr, 2) + pow(dc, 2));
}
// sends a move to the tournament engine and keeps track of ants new location
void move(int index, char dir, struct game_state* Game, struct game_info* Info) {
fprintf(stdout, "O %i %i %c\n", Game->my_ants[index].row, Game->my_ants[index].col, dir);
switch (dir) {
case 'N':
if (Game->my_ants[index].row != 0)
Game->my_ants[index].row -= 1;
else
Game->my_ants[index].row = Info->rows - 1;
break;
case 'E':
if (Game->my_ants[index].col != Info->cols - 1)
Game->my_ants[index].col += 1;
else
Game->my_ants[index].col = 0;
break;
case 'S':
if (Game->my_ants[index].row != Info->rows - 1)
Game->my_ants[index].row += 1;
else
Game->my_ants[index].row = 0;
break;
case 'W':
if (Game->my_ants[index].col != 0)
Game->my_ants[index].col -= 1;
else
Game->my_ants[index].col = Info->cols - 1;
break;
}
}
// just a function that returns the string on a given line for i/o
// you don't need to worry about this
char *get_line(char *text) {
char *tmp_ptr = text;
int len = 0;
while (*tmp_ptr != '\n') {
++tmp_ptr;
++len;
}
char *return_str = malloc(len + 1);
memset(return_str, 0, len + 1);
int i = 0;
for (; i < len; ++i) {
return_str[i] = text[i];
}
return return_str;
}
// main, communicates with tournament engine
int main(int argc, char *argv[]) {
int action = -1;
struct game_info Info;
struct game_state Game;
Info.map = 0;
Game.my_ants = 0;
Game.enemy_ants = 0;
Game.food = 0;
Game.dead_ants = 0;
while (42) {
int initial_buffer = 100000;
char *data = malloc(initial_buffer);
memset(data, 0, initial_buffer);
*data = '\n';
char *ins_data = data + 1;
int i = 0;
while (1 > 0) {
++i;
if (i >= initial_buffer) {
initial_buffer *= 2;
data = realloc(data, initial_buffer);
ins_data = data + i;
memset(ins_data, 0, initial_buffer - i);
}
*ins_data = getchar();
if (*ins_data == '\n') {
char *backup = ins_data;
while (*(backup - 1) != '\n') {
--backup;
}
char *test_cmd = get_line(backup);
if (strcmp(test_cmd, "go") == 0) {
action = 0;
free(test_cmd);
break;
}
else if (strcmp(test_cmd, "ready") == 0) {
action = 1;
free(test_cmd);
break;
}
free(test_cmd);
}
++ins_data;
}
if (action == 0) {
char *skip_line = data + 1;
while (*++skip_line != '\n');
++skip_line;
_init_map(skip_line, &Info);
_init_game(&Info, &Game);
do_turn(&Game, &Info);
fprintf(stdout, "go\n");
fflush(stdout);
}
else if (action == 1) {
_init_ants(data + 1, &Info);
Game.my_ant_index = -1;
fprintf(stdout, "go\n");
fflush(stdout);
}
free(data);
}
}