forked from mistyy1007/three-musketeers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththree_musketeers_test.py
181 lines (152 loc) · 6.59 KB
/
three_musketeers_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
import unittest
from three_musketeers import *
left = 'left'
right = 'right'
up = 'up'
down = 'down'
M = 'M'
R = 'R'
_ = '-'
class TestThreeMusketeers(unittest.TestCase):
def setUp(self):
set_board([ [_, _, _, M, _],
[_, _, R, M, _],
[_, R, M, R, _],
[_, R, _, _, _],
[_, _, _, R, _] ])
def test_create_board(self):
create_board()
self.assertEqual(at((0, 0)), 'R')
self.assertEqual(at((0, 4)), 'M')
def test_set_board(self):
self.assertEqual(at((0, 0)), '-')
self.assertEqual(at((1, 2)), 'R')
self.assertEqual(at((1, 3)), 'M')
def test_get_board(self):
self.assertEqual([ [_, _, _, M, _],
[_, _, R, M, _],
[_, R, M, R, _],
[_, R, _, _, _],
[_, _, _, R, _] ],
get_board())
def test_string_to_location(self):
self.assertEqual(string_to_location("A1"),(0,0))
self.assertEqual(string_to_location("B5"),(1,4))
self.assertEqual(string_to_location("D3"),(3,2))
def test_location_to_string(self):
self.assertEqual(location_to_string([2,2]),"C3")
self.assertEqual(location_to_string([3,4]),"D5")
self.assertEqual(location_to_string([0,1]),"A2")
def test_at(self):
self.assertEqual(at((3,4)),get_board()[3][4])
def test_all_locations(self):
self.assertEqual(all_locations()[2], (0,2))
self.assertEqual(all_locations()[10], (2,0))
# Replace with tests
def test_adjacent_location(self):
self.assertEqual(adjacent_location((2,3), left), (2,2))
self.assertEqual(adjacent_location((2,3), right), (2,4))
self.assertEqual(adjacent_location((2,3), up), (1,3))
self.assertEqual(adjacent_location((2,3), down), (3,3))
def test_is_legal_move_by_musketeer(self):
self.assertTrue(is_legal_move_by_musketeer((2,2),'left'))
self.assertTrue(is_legal_move_by_musketeer((1,3),'down'))
self.assertFalse(is_legal_move_by_musketeer((0,3),'down'))
def test_is_legal_move_by_enemy(self):
self.assertTrue(is_legal_move_by_enemy((1,2),'left'))
self.assertFalse(is_legal_move_by_enemy((1,2),'right'))
def test_is_legal_move(self):
self.assertTrue(is_legal_move_by_musketeer((2,2),'left'))
self.assertFalse(is_legal_move_by_musketeer((0,3),'down'))
self.assertTrue(is_legal_move_by_enemy((1,2),'left'))
self.assertFalse(is_legal_move_by_enemy((1,2),'right'))
def test_can_move_piece_at(self):
self.assertTrue(can_move_piece_at((2,1)))
self.assertFalse(can_move_piece_at((0,0)))
self.assertFalse(can_move_piece_at((0,3)))
def test_has_some_legal_move_somewhere(self):
set_board([ [_, _, _, M, _],
[_, R, _, M, _],
[_, _, M, _, R],
[_, R, _, _, _],
[_, _, _, R, _] ] )
self.assertFalse(has_some_legal_move_somewhere('M'))
self.assertTrue(has_some_legal_move_somewhere('R'))
set_board([ [R, R, M, _, _],
[M, M, _, _, _],
[_, _, _, _, _],
[_, _, _, _, _],
[_, _, _, _, _] ] )
self.assertFalse(has_some_legal_move_somewhere('R'))
self.assertTrue(has_some_legal_move_somewhere('M'))
def test_possible_moves_from(self):
self.assertEqual(sorted(possible_moves_from((0,3))),[])
self.assertEqual(sorted(possible_moves_from((2,3))),['down','right'])
self.assertEqual(sorted(possible_moves_from((2,2))),['left','right','up'])
self.assertEqual(sorted(possible_moves_from((0,0))),[])
self.assertEqual(sorted(possible_moves_from((3,1))),['down','left','right'])
def test_can_move_piece_at(self):
set_board([ [_, _, _, M, R],
[_, _, _, M, M],
[_, _, R, _, _],
[_, _, _, _, _],
[_, _, _, _, _] ] )
self.assertTrue(can_move_piece_at((0,3)))
self.assertTrue(can_move_piece_at((2,2)))
self.assertFalse(can_move_piece_at((0,4)))
def test_is_legal_location(self):
self.assertTrue(is_legal_location((0,0)))
self.assertFalse(is_legal_location((0,-1)))
self.assertFalse(is_legal_location((-2,-1)))
def test_is_within_board(self):
self.assertTrue(is_within_board((2,2),'left'))
self.assertFalse(is_within_board((0,3),'up'))
def test_all_possible_moves_for(self):
set_board([ [_, _, R, M, R],
[_, _, _, M, M],
[_, _, _, _, _],
[_, _, _, _, _],
[_, _, _, _, _] ] )
self.assertEqual(sorted(all_possible_moves_for('M')), [((0,3), 'left'),((0,3), 'right'),((1,4), 'up')])
self.assertEqual(sorted(all_possible_moves_for('R')), [((0,2), 'down'),((0,2), 'left')])
def test_make_move(self):
make_move((1,2),'left')
self.assertEqual([ [_, _, _, M, _],
[_, R, _, M, _],
[_, R, M, R, _],
[_, R, _, _, _],
[_, _, _, R, _] ],
get_board())
make_move((2,2),'right')
self.assertEqual([ [_, _, _, M, _],
[_, R, _, M, _],
[_, R, _, M, _],
[_, R, _, _, _],
[_, _, _, R, _] ],
get_board())
def test_choose_computer_move(self):
set_last_move()
self.assertTrue(choose_computer_move('M'))
self.assertTrue(choose_computer_move('R'))
# Replace with tests; should work for both 'M' and 'R'
def test_is_enemy_win(self):
set_board([ [_, _, _, M, R],
[_, _, _, M, _],
[_, _, R, M, _],
[_, _, _, _, _],
[_, _, _, _, _] ] )
self.assertTrue(is_enemy_win())
set_board([ [_, _, _, M, R],
[_, _, _, _, _],
[_, _, R, M, _],
[_, M, _, _, _],
[_, _, _, _, _] ] )
self.assertFalse(is_enemy_win())
set_board([ [_, M, M, M, R],
[_, _, _, _, _],
[_, _, R, _, _],
[_, _, _, _, _],
[_, _, _, _, _] ] )
self.assertTrue(is_enemy_win())
# Replace with tests
unittest.main()