-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAIDemo.cc
326 lines (309 loc) · 8.01 KB
/
AIDemo.cc
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
#include "Player.hh"
/**
* Write the name of your player and save this file
* with the same name and .cc extension.
*/
#define PLAYER_NAME Vox
// DISCLAIMER: The following Demo player is *not* meant to do anything
// sensible. It is provided just to illustrate how to use the API.
// Please use AINull.cc as a template for your player.
struct PLAYER_NAME : public Player {
/**
* Factory: returns a new instance of this class.
* Do not modify this function.
*/
static Player* factory () {
return new PLAYER_NAME;
}
/**
* Types and attributes for your player can be defined here.
*/
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> ii;
int dx[9] = {1, 1, 0, -1, -1, -1, 0, 1, 0};
int dy[9] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
map<int, int> kind;
VVI board; //tauler
VVI dist_w; //distancia a aigua
VVI dist_c; //distancia a ciutat
VVI dist_uc; //distancia a ciutat no conquerida
VVI dist_a; //distancia per ser atropellat
VVI dist_f; //distancia a fuel
VVI dist_u; //distancia a una victima
bool valid(int i, int j) {
if (i < 0 or j < 0) return false;
if (i >= rows() or j >= cols()) return false;
return true;
}
bool passable_w(int i, int j) {
if (!valid(i, j)) return false;
if (board[i][j] == Desert) return true;
if (board[i][j] == Road) return true;
if (board[i][j] == City) return true;
return false;
}
bool passable_c(int i, int j) {
if (!valid(i, j)) return false;
if (board[i][j] == Desert) return true;
if (board[i][j] == Road) return true;
return false;
}
void init() {
board = VVI(rows(), VI(cols()));
if (round()% 4 == me()) {
dist_w = VVI(rows(), VI(cols(), -1000));
dist_c = VVI(rows(), VI(cols(), -1000));
dist_uc = VVI(rows(), VI(cols(), -1000));
dist_a = VVI(rows(), VI(cols(), -1000));
}
dist_u = VVI(rows(), VI(cols(), -1000));
dist_f = VVI(rows(), VI(cols(), -1000));
queue<ii> qw, qc, quc;
priority_queue<pair<int, ii> > qf, qa, qu;
for (int i = 0; i < rows(); ++i) {
for (int j = 0; j < cols(); ++j) {
Cell c = cell(i, j);
board[i][j] = c.type;
if (round()% 4 == me() and c.type == Water) {
dist_w[i][j] = 0;
qw.push({i, j});
}
else if (round()% 4 == me() and c.type == City) {
dist_c[i][j] = 0;
qc.push({i, j});
if (c.owner != me()) {
dist_uc[i][j] = 0;
quc.push({i, j});
}
}
else if (c.type == Station) {
dist_f[i][j] = 0;
qf.push({0, {i, j}});
}
}
}
for (int i = 0; round()% 4 == me() and i < 4; ++i) {
if (i != me()) {
vector<int> ca = cars(i);
for (int j = 0; j < ca.size(); ++j) {
Unit u = unit(ca[j]);
int x = u.pos.i, y = u.pos.j;
dist_a[x][y] = 0;
qa.push({0, {x, y}});
}
}
}
for (int i = 0; i < 4; ++i) {
if (i != me()) {
vector<int> w = warriors(i);
for (int j = 0; j < w.size(); ++j) {
Unit u = unit(w[j]);
int x = u.pos.i, y = u.pos.j;
dist_u[x][y] = 0;
qu.push({0, {x, y}});
}
}
}
while (round()% 4 == me() and qw.size() > 0) {
int x0 = qw.front().first, y0 = qw.front().second;
int w0 = dist_w[x0][y0];
qw.pop();
for (int i = 0; i < 8; ++i) {
int x = x0 + dx[i], y = y0 + dy[i];
if (passable_w(x, y) and dist_w[x][y] == -1000) {
dist_w[x][y] = w0 - 1;
qw.push({x, y});
}
}
}
while (round()% 4 == me() and quc.size() > 0) {
int x0 = quc.front().first, y0 = quc.front().second;
int w0 = dist_uc[x0][y0];
quc.pop();
for (int i = 0; i < 8; ++i) {
int x = x0 + dx[i], y = y0 + dy[i];
if (passable_w(x, y) and dist_uc[x][y] == -1000) {
dist_uc[x][y] = w0 - 1;
quc.push({x, y});
}
}
}
while (round()% 4 == me() and qc.size() > 0) {
int x0 = qc.front().first, y0 = qc.front().second;
int w0 = dist_c[x0][y0];
qc.pop();
for (int i = 0; i < 8; ++i) {
int x = x0 + dx[i], y = y0 + dy[i];
if (passable_w(x, y) and dist_c[x][y] == -1000) {
dist_c[x][y] = w0 - 1;
qc.push({x, y});
}
}
}
while (round()% 4 == me() and qa.size() > 0) {
int x0 = qa.top().second.first, y0 = qa.top().second.second;
int w0 = dist_a[x0][y0], w1 = qa.top().first;
qa.pop();
if (w0 == w1) {
for (int i = 0; i < 8; ++i) {
int x = x0 + dx[i], y = y0 + dy[i];
if (passable_c(x, y)) {
int w = dist_a[x][y];
int w2 = w0;
if (board[x0][y0] == Road) --w2;
else w2 -= 4;
if (w2 > w) {
dist_a[x][y] = w2;
qa.push({w2, {x, y}});
}
}
}
}
}
while (qf.size() > 0) {
int x0 = qf.top().second.first, y0 = qf.top().second.second;
int w0 = dist_f[x0][y0], w1 = qf.top().first;
qf.pop();
if (w0 == w1) {
for (int i = 0; i < 8; ++i) {
int x = x0 + dx[i], y = y0 + dy[i];
if (passable_c(x, y)) {
int w = dist_f[x][y];
int w2 = w0;
if (board[x0][y0] == Road) --w2;
else w2 -= 4;
if (w2 > w) {
dist_f[x][y] = w2;
qf.push({w2, {x, y}});
}
}
}
}
}
while (qu.size() > 0) {
int x0 = qu.top().second.first, y0 = qu.top().second.second;
int w0 = dist_u[x0][y0], w1 = qu.top().first;
qu.pop();
if (w0 == w1) {
for (int i = 0; i < 8; ++i) {
int x = x0 + dx[i], y = y0 + dy[i];
if (passable_c(x, y)) {
int w = dist_u[x][y];
int w2 = w0;
if (board[x][y] == Road) --w2;
else w2 -= 4;
if (w2 > w) {
dist_u[x][y] = w2;
qu.push({w2, {x, y}});
}
}
}
}
}
}
void move_warriors() {
if (round()% 4 != me()) return; // This line makes a lot of sense.
VI w = warriors(me());
int n = w.size();
set<ii> occupied;
for (int i = 0; i < n; ++i) {
Unit u = unit(w[i]);
int x0 = u.pos.i, y0 = u.pos.j;
occupied.insert({x0, y0});
}
for (int i = 0; i < n; ++i) {
Unit u = unit(w[i]);
int x0 = u.pos.i, y0 = u.pos.j;
int best = 8, value = -99999999;
int x1 = x0, y1 = y0;
for (int j = 0; j < 9; ++j) {
int x = x0 + dx[j], y = y0 + dy[j];
if (passable_w(x, y) and occupied.count({x, y}) == 0 and dist_a[x][y] < -4) {
int score = 0;
int z = cell(x, y).id;
if (cell(x, y).type != City and z != -1) {
Unit v = unit(z);
if (v.type == Warrior) score += 1000000;
}
if (u.water < 20) score += dist_w[x][y];
else if (u.food < 20) score =+ dist_c[x][y];
else score += 4*dist_uc[x][y] - dist_a[x][y];
if (score > value) {
best = j;
value = score;
x1 = x, y1 = y;
}
}
}
occupied.insert({x1, y1});
command(w[i], Dir(best));
}
}
void move_cars() {
map<ii, int> occupied;
for (int i = 0; i < 4; ++i) {
VI ca = cars(i);
for (int j = 0; j < ca.size(); ++j) {
Unit u = unit(ca[j]);
int x0 = u.pos.i, y0 = u.pos.j;
for (int x = x0-1; x <= x0+1; ++x) {
for (int y = y0-1; y <= y0+1; ++y) {
occupied[{x, y}]++;
}
}
}
}
VI c = cars(me());
int n = c.size();
for (int i = 0; i < n; ++i) {
Unit u = unit(c[i]);
int x0 = u.pos.i, y0 = u.pos.j;
int best = 8, value = -99999999;
int x1 = x0, y1 = y0;
for (int j = 0; j < 8; ++j) {
int x = x0 + dx[j], y = y0 + dy[j];
if (passable_c(x, y)) {
int score = 0;
Cell k = cell(x, y);
if (k.type == Road) score += 10000;
if (occupied[{x, y}] >= 2) score = -100000000;
else if (j != 8 and k.id != -1) {
if (unit(k.id).player == me()) score = -100000000;
else score = 100000000;
}
else if (u.food < 30) score = dist_f[x][y];
else score = dist_u[x][y];
if (score > value) {
best = j;
value = score;
x1 = x, y1 = y;
}
}
}
if (can_move(c[i]) and best != 8) {
command(c[i], Dir(best));
occupied[{x1, y1}]++;
}
}
/*for (int id : C) {
if (kind.find(id) == kind.end()) kind[id] = random(0, 1);
if (can_move(id)) { // This also makes sense.
if (kind[id] == 0) command(id, Dir(random(0, 7)));
else command(id, Top);
}
}*/
}
/**
* Play method, invoked once per each round.
*/
void play () {
init();
move_warriors();
move_cars();
}
};
/**
* Do not modify the following line.
*/
RegisterPlayer(PLAYER_NAME);