-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
408 lines (340 loc) · 12.9 KB
/
game.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
import sys
import math
import io
import random
import copy
# Help the Christmas elves fetch presents in a magical labyrinth!
def log(msg):
print(msg, file=sys.stderr, flush=True)
class Direction:
def __init__(self, dx, dy):
self.dx = dx
self.dy = dy
self.names = {
(0, -1): "UP",
(1, 0): "RIGHT",
(0, 1): "DOWN",
(-1, 0): "LEFT"
}
def __repr__(self):
return self.names[(self.dx, self.dy)]
def __eq__(self, other):
return self.dx == other.dx and self.dy == other.dy
def opposite(self):
return Direction(-self.dx, -self.dy)
UP = Direction(0, -1)
RIGHT = Direction(1, 0)
DOWN = Direction(0, 1)
LEFT = Direction(-1, 0)
DIRS = [UP, RIGHT, DOWN, LEFT]
class Position:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, dir):
return Position(self.x + dir.dx, self.y + dir.dy)
def __eq__(self, other):
return isinstance(other, Position) and self.x == other.x and self.y == other.y
def __repr__(self):
return "P[%d,%d]" % (self.x, self.y)
def dist(self, dest):
return abs(self.x - dest.x) + abs(self.y - dest.y)
def copy(self):
return Position(self.x, self.y)
class Parser:
def __init__(self, data):
if type(data) == str:
data = io.StringIO(data)
self.file = data
def is_ws(self, ch):
return ch in [' ', '\t', '\r', '\n']
def skip_ws(self):
ch = self.file.read(1)
while ch != '' and self.is_ws(ch):
ch = self.file.read(1)
return ch
def next_word(self):
w = self.skip_ws()
while w != '':
ch = self.file.read(1)
if ch == '' or self.is_ws(ch):
break
w = w + ch
return w
def next_int(self):
return int(self.next_word())
def parse_player_info(self):
num_cards = self.next_int()
pos_x = self.next_int()
pos_y = self.next_int()
pattern = self.next_word()
return PlayerInfo(num_cards, Position(pos_x, pos_y), Tile(pattern))
def parse_items(self):
num_items = self.next_int()
items = []
for _ in range(num_items):
name = self.next_word()
pos_x = self.next_int()
pos_y = self.next_int()
plr_id = self.next_int()
items.append(Item(name, Position(pos_x, pos_y), plr_id))
return items
def parse_quests(self):
num_quests = self.next_int()
quests = []
for _ in range(num_quests):
name = self.next_word()
plr_id = self.next_int()
quests.append(Quest(name, plr_id))
return quests
def parse_board(self, size=7):
cells = []
for _ in range(size):
row = []
for _ in range(size):
row.append(Tile(self.next_word()))
cells.append(row)
plr_info = self.parse_player_info()
op_info = self.parse_player_info()
items = self.parse_items()
quests = self.parse_quests()
return Board(size, cells, plr_info, op_info, items, quests)
class Item:
def __init__(self, name, pos, plr_id):
self.name = name
self.pos = pos
self.player_id = plr_id
def __repr__(self):
return "(%s,%s,%d)" % (self.name, self.pos, self.player_id)
class Tile:
def __init__(self, pattern):
self.pattern = pattern
self.visited = False
def __eq__(self, other):
return isinstance(other, Tile) and self.pattern == other.pattern
def __repr__(self):
return self.pattern
def can_go(self, dir):
if dir == UP and self.pattern[0] == "1":
return True
if dir == RIGHT and self.pattern[1] == "1":
return True
if dir == DOWN and self.pattern[2] == "1":
return True
if dir == LEFT and self.pattern[3] == "1":
return True
return False
def copy(self):
return Tile(self.pattern)
class PlayerInfo:
def __init__(self, num_cards, pos, tile):
# num_player_cards: the total number of quests for a player (hidden and revealed)
self.num_cards = num_cards
self.pos = pos
self.tile = tile
def __repr__(self):
return "PlayerInfo(%d,%s,%s)" % (self.num_cards, self.pos, self.tile)
def copy(self):
return PlayerInfo(self.num_cards, self.pos.copy(), self.tile.copy())
class Quest:
def __init__(self, name, plr_id):
self.item_name = name
self.player_id = plr_id
def __repr__(self):
return "{%s,%d}" % (self.item_name, self.player_id)
class Board:
def __init__(self, size, cells, plr_info, op_info, items=[], quests=[]):
self.size = size
self.cells = cells
self.player_info = plr_info
self.opponent_info = op_info
self.items = items
self.quests = quests
def __repr__(self):
s = ""
for y in range(self.size):
s += " ".join(["%s" % self.cells[y][x]
for x in range(self.size)]) + "\n"
s += str(self.player_info) + "\n" + \
str(self.opponent_info) + "\n" + str(self.items) + "\n"
return s
def get_items(self):
my_quests = [q for q in self.quests if q.player_id == 0]
#log("my quests: %s" % str(my_quests))
items = [i for i in self.items if i.name in [
q.item_name for q in my_quests] and i.player_id == 0]
return items
def possible_moves(self):
for n in [-1, 0, 1]:
nx = self.player_info.pos.x + n
if nx >= 0 and nx < self.size:
yield((UP, nx))
yield((DOWN, nx))
ny = self.player_info.pos.y + n
if ny >= 0 and ny < self.size:
yield((LEFT, ny))
yield((RIGHT, ny))
def valid_pos(self, pos):
return pos.x >= 0 and pos.x < self.size and pos.y >= 0 and pos.y < self.size
def get_tile(self, pos):
return self.cells[pos.y][pos.x]
def can_go(self, pos, dir):
if self.valid_pos(pos + dir):
if self.get_tile(pos).can_go(dir) and self.get_tile(pos + dir).can_go(dir.opposite()) and \
not self.get_tile(pos + dir).visited:
return True
return False
def nearest_paths(self, pos, dest):
self.found_paths = []
self.best_dist = min([pos.dist(d) for d in dest])
valid = [d for d in dest if self.valid_pos(d)]
def walk(pos, path=[]):
self.get_tile(pos).visited = True
dist = min([pos.dist(d) for d in valid])
if dist == self.best_dist:
self.found_paths.append(path)
elif dist < self.best_dist:
self.found_paths = [path]
self.best_dist = dist
for dir in DIRS:
if self.can_go(pos, dir) and len(path) < 20:
walk(pos + dir, path + [dir])
self.get_tile(pos).visited = False
if len(valid) > 0:
walk(pos)
log("pos: %s, dest: %s, dist: %d" %
(str(pos), str(valid), self.best_dist))
else:
self.found_paths.append([])
return self.found_paths
def push(self, offset, dir):
cells = copy.deepcopy(self.cells)
plr_info = self.player_info.copy()
opp_info = self.opponent_info.copy()
items = copy.deepcopy(self.items)
N = self.size-1
if dir == RIGHT:
tile = cells[offset][N]
for i in range(N, 0, -1):
cells[offset][i] = cells[offset][i-1]
cells[offset][0] = plr_info.tile
plr_info.tile = tile
if plr_info.pos.y == offset:
plr_info.pos.x = (plr_info.pos.x + 1) % self.size
if opp_info.pos.y == offset:
opp_info.pos.x = (opp_info.pos.x + 1) % self.size
for i in items:
if i.pos.y == offset:
i.pos.x = (i.pos.x + 1) % self.size
elif i.pos.x == -1:
i.pos = Position(0, offset)
elif dir == DOWN:
tile = cells[N][offset]
for i in range(N, 0, -1):
cells[i][offset] = cells[i-1][offset]
cells[0][offset] = plr_info.tile
plr_info.tile = tile
if plr_info.pos.x == offset:
plr_info.pos.y = (plr_info.pos.y + 1) % self.size
if opp_info.pos.x == offset:
opp_info.pos.y = (opp_info.pos.y + 1) % self.size
for i in items:
if i.pos.x == offset:
i.pos.y = (i.pos.y + 1) % self.size
elif i.pos.x == -1:
i.pos = Position(offset, 0)
elif dir == LEFT:
tile = cells[offset][0]
for i in range(0, N):
cells[offset][i] = cells[offset][i+1]
cells[offset][N] = plr_info.tile
plr_info.tile = tile
if plr_info.pos.y == offset:
plr_info.pos.x = (plr_info.pos.x + N) % self.size
if opp_info.pos.y == offset:
opp_info.pos.x = (opp_info.pos.x + N) % self.size
for i in items:
if i.pos.y == offset:
i.pos.x = (i.pos.x + N) % self.size
elif i.pos.x == -1:
i.pos = Position(N, offset)
elif dir == UP:
tile = cells[0][offset]
for i in range(0, N):
cells[i][offset] = cells[i+1][offset]
cells[N][offset] = plr_info.tile
plr_info.tile = tile
if plr_info.pos.x == offset:
plr_info.pos.y = (plr_info.pos.y + N) % self.size
if opp_info.pos.x == offset:
opp_info.pos.y = (opp_info.pos.y + N) % self.size
for i in items:
if i.pos.x == offset:
i.pos.y = (i.pos.y + N) % self.size
elif i.pos.x == -1:
i.pos = Position(offset, N)
return Board(self.size, cells, plr_info, opp_info, items, self.quests)
def game_loop(parser):
last_move = ()
stale = 0
while True:
turn_type = parser.next_int()
log("Turn type: %d" % turn_type)
board = parser.parse_board()
log("Items: %s" % board.get_items())
#log("Quests: %s" % board.quests)
# PUSH <id> <direction> | MOVE <direction> | PASS
if turn_type == 0:
best_dist = 1e6
best_moves = []
for move in board.possible_moves():
log(move)
d, n = move
b2 = board.push(n, d)
items = b2.get_items()
log("items: %s" % str(items))
paths = b2.nearest_paths(b2.player_info.pos, [
i.pos for i in items])
if b2.best_dist < best_dist:
best_dist = b2.best_dist
best_moves = [move]
log("new best dist: %d" % best_dist)
elif b2.best_dist == best_dist:
best_moves.append(move)
if best_dist == 0:
break
log("best_dist: %d\nmoves: %s" % (best_dist, str(best_moves)))
dir, n = random.choice(best_moves)
if last_move == (dir, n):
stale += 1
if stale > 3:
log("STALE!")
dir = random.choice(DIRS)
n = random.randint(0, 6)
stale = 0
last_move = (dir, n)
else:
stale = 0
last_move = (dir, n)
print("PUSH %d %s" % (n, dir), flush=True)
else:
best_dist = 1e6
best_paths = []
items = board.get_items()
paths = board.nearest_paths(
board.player_info.pos, [i.pos for i in items])
if board.best_dist < best_dist:
best_dist = board.best_dist
best_paths = paths
log("new best dist: %d\npaths: %s" %
(best_dist, best_paths))
elif board.best_dist == best_dist:
best_paths.extend(paths)
log("paths: %s" % str(best_paths))
path = random.choice(best_paths)
if len(path) == 0:
print("PASS", flush=True)
else:
print("MOVE %s" % " ".join([str(d) for d in path]), flush=True)
if __name__ == "__main__":
game_loop(Parser(sys.stdin))