-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdemo04_basic_attacker.py
71 lines (60 loc) · 2.8 KB
/
demo04_basic_attacker.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
# This bot selects a food pellet at random, then goes and tries to get it by
# following the shortest path to it.
# It tries on the way to avoid being killed by the enemy: if the next move
# to get to the food would put it on a ghost, then it chooses a random safe
# position
import networkx
TEAM_NAME = 'Basic Attacker Bots'
def init_attack_state():
return {
"attack_target": None,
"attack_path": None,
}
def move(bot, state):
# The state dictionary is initially empty
if state == {}:
# Initialize the state dictionary.
# Each bot needs its own state dictionary to keep track of the
# food targets.
state[0] = init_attack_state()
state[1] = init_attack_state()
# define a few variables for less typing
enemy = bot.enemy
target = state[bot.turn]["attack_target"]
path = state[bot.turn]["attack_path"]
# choose a target food pellet if we still don't have one or
# if the old target is not there anymore. This can happen for
# two different reasons:
# - the old target has been eaten in the last turn
# - the old target has been relocated because of the oppenent's defender
# sitting near it for too long
if (target is None) or (target not in enemy[0].food):
# position of the target food pellet
target = bot.random.choice(enemy[0].food)
# use networkx to get the shortest path from here to the target
# we do not use the first position, which is always equal to bot_position
path = networkx.shortest_path(bot.graph, bot.position, target)[1:]
state[bot.turn]["attack_path"] = path
state[bot.turn]["attack_target"] = target
# get the next position along the shortest path to reach our target
next_pos = path.pop(0)
# if we are not in our homezone we should check if it is safe to proceed
if next_pos not in bot.homezone:
# get a list of safe positions
safe_positions = []
for pos in bot.legal_positions:
if pos not in (enemy[0].position, enemy[1].position):
safe_positions.append(pos)
# we are about to step on top of an enemy
if next_pos not in safe_positions:
# 1. Let's forget about this target and this path
# We will choose a new target in the next round
state[bot.turn]["attack_target"] = None
state[bot.turn]["attack_path"] = None
# watch out! We only want to overwrite these two keys:
# in your bots you may have other relevant information in the state
# dictionary that you don't want to delete here!
# Choose one safe position at random (this always includes the
# current position
next_pos = bot.random.choice(safe_positions)
return next_pos