-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathEnhancedAggressivePlayer.py
442 lines (372 loc) · 14.6 KB
/
EnhancedAggressivePlayer.py
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
from __future__ import print_function
import sys
import time
import math
import random
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
# Hyperparameter
factor = 1.5
max_safe_moves = 8
class Pair():
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return ("(" + str(self.x) + ", " + str(self.y) + ")")
class Game():
def __init__(self, id, rows, cols):
self.id = id
self.rows = rows
self.cols = cols
self.direction = 1 - 2 * self.id;
self.current_player = 0
self.current_soldier = [[-1, -1], [-1, -1]]
self.soldiers = []
self.moves = []
self.bombs = []
self.board = [[0 for j in range(rows)] for i in range(cols)]
self.soldiers_indices = [[-1 for j in range(rows)] for i in range(cols)]
self.PlaceSoldiers()
self.PlaceTownHalls()
def PlaceSoldiers(self):
places = ([(self.rows - 1 - i) for i in range(3)], [i for i in range(3)])
for i in range(self.cols):
for j in range(3):
self.board[i][places[i % 2][j]] = math.pow(-1, i % 2)
if(i % 2 == self.id):
self.soldiers.append(Pair(x = i, y = places[i % 2][j]));
self.soldiers_indices[i][places[i % 2][j]] = len(self.soldiers) - 1
def PlaceTownHalls(self):
for i in range(self.cols // 2):
self.board[i * 2 + 1][self.rows - 1] = 2;
self.board[self.cols - 2 - i * 2][0] = -2;
def IsInBoard(self, x, y):
if(x >= 0 and x <= (self.cols - 1) and y >= 0 and y <= (self.rows - 1)):
return 1
return 0
def Sign(self, i):
if(i > 0):
return 1
if(i == 0):
return 0
if(i < 0):
return -1
def SelectSoldier(self, x, y):
self.moves = []
self.bombs = []
executable = self.Guides(x, y)
if(executable == 0):
return 0
self.current_soldier[self.current_player] = [x, y]
return 1
def Guides(self, x, y):
self.moves = []
self.bombs = []
executable = 0;
b = [1, 1, 1, 0, 0]
dx = [-1, 0, 1, -1, 1]
dy = [-1, -1, -1, 0, 0]
for i in range(len(dx)):
tx = x + dx[i]
ty = y + dy[i] * self.direction
if(not self.IsInBoard(tx, ty) or (self.Sign(self.board[tx][ty]) == self.direction) or ((b[i] == 0) and (self.Sign(self.board[tx][ty]) != -self.direction))):
continue
self.moves.append(Pair(x = tx, y = ty))
executable = 1
check = 0
adjx = [-1, -1, 0, 1, 1]
adjy = [0, -1, -1, -1, 0]
for i in range(len(adjx)):
tx = x + adjx[i]
ty = y + adjy[i] * self.direction;
if(self.IsInBoard(tx, ty) and self.board[tx][ty] == math.pow(-1, 1 - self.id)):
check = 1
break
dx = [-2, 0, 2]
dy = [2, 2, 2]
if(check):
for i in range(len(dx)):
tx = x + dx[i]
ty = y + dy[i] * self.direction
if(not self.IsInBoard(tx, ty) or self.Sign(self.board[tx][ty]) == self.direction):
continue
self.moves.append(Pair(x = tx, y = ty))
executable = 1
dx = [[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[-3, -2, 4, 5], [-4, -3, 3, 4], [-5, -4, 2, 3]], [[-3, -2, 4, 5], [-4, -3, 3, 4], [-5, -4, 2, 3]], [[-3, -2, 4, 5], [-4, -3, 3, 4], [-5, -4, 2, 3]]]
dy = [[[-3, -2, 4, 5], [-4, -3, 3, 4], [-5, -4, 2, 3]], [[-3, -2, 4, 5], [-4, -3, 3, 4], [-5, -4, 2, 3]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[3, 2, -4, -5], [4, 3, -3, -4], [5, 4, -2, -3]]]
valid_dx = [[[0, 0], [0, 0], [0, 0]], [[-1, 3], [-2, 2], [-3, 1]], [[-1, 3], [-2, 2], [-3, 1]], [[-1, 3], [-2, 2], [-3, 1]]]
valid_dy = [[[-1, 3], [-2, 2], [-3, 1]], [[-1, 3], [-2, 2], [-3, 1]], [[0, 0], [0, 0], [0, 0]], [[1, -3], [2, -2], [3, -1]]]
cdx = [[[0], [], [0]], [[3], [], [-3]], [[3], [], [-3]], [[3], [], [-3]]]
cdy = [[[3], [], [-3]], [[3], [], [-3]], [[0], [], [0]], [[-3], [], [3]]]
soldierx = [[[0, 0], [0, 0], [0, 0]], [[1, 2], [-1, 1], [-2, -1]], [[1, 2], [-1, 1], [-2, -1]], [[1, 2], [-1, 1], [-2, -1]]]
soldiery = [[[1, 2], [-1, 1], [-2, -1]], [[1, 2], [-1, 1], [-2, -1]], [[0, 0], [0, 0], [0, 0]], [[-1, -2], [1, -1], [2, 1]]]
for i in range(len(dx)):
for j in range(len(dx[0])):
check = 1
s = ([x + valid_dx[i][j][0], y + valid_dy[i][j][0] * self.direction], [x + valid_dx[i][j][1], y + valid_dy[i][j][1] * self.direction])
if((self.IsInBoard(s[0][0], s[0][1]) and (self.board[s[0][0]][s[0][1]] == 0)) or (self.IsInBoard(s[1][0], s[1][1]) and (self.board[s[1][0]][s[1][1]] == 0))):
for k in range(len(soldierx[0][0])):
tx = x + soldierx[i][j][k];
ty = y + soldiery[i][j][k] * self.direction;
if(not self.IsInBoard(tx, ty) or self.board[tx][ty] != self.direction):
check = 0
if(check):
for k in range(len(dx[0][0])):
if((self.IsInBoard(s[k // 2][0], s[k // 2][1]) and (self.board[s[k // 2][0]][s[k // 2][1]] == 0))):
tx = x + dx[i][j][k]
ty = y + dy[i][j][k] * self.direction
if(self.IsInBoard(tx, ty) and self.Sign(self.board[tx][ty]) != self.direction):
self.bombs.append(Pair(x = tx, y = ty))
executable = 1
for k in range(len(cdx[i][j])):
tx = x + cdx[i][j][k]
ty = y + cdy[i][j][k] * self.direction;
if(not self.IsInBoard(tx, ty) or self.board[tx][ty] != 0):
continue
self.moves.append(Pair(x = tx, y = ty))
executable = 1
# eprint ("For soldier at " + str(x) + ", " + str(y) + ": " + str([str(x) for x in self.moves]))
return executable
def MoveSoldier(self, x, y):
self.board[self.current_soldier[self.current_player][0]][self.current_soldier[self.current_player][1]] = 0
if(self.current_player == self.id):
index = self.soldiers_indices[self.current_soldier[self.current_player][0]][self.current_soldier[self.current_player][1]]
self.soldiers_indices[self.current_soldier[self.current_player][0]][self.current_soldier[self.current_player][1]] = -1
self.soldiers_indices[x][y] = index
self.soldiers[index] = Pair(x = x, y = y)
else:
if(self.board[x][y] == self.direction):
index = self.soldiers_indices[x][y]
self.soldiers_indices[x][y] = -1
self.soldiers[index] = Pair(x = -1, y = -1)
self.board[x][y] = math.pow(-1, self.current_player)
self.current_player = 1 - self.current_player
def ThrowBomb(self, x, y):
if(self.current_player == 1 - self.id):
if(self.board[x][y] == self.direction):
index = self.soldiers_indices[x][y]
self.soldiers_indices[x][y] = -1
self.soldiers[index] = Pair(x = -1, y = -1)
self.board[x][y] = 0
self.current_player = 1 - self.current_player
def execute_sequence(self, sequence):
move = []
for index, j in enumerate(sequence):
if(index % 3 == 2):
move += [j]
self.execute_move(' '.join(move))
move = []
else:
move += [j]
def execute_move(self, command) :
sequence = command.split()
if(len(sequence) > 3):
return self.execute_sequence(sequence)
type = sequence[0]
x = int(sequence[1])
y = int(sequence[2])
if(type == 'S'):
status = self.SelectSoldier(x, y)
elif(type == 'M'):
status = self.MoveSoldier(x, y)
elif(type == 'B'):
status = self.ThrowBomb(x, y)
return status
class EnhancedAggressivePlayer:
def __init__(self):
data = sys.stdin.readline().strip().split()
self.player = int(data[0]) - 1
self.rows = int(data[1])
self.cols = int(data[2])
self.time_left = int(data[3])
self.game = Game(id = self.player, rows = self.rows, cols = self.cols)
self.num_safe_moves = 0
self.state = 0
self.play()
# Get the severity of attack on the coordinates (x, y) in the following position
def GetSeverity(self, player, x, y):
# player = 1 - self.player
# eprint ("Getting severity for " + str(x) + ", " + str(y) + " as player " + str(player))
direction = 1 - 2 * player
severity = 0
# Check if soldier can be attacked by any immediate enemy
enemies = [(-2, 2), (-1, -1), (-1, 0), (0, -1), (0, 2), (1, -1), (1, 0), (2, 2)]
for enemy in enemies:
ex, ey = x + enemy[0], y + direction*enemy[1]
# eprint ("Checking for coordinate " + str(ex) + ", " + str(ey))
if self.game.IsInBoard(ex, ey) and (self.game.board[ex][ey] == math.pow(-1, 1 - player)):
# Enemy can attack - move from current position.
# eprint ("Found attack")
severity += 1
# Check if soldier can be attacked by any cannon
# The directions from where the cannons can attack
cannons = [(-1, 0), (-1, -1), (0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1)]
blanks = [2, 3]
for cannon in cannons:
for blank in blanks:
# First check if cannon attack is even possible from this direction
cx, cy = x + cannon[0]*(blank - 1), y + cannon[1]*(blank - 1)
# eprint ("Checking for cannon at " + str(x + cannon[0]*blank) + ", " + str(y + cannon[1]*blank))
if self.game.IsInBoard(cx, cy) and self.game.board[cx][cy] == 0:
# If cannon attack possible, check if cannon exists or not
# eprint ("Attack possible - checking if cannon exists")
flag = True
for i in range(3):
ex, ey = x + cannon[0]*(i + blank), y + cannon[1]*(i + blank)
if self.game.IsInBoard(ex, ey) and self.game.board[ex][ey] == math.pow(-1, 1 - player):
continue
flag = False
break
if flag:
# Cannon Present
# eprint ("Found attack")
severity += 1
# Assign higher severity to soldiers who are nearer to Townhalls
if (player == 1 and y < 3) or (player == 0 and y > 6):
severity = severity * factor
# eprint ("Found severity: " + str(severity))
return severity
def SelectSoldierAndMove(self):
maxSeverity = 0
aggressSoldier = (-1, -1, -1) # Soldier ID, MoveOrBomb, MoveIndex
for si in range(len(self.game.soldiers)):
sx, sy = self.game.soldiers[si].x, self.game.soldiers[si].y
exc = self.game.Guides(sx, sy)
if (sx != -1 and sy != -1 and exc != 0):
for mi in range(len(self.game.moves)):
mx, my = self.game.moves[mi].x, self.game.moves[mi].y
# If townhall attack possible - Do that
if (self.player == 0 and self.game.board[mx][my] == -2) or (self.player == 1 and self.game.board[mx][my] == 2):
maxSeverity = 100000
aggressSoldier = (si, 0, mi)
if (self.game.board[mx][my] == math.pow(-1, 1 - self.player)):
severity = self.GetSeverity(1 - self.player, mx, my)
if severity > maxSeverity:
maxSeverity = severity
aggressSoldier = (si, 0, mi)
# eprint ("Bombs for " + str(sx) + ", " + str(sy) + ": " + str([str(x) for x in self.game.bombs]))
for bi in range(len(self.game.bombs)):
bx, by = self.game.bombs[bi].x, self.game.bombs[bi].y
# If townhall attack possible - Do that
if (self.player == 0 and self.game.board[bx][by] == -2) or (self.player == 1 and self.game.board[bx][by] == 2):
maxSeverity = 100000
aggressSoldier = (si, 1, bi)
if (self.game.board[bx][by] == math.pow(-1, 1 - self.player)):
severity = self.GetSeverity(1 - self.player, bx, by)
if severity > maxSeverity:
maxSeverity = severity
aggressSoldier = (si, 1, bi)
sx, sy = -1, -1
mx, my = -1, -1
moveOrBomb = 0
if aggressSoldier == (-1, -1, -1):
# No attacking position available - play a safe move (Prefer to move forward)
# Provide a motivation to move towards enemy townhall
eprint ("No attacking position available - Play a safe move")
flag = 0
# Try to make a safe forward move. If not possible, make empty bomb shot or make a safe backward move
eprint ("Trying a forward safe move")
for s in range(len(self.game.soldiers)):
sx, sy = self.game.soldiers[s].x, self.game.soldiers[s].y
if(sx != -1 and sy != -1 and self.game.Guides(sx, sy) != 0):
for m in range(len(self.game.moves)):
mx, my = self.game.moves[m].x, self.game.moves[m].y
severity = self.GetSeverity(self.player, mx, my)
if(mx != -1 and my != -1 and severity == 0 and (my-sy)*self.game.direction < 0):
moveOrBomb = 0
flag = 1
self.num_safe_moves = 0
break
if flag == 1:
break
if flag == 0 and self.num_safe_moves < max_safe_moves:
eprint ("Forward safe move not available - Empty bomb shot or safe backward move")
for s in range(len(self.game.soldiers)):
sx, sy = self.game.soldiers[s].x, self.game.soldiers[s].y
if(sx != -1 and sy != -1 and self.game.Guides(sx, sy) != 0):
if (len(self.game.bombs) != 0):
moveOrBomb = 1
mx, my = self.game.bombs[0].x, self.game.bombs[0].y
self.num_safe_moves += 1
flag = 1
else:
moveOrBomb = 0
for m in range(len(self.game.moves)):
mx, my = self.game.moves[m].x, self.game.moves[m].y
if(mx != -1 and my != -1 and self.GetSeverity(self.player, mx, my) == 0):
flag = 1
self.num_safe_moves += 1
break
if flag == 1:
break
if flag == 0:
eprint ("No safe move available - Play random")
self.num_safe_moves = 0
for s in range(len(self.game.soldiers)):
x, y = self.game.soldiers[s].x, self.game.soldiers[s].y
exc = self.game.Guides(x, y)
if(x != -1 and y != -1 and exc != 0):
sx, sy = x, y
if (len(self.game.moves) != 0):
moveOrBomb = 0
for move in self.game.moves:
if (move.x != -1 and move.y != -1):
mx, my = move.x, move.y
break
else:
moveOrBomb = 1
for bomb in self.game.bombs:
if (bomb.x != -1 and bomb.y != -1):
mx, my = bomb.x, bomb.y
break
else:
# eprint ("Soldier chosen: " + str(aggressSoldier) + " with " + str(maxSeverity))
sx, sy = self.game.soldiers[aggressSoldier[0]].x, self.game.soldiers[aggressSoldier[0]].y
self.game.Guides(sx, sy)
moveOrBomb = aggressSoldier[1]
self.num_safe_moves = 0
if moveOrBomb == 0:
mx, my = self.game.moves[aggressSoldier[2]].x, self.game.moves[aggressSoldier[2]].y
else:
mx, my = self.game.bombs[aggressSoldier[2]].x, self.game.bombs[aggressSoldier[2]].y
return sx, sy, mx, my, moveOrBomb
def play_sequence(self, sequence):
moves = ' '.join(sequence) + '\n'
sys.stdout.write(moves)
sys.stdout.flush()
def play(self):
if(self.player == 1):
move = sys.stdin.readline().strip()
self.game.execute_move(move)
num_moves = 0
while(1):
if num_moves == 0:
# Deterministic Fort Development Strategy
num_moves += 1
if self.player == 0:
sequence = ['S 2 7', 'M 1 6']
else:
sequence = ['S 3 0', 'M 2 1']
elif num_moves == 1:
num_moves += 1
if self.player == 0:
sequence = ['S 4 7', 'M 5 6']
else:
sequence = ['S 5 0', 'M 6 1']
else:
sequence = []
sx, sy, mx, my, moveOrBomb = self.SelectSoldierAndMove()
sequence.append('S {x} {y}'.format(x = sx, y = sy))
if moveOrBomb == 0:
sequence.append('M {x} {y}'.format(x = mx, y = my))
else:
sequence.append('B {x} {y}'.format(x = mx, y = my))
for move in sequence:
self.game.execute_move(move)
self.state = 0
self.play_sequence(sequence)
move = sys.stdin.readline().strip()
self.game.execute_move(move)
random.seed(0)
EnhancedAggressivePlayer()