-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
432 lines (391 loc) · 11.3 KB
/
main.cpp
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
#include "util.h"
#include "game_state.h"
#include "points.h"
#include "survival.h"
#include "points.h"
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std;
static inline void readMap(){
for(int c=0;c<16;c++){
gets(&(map[c][0]));
if(map[c][0]!=EMPTY &&
map[c][0]!=LADDER &&
map[c][0]!=BRICK &&
map[c][0]!=GOLD &&
map[c][0]!=REMOVED_BRICK)
c--;
}
}
static char tempBuffer[1000];
static inline void printReachableMatrix(int starti, int startj)
{
TRACE("Reachable matrix:\n");
for(int i = 0; i < 16; i++)
{
TRACE("reachable ");
for(int j = 0; j < 25; j++)
if(reachable[starti][startj][i][j])
TRACE("1 ");
else
TRACE("0 ");
TRACE("\n");
}
}
static inline void printComponentMatrix()
{
TRACE("Component matrix:\n");
for(int i = 0; i < 16; i++)
{
TRACE("comp ");
for(int j = 0; j < 25; j++)
TRACE("%2d ", component[i][j]);
TRACE("\n");
}
}
static inline void dfs(pair<int,int> start, pair<int,int> curr)
{
if(reachable[start.first][start.second][curr.first][curr.second])
return;
reachable[start.first][start.second][curr.first][curr.second] = true;
for(int i = 0; i < 7; i++)
if(canDoAction2(static_cast<Action>(i), curr))
dfs(start, simulateAction(static_cast<Action>(i), curr));
return;
}
static inline void computeAllWayReachability()
{
for(int i = 0; i < 16; i++)
for(int j = 0; j < 25; j++)
for(int ii = 0; ii < 16; ii++)
for(int jj = 0; jj < 25; jj++)
reachable[i][j][ii][jj] = false;
for(int i = 0; i < 16; i++)
{
for(int j = 0; j < 25; j++)
{
pair<int,int> start = make_pair(i, j);
dfs(start, start);
}
}
}
static inline void assignComponents()
{
for(int i = 0; i < 16; i++)
for(int j = 0; j < 25; j++)
component[i][j] = -1;
int comp = 0;
for(int i = 0; i < 16; i++)
{
for(int j = 0; j < 25; j++)
{
if(component[i][j] == -1)
component[i][j] = comp++;
for(int ii = 0; ii < 16; ii++)
for(int jj = 0; jj < 25; jj++)
if((reachable[i][j][ii][jj]) && (reachable[ii][jj][i][j]))
component[ii][jj] = component[i][j];
}
}
}
static inline void findGoldInComponents()
{
for(int i = 0; i < 600; i++)
gold_comp[i] = 0;
for(int i = 0; i < 16; i++)
for(int j = 0; j < 25; j++)
if(component[i][j] != -1)
if(map[i][j] == GOLD)
gold_comp[component[i][j]]++;
}
static inline void findTotalGoldInMap()
{
totalGoldOnMap = 0;
for(int i = 0; i < 16; i++)
for(int j = 0; j < 25; j++)
if(map[i][j] == GOLD)
totalGoldOnMap++;
}
static inline void saveFirstMap()
{
for(int i = 0; i < 16; i++)
for(int j = 0; j < 25; j++)
originalMap[i][j] = map[i][j];
}
static inline void initGame(){
scanf(" %d\n",&nrounds);
readMap();
scanf(" %d %d ",&currLoc.first,&currLoc.second);
ourSpawn = currLoc;
brickDelay = 0;
scanf(" %d %d ",&enemyLoc.first,&enemyLoc.second);
enemySpawn = enemyLoc;
enemySpawnDelay = 0;
enemyBrickDelay = 0;
scanf(" %d\n",&nenemies);
for(int i=0;i<nenemies;i++){
//todo, find a better way of dealing with the program.
scanf(" %d %d %s",&enemies[i].loc.first,&enemies[i].loc.second,tempBuffer);
enemies[i].spawn = enemies[i].loc;
enemies[i].program.resize(strlen(tempBuffer));
for(int j=0;j<enemies[i].program.size();j++){
switch(tempBuffer[j]){
case 'R':
enemies[i].program[j] = RIGHT;
break;
case 'L':
enemies[i].program[j] = LEFT;
break;
case 'T':
enemies[i].program[j] = TOP;
break;
case 'B':
enemies[i].program[j] = BOTTOM;
break;
}
}
enemies[i].master = NOONE;
enemies[i].isTrapped = false;
enemies[i].spawnDelay = 0;
enemies[i].distSq = distSq(enemies[i].loc,currLoc);
enemies[i].distSqToOpponent = distSq(enemies[i].loc,enemyLoc);
}
currTurn = -1;
currScore = 0;
enemyScore = 0;
computeAllWayReachability();
assignComponents();
findTotalGoldInMap();
findGoldInComponents();
printComponentMatrix();
TRACE("Total gold = %d\n", totalGoldOnMap);
for(int i = 0; i < 85; i++)
if(gold_comp[i] > 1)
TRACE("Gold in %d = %d\n", i, gold_comp[i]);
saveFirstMap();
}
static inline void act(Action act){
puts(actionNames[act]);
fflush(stdout);//don't remove this!
}
//process the input for one of the enemies
static void processEnemy1(int i){
pair<int,int> oldLoc = enemies[i].loc;
int master;
scanf(" %d %d %d",&enemies[i].loc.first,&enemies[i].loc.second,&master);
enemies[i].master = static_cast<Player>(master);
if(enemies[i].loc.first!=-1 && oldLoc.first!=-1 && oldLoc!=enemies[i].loc){
if(enemies[i].loc.first<oldLoc.first){
enemies[i].lastMove = TOP;
}
else if (enemies[i].loc.first>oldLoc.first){
//even if it's falling we should interpret that as a "BOTTOM"
//since that's what the enemies program does
enemies[i].lastMove = BOTTOM;
}
else if(enemies[i].loc.second<oldLoc.second){
enemies[i].lastMove = LEFT;
}
else{
enemies[i].lastMove = RIGHT;
}
}
else{
enemies[i].lastMove = NONE;
}
if(enemies[i].loc.first!=-1 && (map[enemies[i].loc.first][enemies[i].loc.second]==REMOVED_BRICK || map[enemies[i].loc.first][enemies[i].loc.second]==FILLED_BRICK)){
map[enemies[i].loc.first][enemies[i].loc.second]=FILLED_BRICK;
enemies[i].isTrapped = true;
}
else{
enemies[i].isTrapped = false;
}
}
static void processEnemy2(int i){
if(enemies[i].loc.first==-1){
enemies[i].distSq = -1;
enemies[i].distSqToOpponent = -1;
if(enemies[i].spawnDelay==0){
enemies[i].spawnDelay=max(24-missedTurns,1);
}
else{
enemies[i].spawnDelay--;
if(enemies[i].spawnDelay==0)
enemies[i].spawnDelay = 1;
}
enemies[i].chaseState = PATROL;
enemies[i].patrolIndex = 0;
}
else{
enemies[i].spawnDelay=0;
if(currLoc.first!=-1 && !enemies[i].isTrapped)
enemies[i].distSq = distSq(enemies[i].loc,currLoc);
else
enemies[i].distSq = -1;
if(enemyLoc.first!=-1 && !enemies[i].isTrapped)
enemies[i].distSqToOpponent = distSq(enemies[i].loc,enemyLoc);
else
enemies[i].distSqToOpponent = -1;
pair<int,ChaseInfo> info = computeChaseState(i);
enemies[i].chaseInfo = info.second;
switch(enemies[i].chaseState){
case CHASE_BLUE:
case CHASE_RED:
if(enemies[i].lastMove!=NONE)
enemies[i].chaseStack.push_front(enemies[i].lastMove);
break;
case PATROL:
if(enemies[i].lastMove!=NONE){
enemies[i].patrolIndex++;
enemies[i].patrolIndex %= enemies[i].program.size();
}
break;
case RETURN_TO_PATROL:
//assert(!enemies[i].chaseStack.empty());
if(enemies[i].lastMove!=NONE)
enemies[i].chaseStack.pop_front();
if(enemies[i].chaseStack.empty())
enemies[i].chaseState = PATROL;
break;
}
switch(info.first){
case RED:
enemies[i].chaseState = CHASE_RED;
break;
case BLUE:
enemies[i].chaseState = CHASE_BLUE;
break;
case NOONE:
switch(enemies[i].chaseState){
case CHASE_RED:
case CHASE_BLUE:
if(enemies[i].chaseStack.empty())
enemies[i].chaseState = PATROL;
else
enemies[i].chaseState = RETURN_TO_PATROL;
break;
}
}
}
}
static void setSquareDelays()
{
for(int i = 0; i < 16; i++)
for(int j = 0; j < 25; j++)
if(originalMap[i][j]==GOLD && game.map[i][j] !=GOLD)
{
if(game.timeout[i][j]==0)
game.timeout[i][j]= 150;
else
game.timeout[i][j]--;
}
else if (originalMap[i][j]==BRICK && game.map[i][j]!=BRICK)
{
if(game.timeout[i][j]==0)
game.timeout[i][j]=25;
else
game.timeout[i][j]--;
}
}
//returns false when finished
static bool doTurn(){
int nextTurn;
scanf(" %d",&nextTurn);
if(nextTurn==-1)
return false;
TRACE("TRACE: Starting turn #%d\n",nextTurn);
missedTurns = nextTurn - currTurn -1;
if(missedTurns!=0)
WARN("WARNING: Lost Turn(s)! (%d turns lost)\n",missedTurns);
currTurn = nextTurn;
readMap();
setSquareDelays();
scanf(" %d %d %d %d",&currLoc.first,&currLoc.second,&currScore,&brickDelay);
scanf(" %d %d %d %d",&enemyLoc.first,&enemyLoc.second,&enemyScore,&enemyBrickDelay);
if(enemyLoc.first==-1){
if(enemySpawnDelay==0){
enemySpawnDelay = max(49-missedTurns,1);
}
else{
enemySpawnDelay--;
if(enemySpawnDelay==0)
enemySpawnDelay=1;
}
}
else{
enemySpawnDelay = 0;
}
//these two loops MUST be separate, the second depends on the first finishing
for(int i=0;i<nenemies;i++){
processEnemy1(i);
}
for(int i=0;i<nenemies;i++){
processEnemy2(i);
}
for(int i=0;i<16;i++){
TRACE("MAP: %s\n",&map[i][0]);
}
TRACE("POS: %d %d\n",currLoc.first,currLoc.second);
//actual ai starts here
int survivalScore[7];
memset(survivalScore,0,sizeof(int)*7);
scoreSurvival(survivalScore);
//TODO add points score
vector<state> states = pointsScore(5);
//for(int i =1; i <states.size();i++)
// survivalScore[states[i].first]+=100/states[i].depth*i;
state s= states.size()>1?states[1]:states[0];
TRACE("State 1 DIST: %d\n",s.depth);
for(int i =2; i <states.size();i++)
{
TRACE("STATE: %d DIST %d COST %d\n",i,states[i].depth,states[i].cost);
if(states[i].depth-s.depth<3 || states[i].cost<=s.cost)
s = states[i];
else
break;
}
if(s.first==DIG_LEFT || s.first==DIG_RIGHT)
TRACE("ORDERED TO DIG!\n");
TRACE("TRACE: Action: %s pos: %d %d depth: %d\n",actionNames[static_cast<int>(s.first)],s.pos.first,s.pos.second,s.depth);
if(s.first!=NONE)
survivalScore[s.first]+=50;
vector<Action> bests;
int maxScore = 0;
for(int i=NONE;i<7;i++){
if(survivalScore[i]>maxScore){
maxScore = survivalScore[i];
bests.clear();
bests.push_back(static_cast<Action>(i));
}
else if(survivalScore[i]==maxScore){
bests.push_back(static_cast<Action>(i));
}
}
Action a = NONE;
if(bests.size()!=0){
a = bests[rand()%bests.size()];
TRACE("CANDIDATES:");
for(int i=0;i<bests.size();i++){
TRACE(" %s",actionNames[bests[i]]);
}
TRACE("\n");
}
rlocs.push_back(currLoc.first);
clocs.push_back(currLoc.second);
actions.push_back(a);
act(a);
TRACE("TRACE: Turn #%d finished with action %s with a score of %d\n",currTurn,actionNames[a],maxScore);
return true;
}
int main(){
srand(time(NULL));
initGame();
while(doTurn());
TRACE("Game Finished!\n");
return 0;
}