-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_laser_chess.py
433 lines (345 loc) · 14.3 KB
/
test_laser_chess.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
from laser_chess import *
from laser_chess_consts import *
import pytest
import unittest
def board_with_corner_kings():
"""
This creates a board with two lasers and two kings on the board
and returns the LaserChess object "encapsulating" the board.
"""
board_setup = make_empty_board()
board_setup[1, 1] = KING_2
board_setup[6, 8] = KING_1
test_board = LaserChess(board_setup)
return test_board
"""This tests the methods in LaserChess, as well as the functions.
The helper functions are already tested in Repl.it's
Unit Test thing, but I copied it over here."""
class TestHelperFuncs(unittest.TestCase):
def test_make_piece(self):
self.assertEqual(make_piece(FIRST, DEFENDER, FEND_S), FEND_S1)
self.assertEqual(make_piece(FIRST, LASER, LASER_HORT), LASER_H1)
self.assertEqual(make_piece(SECOND, DEFLECTOR, FLEC_NW), FLEC_NW2)
self.assertEqual(make_piece(SECOND, SWITCH, SWITCH_NWSE), SWITCH_NWSE2)
self.assertEqual(make_piece(SECOND, KING, KING_ORIENT), KING_2)
def test_find_player(self):
self.assertEqual(find_player(SWITCH_NWSE2), SECOND)
self.assertEqual(find_player(LASER_V1), FIRST)
self.assertEqual(find_player(FEND_S2), SECOND)
self.assertEqual(find_player(KING_1), FIRST)
def test_find_piece(self):
self.assertEqual(find_piece(FLEC_NW1), DEFLECTOR)
self.assertEqual(find_piece(SWITCH_NWSE2), SWITCH)
self.assertEqual(find_piece(LASER_V1), LASER)
def test_find_orient(self):
self.assertEqual(find_orient(KING_2), KING_ORIENT)
self.assertEqual(find_orient(FEND_W1), FEND_W)
self.assertEqual(find_orient(SWITCH_NESW2), SWITCH_NESW)
def test_tuple_add(self):
self.assertEqual(tuple_add((1, 5, 4), (2, 4, 7)), (3, 9, 11))
self.assertEqual(tuple_add((-4, -3, -5, -9, -2), (9, 8, 2, 1, 3)), (5, 5, -3, -8, 1))
self.assertEqual(tuple_add((1, ), (2, )), (3, ))
self.assertEqual(tuple_add(N, E), NE)
self.assertEqual(tuple_add(W, S), SW)
self.assertEqual(tuple_add(N, S), (0, 0))
def test_coord_within_bounds(self):
self.assertTrue(coord_within_bounds((0, 0)))
self.assertTrue(coord_within_bounds((2, 4)))
self.assertFalse(coord_within_bounds((0, 10)))
self.assertFalse(coord_within_bounds((5, 11)))
self.assertFalse(coord_within_bounds((8, 3)))
self.assertFalse(coord_within_bounds((11, 15)))
board_setup1 = make_empty_board()
board_setup1[1, 1] = KING_1
board_setup1[6, 8] = KING_2
board_setup1[0, 7] = FEND_E1
board_setup1[7, 2] = FEND_W2
board_setup1[2, 1] = SWITCH_NWSE1
board_setup1[3, 8] = SWITCH_NWSE2
board_setup1[5, 0] = FLEC_NW2
board_setup1[1, 9] = FLEC_SE1
board_setup1[3, 7] = SWITCH_NESW2
test_board1 = LaserChess(board_setup1)
# test_board1.pretty_print()
class TestIllegalMoves():
# Empty square
def test_empty_square(self):
assert test_board1.make_move((2, 5), N, SECOND) == False
assert test_board1.make_move((3, 2), E, FIRST) == False
# Moving the laser
def test_moving_laser(self):
assert test_board1.make_move((0, 0), E, SECOND) == False
assert test_board1.make_move((0, 0), S, SECOND) == False
assert test_board1.make_move((0, 0), SE, SECOND) == False
assert test_board1.make_move((7, 9), W, FIRST) == False
assert test_board1.make_move((7, 9), N, FIRST) == False
assert test_board1.make_move((7, 9), NW, FIRST) == False
# Forbidden squares in the corner there.
def test_forbidden_corner(self):
assert test_board1.make_move((6, 8), S, FIRST) == False
assert test_board1.make_move((1, 1), N, SECOND) == False
assert test_board1.make_move((7, 2), W, SECOND) == False
assert test_board1.make_move((0, 7), E, FIRST) == False
# Forbidden squares on the edges.
def test_forbidden_edge(self):
assert test_board1.make_move((2, 1), SW, FIRST) == False
assert test_board1.make_move((2, 1), W, FIRST) == False
assert test_board1.make_move((2, 1), NW, FIRST) == False
assert test_board1.make_move((3, 9), NE, SECOND) == False
assert test_board1.make_move((3, 9), E, SECOND) == False
assert test_board1.make_move((3, 9), SE, SECOND) == False
# Going over the edge
def test_edge(self):
assert test_board1.make_move((7, 2), S, SECOND) == False
assert test_board1.make_move((7, 2), SW, SECOND) == False
assert test_board1.make_move((7, 2), SE, SECOND) == False
assert test_board1.make_move((0, 7), N, FIRST) == False
assert test_board1.make_move((0, 7), NW, FIRST) == False
assert test_board1.make_move((0, 7), NE, FIRST) == False
assert test_board1.make_move((5, 0), W, SECOND) == False
assert test_board1.make_move((5, 0), SW, SECOND) == False
assert test_board1.make_move((5, 0), NW, SECOND) == False
assert test_board1.make_move((1, 9), E, FIRST) == False
assert test_board1.make_move((1, 9), NE, FIRST) == False
assert test_board1.make_move((1, 9), SE, FIRST) == False
def test_forbidden_switch(self):
# Switch can't switch with kings
assert test_board1.make_move((2, 1), N, FIRST) == False
# Switch can't swtich with other switches
assert test_board1.make_move((3, 7), E, SECOND) == False
assert test_board1.make_move((3, 8), W, SECOND) == False
# Can't touch other player's pieces
def test_other_player(self):
assert not test_board1.make_move((5, 0), E, FIRST)
assert not test_board1.make_move((1, 1), SE, SECOND)
# Can't switch pieces on the forbidden squares on the edge.
def test_forbidden_edge_switch(self):
test_board = board_with_corner_kings()
test_board.board[4, 8] = SWITCH_NESW2
test_board.board[3, 9] = FLEC_NE1
test_board.board[4, 9] = FEND_W1
test_board.board[5, 9] = FLEC_SW1
test_board.board[4, 1] = SWITCH_NWSE1
test_board.board[3, 0] = FLEC_NW2
test_board.board[4, 0] = FEND_E2
test_board.board[5, 0] = FLEC_SE2
for second_move in (NE, E, SE):
assert not test_board.make_move((4, 8), second_move, SECOND)
for first_move in (SW, W, NW):
assert not test_board.make_move((4, 1), first_move, FIRST)
"""The following tests test allowed moves."""
board_setup2 = make_empty_board()
board_setup2[1, 1] = KING_2
board_setup2[6, 8] = KING_1
board_setup2[3, 2] = SWITCH_NESW1
board_setup2[4, 3] = FLEC_SE1
board_setup2[2, 6] = SWITCH_NWSE2
board_setup2[3, 6] = FEND_N2
test_board2 = LaserChess(board_setup2)
# print(test_board)
# move to an empty space
class TestLegalMoves():
def test_move_to_empty(self):
assert test_board2.make_move((1, 1), SE, SECOND)
assert test_board2.make_move((6, 8), NW, FIRST)
# switch special move, baby.
def test_allowed_switch(self):
assert test_board2.make_move((3, 2), SE, FIRST)
assert test_board2.make_move((2, 6), S, SECOND)
# rotations are always legal
def test_rotation_moves(self):
assert test_board2.make_move((2, 6), CCW, SECOND)
assert test_board2.make_move((3, 2), CW, FIRST)
# more move to an empty space
def test_more_move(self):
assert test_board2.make_move((2, 6), N, SECOND)
assert test_board2.make_move((3, 2), S, FIRST)
assert test_board2.make_move((3, 6), E, SECOND)
assert test_board2.make_move((4, 3), SW, FIRST)
"""This tests the capabilities of the Laser."""
class TestLaserHitsDefenders():
def test_south_laser_and_defenders(self):
test_board = board_with_corner_kings()
test_board.board[2, 0] = FEND_S1
test_board.board[3, 0] = FEND_W1
test_board.board[4, 0] = FEND_E1
test_board.board[5, 0] = FEND_N1
print(test_board)
for piece_gone in (FEND_S1, FEND_W1, FEND_E1):
assert test_board.shoot_laser(SECOND) == piece_gone
"""
assert test_board.shoot_laser(SECOND) == FEND_S1
assert test_board.shoot_laser(SECOND) == FEND_W1
assert test_board.shoot_laser(SECOND) == FEND_E1
"""
assert test_board.shoot_laser(SECOND) is None
def test_east_laser_and_defenders(self):
test_board = board_with_corner_kings()
test_board.make_move((0, 0), CCW, SECOND)
test_board.board[0, 2] = FEND_E1
test_board.board[0, 3] = FEND_N1
test_board.board[0, 4] = FEND_S1
test_board.board[0, 5] = FEND_W1
print(test_board)
assert test_board.shoot_laser(SECOND) == FEND_E1
assert test_board.shoot_laser(SECOND) == FEND_N1
assert test_board.shoot_laser(SECOND) == FEND_S1
assert test_board.shoot_laser(SECOND) is None
def test_north_laser_and_defenders(self):
test_board = board_with_corner_kings()
test_board.board[2, 9] = FEND_S1
test_board.board[3, 9] = FEND_W1
test_board.board[4, 9] = FEND_E1
test_board.board[5, 9] = FEND_N1
print(test_board)
assert test_board.shoot_laser(FIRST) == FEND_N1
assert test_board.shoot_laser(FIRST) == FEND_E1
assert test_board.shoot_laser(FIRST) == FEND_W1
assert test_board.shoot_laser(FIRST) is None
def test_west_laser_and_defenders(self):
test_board = board_with_corner_kings()
test_board.make_move((7, 9), CCW, FIRST)
test_board.board[7, 8] = FEND_W1
test_board.board[7, 7] = FEND_N1
test_board.board[7, 6] = FEND_S1
test_board.board[7, 5] = FEND_E1
print(test_board)
assert test_board.shoot_laser(FIRST) == FEND_W1
assert test_board.shoot_laser(FIRST) == FEND_N1
assert test_board.shoot_laser(FIRST) == FEND_S1
assert test_board.shoot_laser(FIRST) is None
class TestLaserHitsDeflectors():
def test_north_laser_hits_deflectors(self):
test_board = board_with_corner_kings()
test_board.board[4, 9] = FLEC_NE2
test_board.board[3, 9] = FLEC_NW1
test_board.board[2, 9] = FLEC_SW1
test_board.board[2, 4] = FLEC_SE2
test_board.board[4, 4] = FEND_S2
print(test_board)
assert test_board.shoot_laser(FIRST) == FLEC_NE2
assert test_board.shoot_laser(FIRST) == FLEC_NW1
assert test_board.shoot_laser(FIRST) == FEND_S2
def test_south_laser_hits_deflectors(self):
test_board = board_with_corner_kings()
test_board.board[3, 0] = FLEC_SW1
test_board.board[4, 0] = FLEC_SE2
test_board.board[5, 0] = FLEC_NE2
test_board.board[5, 5] = FLEC_NW2
test_board.board[3, 5] = FEND_W1
print(test_board)
assert test_board.shoot_laser(SECOND) == FLEC_SW1
assert test_board.shoot_laser(SECOND) == FLEC_SE2
assert test_board.shoot_laser(SECOND) == FEND_W1
def test_east_laser_hits_deflectors(self):
test_board = board_with_corner_kings()
test_board.make_move((0, 0), CCW, SECOND)
test_board.board[0, 3] = FLEC_SE1
test_board.board[0, 4] = FLEC_NE2
test_board.board[0, 5] = FLEC_SW2
test_board.board[5, 5] = FLEC_NW1
test_board.board[5, 4] = FEND_S1
print(test_board)
assert test_board.shoot_laser(SECOND) == FLEC_SE1
assert test_board.shoot_laser(SECOND) == FLEC_NE2
assert test_board.shoot_laser(SECOND) == FEND_S1
def test_west_laer_hits_deflectors(self):
test_board = board_with_corner_kings()
test_board.make_move((7, 9), CCW, FIRST)
test_board.board[7, 6] = FLEC_NW2
test_board.board[7, 4] = FLEC_SW1
test_board.board[7, 2] = FLEC_NE1
test_board.board[2, 2] = FLEC_SE2
test_board.board[2, 6] = FEND_N2
print(test_board)
assert test_board.shoot_laser(FIRST) == FLEC_NW2
assert test_board.shoot_laser(FIRST) == FLEC_SW1
assert test_board.shoot_laser(FIRST) == FEND_N2
class TestLaserHitsSwitches():
def test_nesw_switch(self):
test_board = board_with_corner_kings()
test_board.board[4, 0] = SWITCH_NESW2
test_board.board[4, 3] = SWITCH_NESW1
test_board.board[6, 3] = SWITCH_NWSE2
test_board.board[6, 0] = SWITCH_NESW2
test_board.board[5, 0] = FEND_N1
print(test_board)
assert test_board.shoot_laser(SECOND) == FEND_N1
def test_nwse_switch(self):
test_board = board_with_corner_kings()
test_board.make_move((7, 9), CCW, FIRST)
test_board.board[7, 4] = FLEC_NE1
test_board.board[5, 4] = SWITCH_NWSE1
test_board.board[5, 7] = SWITCH_NWSE2
test_board.board[3, 7] = SWITCH_NESW1
test_board.board[3, 4] = SWITCH_NWSE2
test_board.board[4, 4] = FEND_S2
print(test_board)
assert test_board.shoot_laser(FIRST) == FEND_S2
class TestLaserMisc():
def test_straight_laser(self):
test_board = board_with_corner_kings()
test_board.board[0, 9] = FEND_N2
test_board.board[7, 0] = FEND_S1
print(test_board)
assert test_board.shoot_laser(FIRST) == FEND_N2
assert test_board.shoot_laser(SECOND) == FEND_S1
def test_flec_reflect_hit(self):
test_board = board_with_corner_kings()
test_board.board[3, 8] = FLEC_SW1
test_board.board[3, 1] = FLEC_SW2
test_board.make_move((3, 8), E, FIRST)
print(test_board)
assert test_board.shoot_laser(FIRST) == FLEC_SW2
def test_hit_defender_face(self):
test_board = board_with_corner_kings()
test_board.board[7, 4] = FEND_E1
test_board.make_move((7, 9), CW, FIRST)
print(test_board)
assert test_board.shoot_laser(FIRST) is None
def test_hit_defender_side(self):
test_board = board_with_corner_kings()
test_board.board[0, 3] = FEND_S2
test_board.make_move((0, 0), CCW, SECOND)
print(test_board)
assert test_board.shoot_laser(SECOND) == FEND_S2
def test_switch_reflect(self):
test_board = board_with_corner_kings()
test_board.board[2, 9] = SWITCH_NESW1
test_board.board[2, 2] = SWITCH_NWSE2
print(test_board)
assert test_board.shoot_laser(FIRST) is None
def test_laser_hit_laser(self):
test_board = board_with_corner_kings()
test_board.board[5, 9] = FLEC_SW1
test_board.board[5, 0] = FLEC_NE2
print(test_board)
assert test_board.shoot_laser(SECOND) is None
assert test_board.shoot_laser(FIRST) is None
def test_laser_goes_offboard(self):
test_board = board_with_corner_kings()
test_board.board[0, 2] = SWITCH_NWSE2
assert test_board.shoot_laser(SECOND) is None
test_board.board[6, 9] = SWITCH_NWSE1
assert test_board.shoot_laser(FIRST) is None
print(test_board)
def test_zigzagly_laser_and_hit_king(self):
test_board = board_with_corner_kings()
test_board.board[1, 9] = SWITCH_NESW1
test_board.board[1, 2] = FLEC_SE2
test_board.board[6, 2] = FLEC_NE1
test_board.board[6, 5] = FLEC_NW1
test_board.board[2, 5] = SWITCH_NWSE2
test_board.board[2, 7] = SWITCH_NESW2
test_board.board[4, 7] = FLEC_NW1
test_board.board[4, 4] = FLEC_NE2
test_board.board[3, 4] = FLEC_SW1
test_board.board[3, 0] = FLEC_SE2
test_board.board[5, 0] = FLEC_NE2
test_board.board[5, 1] = FLEC_NW1
print(test_board)
assert test_board.shoot_laser(FIRST) == KING_2
def main():
pytest.main()
if __name__ == "__main__":
main()