-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdemo03_smartrandom.py
58 lines (52 loc) · 2.37 KB
/
demo03_smartrandom.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
# This bot moves randomly, with some improvements:
# - it kills enemies in the vicinity
# - it eats food in the vicinity
# - does not do kamikaze
# - avoids going back to positions it has seen already
TEAM_NAME = 'SmartRandomBots'
def move(bot, state):
enemy = bot.enemy
# get a tuple with our enemy positions
enemy_pos = (enemy[0].position, enemy[1].position)
# sensible positions are all legal positions that do not cause death
sensible_positions = []
# loop throuh all legal_positions for this bot
for next_pos in bot.legal_positions:
# each legal position is good unless we step on a ghost outside our homezone
if (next_pos in enemy_pos) and (next_pos not in bot.homezone):
continue
else:
sensible_positions.append(next_pos)
if len(sensible_positions) == 0:
# we can't go anywhere safe
# so just stop here and hope for the best
next_pos = bot.position
else:
# we can do something without risk
# collect positions that have something interesting in it (food can be
# eaten or enemy can be killed)
interesting_positions = []
for new_pos in sensible_positions:
# the new position is interesting if
# 1. we are in our homezone and we can kill an enemy
cond1 = (new_pos in bot.homezone) and (new_pos in enemy_pos)
# or, if 2. we are in enemy's homezone and we can eat food
cond2 = (new_pos in enemy[0].homezone) and (new_pos in enemy[0].food)
if cond1 or cond2:
# if either one condition is met, this position is interesting
interesting_positions.append(new_pos)
# now we have scanned all sensible positions
# do we have any interesting left?
if len(interesting_positions) > 0:
# yes, so choose one at random
next_pos = bot.random.choice(interesting_positions)
else:
# all sensible positions are equally (un)interesting, so pick
# one that we haven't seen already
for next_pos in sensible_positions:
if next_pos not in bot.track:
break
# if we don't break out of the loop early it means that all new
# positions have been visited already, so we just pick the
# last one in the list
return next_pos