-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_demo04_basic_attacker.py
72 lines (68 loc) · 2.27 KB
/
test_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
72
from demo04_basic_attacker import move
from pelita.utils import setup_test_game
def test_eat_food():
# do we eat food when it's available?
layout="""
########
# 0.#
#.1 EE#
########
"""
bot = setup_test_game(layout=layout, is_blue=True)
next_move, _ = move(bot, None)
assert next_move == (1, 0)
def test_no_kamikaze():
# do we avoid enemies when they can kill us?
layout="""
########
# E.#
#.1 0E#
########
"""
bot = setup_test_game(layout=layout, is_blue=True)
# create a "fake" track of previous moves, as our bot needs it to decide
# where to go to avoid a bot
bot.track = [(4,2), (5,2)]
next_move, _ = move(bot, None)
assert next_move == (-1, 0)
def test_shortest_path():
# is the Graph implementation in pelita giving us the shortest path to the
# food pellet? And are we really following it?
# straight line is the right choice
layout1="""
########
#0 .#
#.1 EE#
########
"""
path1 = [(6,1), (5,1), (4,1), (3,1), (2,1)]
# there are more alternatives now, but the shortest is unique, so we can
# test it
layout2="""
########
#0####.#
# #
# #
#.1 EE#
########
"""
path2 = [(6, 1), (6,2), (5,2), (4,2), (3,2), (2,2), (1,2)]
for l, p in ((layout1, path1), (layout2, path2)):
bot = setup_test_game(layout=l, is_blue=True)
# we can ignore this, we just call move to have the bot generate the graph
# representation of the maze
next_move, state = move(bot, None)
graph = state['graph']
path = graph.a_star((1,1), (6,1))
# test that the generated path is the shortest one
assert path == p
# given this layout, move our bot to the next position in the shortest
# path and see if we follow it
path.reverse() # flip the path so we have it in the right order
for idx, step in enumerate(path[:-1]):
# create a layout where we are starting from the current step in
# the path
bot = setup_test_game(layout=l, is_blue=True, bots=[step])
next_move, state = move(bot, state)
next_pos = (step[0]+next_move[0], step[1]+next_move[1])
assert next_pos == path[idx+1]