-
Notifications
You must be signed in to change notification settings - Fork 0
/
natalie.py
120 lines (109 loc) · 3.76 KB
/
natalie.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
import heapq
import numpy as np
TEAM_NAME = 'Nat'
def move(bot, state):
if state is None:
state = {}
state["enemy_0_hx"] = []
state["enemy_1_hx"] = []
bot.say("it's me mario!")
food = bot.enemy[0].food
turn = bot.round + bot.turn
if turn <= 10:
state["enemy_0_hx"].append({
"position": bot.enemy[0].position,
"noisy": bot.enemy[0].is_noisy
})
state["enemy_1_hx"].append({
"position": bot.enemy[1].position,
"noisy": bot.enemy[1].is_noisy
})
else:
mod = turn % 10
state["enemy_0_hx"][mod] = {
"position": bot.enemy[0].position,
"noisy": bot.enemy[0].is_noisy
}
state["enemy_1_hx"][mod] = {
"position": bot.enemy[1].position,
"noisy": bot.enemy[1].is_noisy
}
walls = set(bot.walls)
e1 = simple_enemy_locator(state["enemy_0_hx"])
e2 = simple_enemy_locator(state["enemy_1_hx"])
buff = buffer_for_enemy([e1, e2])
walls.update(buff)
closest_food_move = choose_move(bot.position, food, walls)
if closest_food_move:
move = bot.get_move(closest_food_move)
else:
move = bot.get_move(bot.track[-1])
return move, state
def simple_enemy_locator(enemy_history):
y = 0
x = 0
for enemy_hx in (enemy_history[-2:-1]):
if not enemy_hx["noisy"]:
return enemy_hx["position"]
else:
y += enemy_hx["position"][0] / 2
x += enemy_hx["position"][1] / 2
return (round(y), round(x))
def buffer_for_enemy(enemy_locations):
return set((y + dy, x + dx)
for y, x in enemy_locations
for dy, dx in [(-3, 0), (3, 0), (0, 3), (0, -3),
(-2, 1), (-2, 0), (-2, -1),
(0, -2), (-1, -2), (1, -2),
(2, 1), (2, 0), (2, -1),
(0, 2), (-1, 2), (1, 2),
(1, 0), (1, -1), (1, 1),
(0, 1), (-1, 1), (-1, -1),
(0, -1), (-1, 0), (0, 0) ])
# Return the shortest path from current position to any of the food
# in the case of a tie, return all shortest paths
def shortest_paths(current_pos, food, walls):
result = []
best = None
visited = set(walls)
queue = [(0, [current_pos])]
while queue:
distance, path = heapq.heappop(queue)
if best and len(path) > best:
return result
node = path[-1]
if node in food:
result.append(path)
best = len(path)
continue
if node in visited:
continue
visited.add(node)
for neighbor in adjacent({node}):
if neighbor in visited:
continue
heapq.heappush(queue, (distance + 1, path + [neighbor]))
return result
# adjacent returns all cells that are adjacent to all of the provided positions
def adjacent(positions):
return set((y + dy, x + dx)
for y, x in positions
for dy, dx in [(-1, 0), (0, -1), (0, 1), (1, 0)])
# choose_target determines which target square a unit at `position` should
# move toward, given the specified target units
def choose_target(position, food, walls):
if not food:
return None
if position in food:
return position
paths = shortest_paths(position, food, walls)
ends = [x[-1] for x in paths]
return min(ends) if ends else None
# choose_move determines the immediate up/down/left/right move to make
# given the source and target squares
def choose_move(position, target, walls):
if position == target:
return position
paths = shortest_paths(position, target, walls)
starts = [x[1] for x in paths]
return min(starts) if starts else None