-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
296 lines (229 loc) · 11.9 KB
/
tests.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
from unittest import TestCase
import autopy
from board import get_position, NotVisible
from screen import get_current_grid
from tactics import get_scoring_moves, get_grid_after_move, count_scoring_lines
class BoardDetectionTest(TestCase):
def test_basic_detection(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board1.png")
x, y = get_position(bitmap) # throws board.NotVisible if no position
# since the board positioning is imprecise, we just assert a range
self.assertGreater(x, 268)
self.assertLess(x, 272)
self.assertGreater(y, 100)
self.assertLess(y, 105)
def test_darker_board_detection(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_dark_background.png")
get_position(bitmap) # throws board.NotVisible if no position
def test_snow_board_detection(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_snow.png")
get_position(bitmap) # throws board.NotVisible if no position
def test_part_board(self):
"""If we can only see part of the board, we shouldn't say we
can detect it.
"""
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_part.png")
self.assertRaises(NotVisible, get_position, bitmap)
def test_side_board(self):
"""If we can only see one side of the board, we shouldn't say
we can detect it.
"""
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_left_part.png")
self.assertRaises(NotVisible, get_position, bitmap)
def test_black_screen(self):
"""Just because we look for a black line, does not mean a
screen with a lot of black should be detected as a board.
"""
bitmap = autopy.bitmap.Bitmap.open("sample_images/terminal.png")
self.assertRaises(NotVisible, get_position, bitmap)
class JewelDetectionTest(TestCase):
def test_purple(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board1.png")
grid = get_current_grid(bitmap)
self.assertEqual(grid[0][4], 'purple')
def test_green(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board1.png")
grid = get_current_grid(bitmap)
self.assertEqual(grid[0][0], 'green')
def test_board_shine(self):
"""Test a board that's having the diagonal shine scroll across
it.
"""
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_with_shine.png")
grid = get_current_grid(bitmap)
self.assertEqual(grid[7][4], 'white')
self.assertEqual(grid[6][5], 'green')
self.assertEqual(grid[5][6], 'blue')
self.assertEqual(grid[4][7], 'purple')
def test_blue(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board1.png")
grid = get_current_grid(bitmap)
self.assertEqual(grid[0][1], 'blue')
def test_yellow(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board1.png")
grid = get_current_grid(bitmap)
self.assertEqual(grid[0][5], 'yellow')
def test_orange(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board1.png")
grid = get_current_grid(bitmap)
self.assertEqual(grid[0][3], 'orange')
def test_red(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board1.png")
grid = get_current_grid(bitmap)
self.assertEqual(grid[1][1], 'red')
def test_white(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board1.png")
grid = get_current_grid(bitmap)
self.assertEqual(grid[1][4], 'white')
def test_all(self):
"""Although the other tests should be sufficient, we have an
extra test of each jewel since our input is noisy.
"""
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_dark_background.png")
grid = get_current_grid(bitmap)
self.assertEqual(grid[0][1], 'white')
self.assertEqual(grid[1][7], 'white')
self.assertEqual(grid[0][0], 'orange')
self.assertEqual(grid[7][3], 'orange')
self.assertEqual(grid[0][3], 'green')
self.assertEqual(grid[0][7], 'green')
self.assertEqual(grid[0][5], 'purple')
self.assertEqual(grid[7][7], 'purple')
self.assertEqual(grid[1][5], 'yellow')
self.assertEqual(grid[5][3], 'yellow')
self.assertEqual(grid[0][2], 'red')
self.assertEqual(grid[6][7], 'red')
self.assertEqual(grid[7][5], 'blue')
self.assertEqual(grid[7][6], 'blue')
class CrossJewelDetectionTest(TestCase):
def test_yellow_cross(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_with_glowing_yellow.png")
grid = get_current_grid(bitmap)
# todo: check it's special
self.assertEqual(grid[2][5], 'yellow')
def test_green_cross(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_with_green_cross.png")
grid = get_current_grid(bitmap)
# todo: check it's special
self.assertEqual(grid[2][7], 'green')
def test_purple_cross(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_with_purple_cross.png")
grid = get_current_grid(bitmap)
# todo: check it's special
self.assertEqual(grid[7][3], 'purple')
def test_orange_cross(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_with_orange_cross.png")
grid = get_current_grid(bitmap)
# todo: check it's special
self.assertEqual(grid[3][2], 'orange')
def test_red_cross(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_with_red_cross.png")
grid = get_current_grid(bitmap)
# todo: check it's special
self.assertEqual(grid[6][6], 'red')
def test_blue_cross(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_with_blue_cross.png")
grid = get_current_grid(bitmap)
# todo: check it's special
self.assertEqual(grid[6][3], 'blue')
class FireJewelDetectionTest(TestCase):
def test_red_fire(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_with_red_fire.png")
grid = get_current_grid(bitmap)
# todo: check it's special
self.assertEqual(grid[6][3], 'red')
def test_white_fire(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_with_white_fire.png")
grid = get_current_grid(bitmap)
# todo: check it's special
self.assertEqual(grid[3][6], 'white')
def test_blue_fire(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_with_blue_fire.png")
grid = get_current_grid(bitmap)
# todo: check it's special
self.assertEqual(grid[0][3], 'blue')
def test_purple_fire(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_with_purple_fire.png")
grid = get_current_grid(bitmap)
# todo: check it's special
self.assertEqual(grid[5][6], 'purple')
def test_yellow_fire(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_with_yellow_green_fire.png")
grid = get_current_grid(bitmap)
# todo: check it's special
self.assertEqual(grid[3][6], 'yellow')
def test_green_fire(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_with_yellow_green_fire.png")
grid = get_current_grid(bitmap)
# todo: check it's special
self.assertEqual(grid[4][6], 'green')
class WildcardJewelDetectionTest(TestCase):
def test_wildcard(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_with_wildcard.png")
grid = get_current_grid(bitmap)
self.assertEqual(grid[5][5], 'wildcard')
# since the wildcard gem rotates, we have two images to test with
bitmap = autopy.bitmap.Bitmap.open("sample_images/board_with_wildcard2.png")
grid = get_current_grid(bitmap)
self.assertEqual(grid[5][5], 'wildcard')
class MovesTest(TestCase):
def test_valid_moves(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board2.png")
grid = get_current_grid(bitmap)
scoring_moves = list(get_scoring_moves(grid))
expected_moves = [((6,2), (7,2)), ((6, 3), (7, 3))]
# check we found all the expected moves
for move in expected_moves:
self.assertIn(move, scoring_moves)
# check there are no other moves found
self.assertEqual(len(scoring_moves), len(expected_moves))
def test_move_outcome(self):
bitmap = autopy.bitmap.Bitmap.open("sample_images/board2.png")
grid = get_current_grid(bitmap)
final_grid = get_grid_after_move(grid, ((6, 2), (7, 2)))
expected_final_grid = [
['purple', 'green', 'green', 'yellow', 'white', 'blue', None, 'yellow'],
['white', 'blue', 'red', 'blue', 'purple', 'orange', None, 'white'],
['purple', 'purple', 'orange', 'yellow', 'white', 'blue', None, 'orange'],
['red', 'green', 'blue', 'purple', 'red', 'yellow', 'purple', 'blue'],
['blue', 'yellow', 'blue', 'yellow', 'orange', 'white', 'green', 'red'],
['orange', 'yellow', 'red', 'green', 'red', 'yellow', 'yellow', 'purple'],
['purple', 'green', 'white', 'blue', 'orange', 'orange', 'blue', 'purple'],
['blue', 'red', 'white', 'purple', 'yellow', 'orange', 'green', 'green']]
self.assertEqual(expected_final_grid, final_grid)
def test_move_outcome_multiple_lines(self):
"""Test when lining up three jewels makes more line up, that
we iterate until there are no more lines to remove.
"""
grid = [
['green', 'yellow', 'purple', 'green', 'white', 'yellow', 'blue', 'yellow'],
['yellow', 'red', 'yellow', 'blue', 'orange', 'white', 'yellow', 'yellow'],
['blue', 'purple', 'orange', 'white', 'purple', 'blue', 'blue', 'red'],
['green', 'purple', 'red', 'yellow', 'green', 'white', 'yellow', 'yellow'],
['blue', 'green', 'red', 'white', 'purple', 'orange', 'white', 'yellow'],
['yellow', 'white', 'white', 'blue', 'orange', 'white', 'yellow', 'red'],
['purple', 'green', 'blue', 'red', 'yellow', 'blue', 'yellow', 'purple'],
['blue', 'green', 'yellow', 'orange', 'red', 'green', 'red', 'red']]
final_grid = get_grid_after_move(grid, ((5, 4), (6, 4)))
expected_final_grid = [
['green', 'yellow', 'purple', 'green', 'white', None, None, None],
['yellow', 'red', 'yellow', 'blue', 'orange', None, 'blue', 'yellow'],
['blue', 'purple', 'orange', 'white', 'purple', None, 'yellow', 'yellow'],
['green', 'purple', 'red', 'yellow', 'green', None, 'blue', 'red'],
['blue', 'green', 'red', 'white', 'purple', 'white', 'orange', 'yellow'],
['yellow', 'white', 'white', 'blue', 'orange', 'blue', 'yellow', 'red'],
['purple', 'green', 'blue', 'red', 'yellow', 'blue', 'yellow', 'purple'],
['blue', 'green', 'yellow', 'orange', 'red', 'green', 'red', 'red']]
self.assertEqual(final_grid, expected_final_grid)
def test_scoring_rows_sparse(self):
"""Empty grid positions are not scoring rows"""
grid = [
['purple', 'green', 'green', 'yellow', 'white', 'blue', None, 'yellow'],
['white', 'blue', 'red', 'blue', 'purple', 'orange', None, 'white'],
['purple', 'purple', 'orange', 'yellow', 'white', 'blue', None, 'orange'],
['red', 'green', 'blue', 'purple', 'red', 'yellow', 'purple', 'blue'],
['blue', 'yellow', 'blue', 'yellow', 'orange', 'white', 'green', 'red'],
['orange', 'yellow', 'red', 'green', 'red', 'yellow', 'yellow', 'purple'],
['purple', 'green', 'white', 'blue', 'orange', 'orange', 'blue', 'purple'],
['blue', 'red', 'white', 'purple', 'yellow', 'orange', 'green', 'green']]
self.assertEqual(count_scoring_lines(grid), 0)