-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
227 lines (202 loc) · 9.13 KB
/
test.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
__author__ = 'gvrooyen'
import unittest
import tron
import judge
import shutil
from constants import *
import random
import time
import viz
import minmaxflood_i
MAX_MOVE_TIME = 4.9
class TestTron(unittest.TestCase):
def test_movement(self):
P = tron.Position((10,10))
self.assertEqual(P.north().to_tuple(), (10,9))
self.assertEqual(P.south().to_tuple(), (10,11))
self.assertEqual(P.east().to_tuple(), (11,10))
self.assertEqual(P.west().to_tuple(), (9,10))
self.assertTrue(P.is_adjacent((10,11)))
self.assertFalse(P.is_adjacent((9,9)))
self.assertFalse(P.is_adjacent((20,20)))
P = tron.Position((0,0))
self.assertEqual(P.north(), None)
self.assertEqual(P.south(), None)
self.assertEqual(P.east().to_tuple(), (0,0))
self.assertEqual(P.west().to_tuple(), (0,0))
self.assertTrue(P.at_north_pole())
self.assertFalse(P.at_south_pole())
self.assertFalse(P.is_adjacent((29,0))) # (29,0) and (0,0) are the same point (the North Pole)
self.assertTrue(P.is_adjacent((17,1))) # All points on the arctic circle are adjacent to the North Pole
P = tron.Position((29,29))
self.assertEqual(P.north(), None)
self.assertEqual(P.south(), None)
self.assertEqual(P.east().to_tuple(), (0,29))
self.assertEqual(P.west().to_tuple(), (0,29))
self.assertTrue(P.at_south_pole())
self.assertFalse(P.at_north_pole())
self.assertFalse(P.is_adjacent((0,29))) # (29,29) and (0,29) are the same point (the South Pole)
self.assertTrue(P.is_adjacent((19,28))) # All points on the antarctic circle are adjacent to the South Pole
class TestStateFile(unittest.TestCase):
def test_poles(self):
"""
Verify that behaviour at the poles are as expected. All points in the (ant)arctic circles should be
treated as adjacent, and (x,0) should be the same point for all x, and likewise for (x,29).
"""
blue_red = (((15,1),(15,28)),
((25,0),(25,29)), # Both players step into the poles. The system should handle the adjacency
# of the unusual coordinates correctly.
((5,1), (5,28)), # Both players step out of the poles at completely different longitudes. Again,
# the system should handle the adjacency correctly despite the unusual
# coordinates.
((5,0), (5,29)) # This should generate an exception, since both players have already visited
# the poles.
)
J = judge.Judge(pos_blue=blue_red[0][0], pos_red=blue_red[0][1])
J.world.save('game.state', player=BLUE)
self.assertEqual(J.adjudicate('game.state', new_move=None), None)
winner = None
move = 0
for (blue,red) in blue_red[1:]:
move += 1
for pos in (blue, red):
player = BLUE if pos == blue else RED
J.world.save('game.state', player=player)
P = tron.World('game.state')
P.move_player(pos)
P.save('game.state')
if move == 3:
self.assertRaises(judge.StateFileException, J.adjudicate, 'game.state', new_move=player)
else:
J.adjudicate('game.state', new_move=player)
def test_adjudication(self):
blue_red = (((10,10),(9,9)),
((11,10),(10,9)),
((12,10),(11,9)),
((13,10),(12,9)),
((13,11),(13,9)),
((12,11),(14,9)),
((11,11),(14,10)),
((10,11),(14,11)),
((10,12),(14,12)),
((11,12),(14,13)),
((12,12),(14,14)),
((13,12),(14,15)),
((13,13),(13,15)),
((12,13),(12,15)),
((11,13),(11,15)),
((10,13),(10,15)),
((10,14),(9,15)),
((11,14),(9,14)),
((12,14),(9,13)),
((13,14),(9,12))
)
J = judge.Judge(pos_blue=blue_red[0][0], pos_red=blue_red[0][1])
J.world.save('game.state', player=BLUE)
self.assertEqual(J.adjudicate('game.state', new_move=None), None)
winner = None
for (blue,red) in blue_red[1:]:
for pos in (blue, red):
player = BLUE if pos == blue else RED
J.world.save('game.state', player=player)
P = tron.World('game.state')
P.move_player(pos)
P.save('game.state')
winner = J.adjudicate('game.state', new_move=player)
if winner:
break
if winner:
break
else:
self.fail("Winning condition not detected")
self.assertEqual(winner, RED)
def test_basic_strategy(self):
for seed in xrange(100,110):
random.seed(seed)
J = judge.Judge()
J.world.save('game.state', player=BLUE)
self.assertEqual(J.adjudicate('game.state', new_move=None), None)
winner = None
player = BLUE
while (winner == None):
player = RED if player == BLUE else BLUE
shutil.copyfile('game.state', 'game.state.bak')
J.world.save('game.state', player=player)
W = tron.World('game.state')
S = tron.Strategy()
S.move(W)
shutil.copyfile('game.state', 'game.state.bak')
W.save('game.state')
winner = J.adjudicate('game.state', new_move=player)
self.assertNotEqual(winner, None)
# world_map = viz.WorldMap()
# world_map.plot_trace(J.trace_blue,J.trace_red)
# world_map.plot_points(J.world.empty_space(),'g')
# world_map.show()
def test_prospect(self):
random.seed(1006)
J = judge.Judge()
J.world.save('game.state', player=BLUE)
self.assertEqual(J.adjudicate('game.state', new_move=None), None)
winner = None
player = BLUE
for turn in xrange(0,30):
player = RED if player == BLUE else BLUE
shutil.copyfile('game.state', 'game.state.bak')
J.world.save('game.state', player=player)
W = tron.World('game.state')
S = tron.Strategy()
S.move(W)
shutil.copyfile('game.state', 'game.state.bak')
W.save('game.state')
winner = J.adjudicate('game.state', new_move=player)
if winner:
break
(player_domain, opponent_domain) = J.world.prospect(plies=40)
# world_map = viz.WorldMap()
# world_map.plot_trace(J.trace_blue,J.trace_red)
# world_map.plot_points(player_domain, 'c')
# world_map.plot_points(opponent_domain, 'm')
# world_map.show()
def test_minmaxflood_i(self):
for seed in xrange(101,103):
random.seed(seed)
# TODO: The basic strategy test loop can be factored out into a separate module
J = judge.Judge()
J.world.save('game.state', player=BLUE)
self.assertEqual(J.world.pos_opponent[1],J.world.pos_player[1]) # Same axial
self.assertEqual(abs(J.world.pos_opponent[0] - J.world.pos_player[0]), 15) # Opposite sides
self.assertEqual(J.adjudicate('game.state', new_move=None), None)
print("Blue starts at (%d,%d)" % J.world.pos_player)
print("Red starts at (%d,%d)" % J.world.pos_opponent)
winner = None
player = RED
while (winner == None):
if player == BLUE:
player = RED
# Red plays with the naive random-move strategy
S = tron.Strategy()
else:
player = BLUE
# Blue plays with the strategy under test
S = minmaxflood_i.Strategy()
S.time_limit = 20.0
shutil.copyfile('game.state', 'game.state.bak')
J.world.save('game.state', player=player)
W = tron.World('game.state')
start_time = time.time()
S.move(W)
shutil.copyfile('game.state', 'game.state.bak')
W.save('game.state')
# self.assertLess(time.time() - start_time, MAX_MOVE_TIME)
winner = J.adjudicate('game.state', new_move=player)
self.assertNotEqual(winner, None)
if winner == BLUE:
result = "Blue wins!"
else:
result = "Red wins!"
world_map = viz.WorldMap()
world_map.plot_trace(J.trace_blue,J.trace_red)
# world_map.plot_points(J.world.empty_space(),'g')
world_map.save(result, str(seed)+'.png')
# TODO: Add a unit test for the number of liberties at the north and the south pole (it was calculated incorrectly)