-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ninemensmorris.py
228 lines (213 loc) · 7.77 KB
/
test_ninemensmorris.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
import ninemensmorris as nmm
import pytest
# Making sure we detect wins due to lack of moves correctly
def test_no_move_win():
state = nmm.NineMensMorris();
state.board[0][0] = 1;
state.board[0][1] = 1;
state.board[0][2] = 1;
state.board[0][7] = 2;
state.board[1][1] = 2;
state.board[0][3] = 2;
state.white_pieces_on_board = 3;
state.black_pieces_on_board = 3;
state.white_pieces_avail = 0;
state.black_pieces_avail = 0;
state.white_phase = 3;
state.black_phase = 3;
assert(len(state.getValidMoves(1)) == 3 * 18); # Both player have moves and no one has won
assert(len(state.getValidMoves(2)) == 3 * 18);
assert(not state.isWin(1));
assert(not state.isWin(2));
state.board[0][6] = 1;
state.board[0][5] = 2;
state.white_phase = 2;
state.black_phase = 2;
state.white_pieces_on_board = 4;
state.black_pieces_on_board = 5;
assert(len(state.getValidMoves(1)) == 0); # Player 1 can't move, so player 2 has won
assert(len(state.getValidMoves(2)) == 8);
assert(not state.isWin(1));
assert(state.isWin(2));
# Testing that variables are updated properly
def test_variable_update():
state = nmm.NineMensMorris();
state = state.move(None, (2, 7), None, 1);
assert(state.white_pieces_avail == 8);
assert(state.white_pieces_on_board == 1);
assert(state.white_phase == 1);
expandedStates = state.getValidMoves(2);
assert(len(expandedStates) == 23);
for newState in expandedStates:
assert(newState.white_pieces_avail == 8)
assert(newState.black_pieces_avail == 8)
assert(newState.white_phase == 1)
assert(newState.black_phase == 1)
assert(newState.white_pieces_on_board == 1)
assert(newState.black_pieces_on_board == 1)
# Testing that the correct value is returned
def test_minSlidesToMill():
state = nmm.NineMensMorris();
state.board[0][0] = 1
state.board[0][7] = 1
state.board[0][2] = 1
assert(state.minSlidesToMill(1) == 4)
state.board[0][4] = 2
assert(state.minSlidesToMill(1) == 4)
state.board[1][0] = 2
assert(state.minSlidesToMill(1) == 6)
state.board[1][6] = 2;
assert(state.minSlidesToMill(2) == 6)
assert(state.minSlidesToMill(1) == 6)
# Testing that millDifference returns the right value
def test_millDifference():
state = nmm.NineMensMorris();
assert(state.millDifference(1) == 0)
assert(state.millDifference(2) == 0)
state.board[0][0] = 1;
state.board[0][1] = 1;
state.board[0][2] = 1;
assert(state.millDifference(1) == 1)
assert(state.millDifference(2) == -1)
state.board[1][0] = 1;
state.board[1][1] = 1;
state.board[1][2] = 1;
assert(state.millDifference(1) == 2)
assert(state.millDifference(2) == -2)
state.board[2][0] = 2;
state.board[2][7] = 2;
state.board[2][6] = 2;
assert(state.millDifference(1) == 1)
assert(state.millDifference(2) == -1)
# Test that blockedInDifference returns the correct value
def test_blockedInDifference():
state = nmm.NineMensMorris();
assert(state.blockedInDifference(1) == 0)
assert(state.blockedInDifference(2) == 0)
state.board[0][0] = 1;
state.board[0][7] = 2;
state.board[0][1] = 2;
assert(state.blockedInDifference(1) == -1)
assert(state.blockedInDifference(2) == 1)
state.board[1][1] = 2;
state.board[0][2] = 2;
state.printBoard()
assert(state.blockedInDifference(1) == 0)
assert(state.blockedInDifference(2) == 0)
state.board[1][5] = 2;
state.board[2][5] = 1;
state.board[0][5] = 1;
state.board[1][6] = 1;
state.board[1][4] = 1;
assert(state.blockedInDifference(1) == 1)
assert(state.blockedInDifference(2) == -1)
# Test that spammableMillPiecesDifference returns the correct value
def test_spammableMillPiecesDifference():
state = nmm.NineMensMorris();
assert(state.spammableMillPiecesDifference(1) == 0)
assert(state.spammableMillPiecesDifference(2) == 0)
state.board[0][0] = 1;
state.board[0][1] = 1;
state.board[0][2] = 1;
assert(state.spammableMillPiecesDifference(1) == 3)
assert(state.spammableMillPiecesDifference(2) == -3)
state.board[1][0] = 1;
state.board[1][2] = 1;
state.board[1][1] = 1;
assert(state.spammableMillPiecesDifference(1) == 5)
assert(state.spammableMillPiecesDifference(2) == -5)
state.board[1][6] = 2;
state.board[1][5] = 2;
state.board[1][4] = 2;
state.board[2][6] = 2;
state.board[2][4] = 2;
state.board[2][3] = 2;
state.board[2][2] = 2;
assert(state.spammableMillPiecesDifference(1) == -1)
assert(state.spammableMillPiecesDifference(2) == 1)
state.board[2][1] = 2;
assert(state.spammableMillPiecesDifference(1) == -1)
assert(state.spammableMillPiecesDifference(2) == 1)
# Test that numIntersectionsHeld returns the correct value
def test_numIntersectionsHeld():
state = nmm.NineMensMorris()
assert(state.numIntersectionsHeld(1) == 0)
assert(state.numIntersectionsHeld(2) == 0)
state.board[0][0] = 1;
state.board[0][1] = 1;
state.board[0][2] = 2;
state.board[0][3] = 2;
assert(state.numIntersectionsHeld(1) == 1)
assert(state.numIntersectionsHeld(2) == 1)
state.board[2][1] = 1;
state.board[1][5] = 2;
assert(state.numIntersectionsHeld(1) == 2)
assert(state.numIntersectionsHeld(2) == 2)
# Test that numDoubleMills returns the correct values
def test_numDoubleMills():
state = nmm.NineMensMorris()
assert(state.numDoubleMills(1) == 0)
assert(state.numDoubleMills(2) == 0)
state.board[0][0] = 1;
state.board[0][1] = 1;
state.board[0][2] = 1;
state.board[1][0] = 1;
state.board[1][2] = 1;
assert(state.numDoubleMills(1) == 1)
assert(state.numDoubleMills(2) == 0)
state.board[0][5] = 2;
state.board[1][5] = 2;
state.board[2][5] = 2;
state.board[2][2] = 2;
state.board[2][3] = 2;
assert(state.numDoubleMills(1) == 1)
assert(state.numDoubleMills(2) == 1)
state.board[0][3] = 2;
state.board[1][4] = 2;
state.board[1][6] = 2;
assert(state.numDoubleMills(1) == 1)
assert(state.numDoubleMills(2) == 2)
state.board[2][4] = 1;
assert(state.numDoubleMills(1) == 1)
assert(state.numDoubleMills(2) == 1)
#Test that num of 3 piece configurations are calculated properly
def test_3Piece_configs():
state = nmm.NineMensMorris()
assert(state.num3PieceConfigs(1) == 0)
for i in range(0,3):
for j in range(0,8):
state.board[i][j] = 1
assert(state.num3PieceConfigs(1) == 44)
@pytest.mark.parametrize(
"board, player, num", [
([[1,1,1,1,1,1,1,1], [0,0,0,0,0,0,0,0], [1,0,1,0,1,0,1,0]], 1, 8),
([[1,1,1,1,1,1,1,1], [0,0,0,0,0,0,0,0], [1,0,1,0,1,0,1,0]], 0, 8),
])
def test_num2PieceConfigs(board, player, num):
state = nmm.NineMensMorris()
state.board = board
assert state.num2PieceConfigs(player) == num
@pytest.mark.parametrize(
"board, player, num", [
([[1,1,1,1,1,1,1,1], [0,0,0,0,0,0,0,0], [1,0,1,0,1,0,1,0]], 1, 8),
([[1,1,1,1,1,1,1,1], [0,0,0,0,0,0,0,0], [1,0,1,0,1,0,1,0]], 0, 4),
])
def test_numNonOppositeCorners(board, player, num):
state = nmm.NineMensMorris()
state.board = board
assert state.numNonOppositeCorners(player) == num
@pytest.mark.parametrize(
"board, player, pw, pb, num", [
([[1,0,0,0,0,0,1,1], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0]], 1, 1, 2, 1),
([[2,0,0,0,0,0,2,2], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0]], 2, 2, 1, 1)
])
def test_closedMill(board, player, pw, pb, num):
state = nmm.NineMensMorris()
state.board = board
for i in range(0,3):
for j in range(0,8):
if (state.board[i][j] == 1):
state.white_pieces_on_board += 1
elif (state.board[i][j] == 2):
state.black_pieces_on_board += 1
assert state.closedMill(player, pw, pb) == num