-
Notifications
You must be signed in to change notification settings - Fork 0
/
gravitrips_test.py
215 lines (185 loc) · 4.92 KB
/
gravitrips_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
import itertools
from contextlib import contextmanager
from textwrap import dedent
import pytest
from gravitrips import Gravitrips, InvalidInput
@contextmanager
def does_not_raise():
yield
@pytest.mark.parametrize(
"moves, players, expected",
(
# Two players
(0, 2, 0),
(1, 2, 1),
(2, 2, 0),
(3, 2, 1),
# Three players
(0, 3, 0),
(1, 3, 1),
(2, 3, 2),
(3, 3, 0),
(4, 3, 1),
(5, 3, 2),
),
)
def test_active_player_id(moves, players, expected):
game = Gravitrips(players=players)
game.moves = moves
assert game.active_player_id == expected
@pytest.mark.parametrize(
"moves, players, expected",
(
# Two players
(0, 2, "A"),
(1, 2, "B"),
(2, 2, "A"),
# Three players
(0, 3, "A"),
(1, 3, "B"),
(2, 3, "C"),
(3, 3, "A"),
(4, 3, "B"),
(5, 3, "C"),
),
)
def test_active_player_name(moves, players, expected):
game = Gravitrips(players=players)
game.moves = moves
assert game.active_player_name == expected
@pytest.mark.parametrize(
"stdin_str, expected_exception, expected_result",
(
("-1", pytest.raises(InvalidInput, match="The input must be a number"), None),
("0", pytest.raises(InvalidInput, match="The input must be a number"), None),
("1", does_not_raise(), 1),
("7", does_not_raise(), 7),
("8", pytest.raises(InvalidInput, match="The input must be a number"), None),
("1000", pytest.raises(InvalidInput, match="The input must be a number"), None),
),
)
def test_request_input(monkeypatch, stdin_str, expected_exception, expected_result):
game = Gravitrips()
monkeypatch.setattr("builtins.input", lambda x: stdin_str)
with expected_exception:
assert expected_result == game.request_input()
def parse_grid(grid_text):
def encode_cell(s):
return None if s == "." else ord(s) - 65
return [
[encode_cell(col) for col in row]
for row in reversed(grid_text.strip().split("\n"))
]
grid_north_line = dedent(
"""\
.......
.......
A......
A.....B
A.....B
A.....B
"""
)
grid_north_east_line = dedent(
"""\
.......
.......
...A...
..AB...
.ABB...
ABBBAAA
"""
)
grid_east_line = dedent(
"""\
.......
.......
.......
.......
.......
AAAABBB
"""
)
grid_south_east_line = dedent(
"""\
.......
.......
...A...
...BA..
...BBA.
AAABBBA
"""
)
@pytest.mark.parametrize(
"last_move, grid_text",
(
((0, 3), grid_north_line),
((0, 0), grid_north_east_line),
((1, 1), grid_north_east_line),
((2, 2), grid_north_east_line),
((3, 3), grid_north_east_line),
((0, 0), grid_east_line),
((1, 0), grid_east_line),
((2, 0), grid_east_line),
((3, 0), grid_east_line),
((6, 0), grid_south_east_line),
((5, 1), grid_south_east_line),
((4, 2), grid_south_east_line),
((3, 3), grid_south_east_line),
),
)
def test_compute_winner(last_move, grid_text):
game = Gravitrips()
game.grid = parse_grid(grid_text)
game.compute_winner(*last_move)
assert game.winner == "A"
@pytest.mark.parametrize(
"direction, last_move, grid_text, expected_cells",
(
(
Gravitrips.DIRECTIONS[0],
(0, 3),
grid_north_line,
{(0, 0), (0, 1), (0, 2), (0, 3)},
),
(
Gravitrips.DIRECTIONS[1],
(0, 0),
grid_north_east_line,
{(0, 0), (1, 1), (2, 2), (3, 3)},
),
(
Gravitrips.DIRECTIONS[2],
(0, 0),
grid_east_line,
{(0, 0), (1, 0), (2, 0), (3, 0)},
),
(
Gravitrips.DIRECTIONS[3],
(6, 0),
grid_south_east_line,
{(6, 0), (5, 1), (4, 2), (3, 3)},
),
),
)
def test_get_last_connected_cells(direction, last_move, grid_text, expected_cells):
game = Gravitrips()
game.grid = parse_grid(grid_text)
connected_cells = set(game.get_connected_cells(direction, *last_move))
assert connected_cells == expected_cells
@pytest.mark.parametrize(
"player_a_moves, player_b_moves, expected_winner",
(
((1, 2, 3, 4, 5, 6, 7), (1, 2, 3, 4, 5, 6, 7), "A"),
((1, 2, 3, 4, 5, 6, 7), (2, 3, 4, 5, 6, 7, 1), "B"),
((7, 6, 5, 4, 3, 2, 1), (1, 2, 3, 4, 5, 6, 7), "A"),
((1, 1, 1, 1), (2, 2, 2, 2), "A"),
),
)
def test_game(monkeypatch, player_a_moves, player_b_moves, expected_winner):
moves = list(reversed(list(itertools.chain(*zip(player_a_moves, player_b_moves)))))
monkeypatch.setattr("builtins.input", lambda x: moves.pop())
monkeypatch.setattr("os.system", lambda x: None)
game = Gravitrips()
game.start()
assert game.winner == expected_winner