-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_game.py
169 lines (139 loc) · 4.86 KB
/
test_game.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
import pytest
from game import Dot, GameInstance, _Tile
class TestTile(object):
tile1 = _Tile()
tile2 = _Tile()
tile3 = _Tile()
tile1.next = tile2
tile2.next = tile3
def test_end_returns_self(self):
assert self.tile3.line_end() == self.tile3
def test_follow_one_link(self):
assert self.tile2.line_end() == self.tile3
def test_follow_multi_link(self):
assert self.tile1.line_end() == self.tile3
class TestGameInstance(object):
valid_dots = [Dot(1, 1, 'blue'), Dot(2, 2, 'red'), Dot(3, 3, 'blue'), Dot(0, 0, 'red')]
colliding_dots = valid_dots[1:] + [Dot(2, 2, 'blue')]
no_matching_dot = valid_dots[:-1]
dim = 5
def test_valid_game_board(self):
game = GameInstance(self.dim, self.valid_dots)
assert isinstance(game, GameInstance)
assert len(game.board) == self.dim
assert len(game.board[0]) == self.dim
for dot in self.valid_dots:
tile = game.board[dot.x][dot.y]
assert tile.is_dot
assert dot.color == tile.color
def test_invalid_game_boards(self):
with pytest.raises(ValueError):
GameInstance(self.dim, self.colliding_dots)
with pytest.raises(ValueError):
GameInstance(self.dim, self.no_matching_dot)
def test_valid_color_tile(self):
game = GameInstance(self.dim, self.valid_dots)
# tile at (1,1) is a blue dot
game.color_tile((1, 1), (1, 2))
assert game.board[1][2].color == 'blue'
assert game.board[1][1].next == game.board[1][2]
assert not game.board[1][2].next
game.color_tile((1, 2), (1, 3))
assert game.board[1][2].next == game.board[1][3]
assert game.board[1][3].color == 'blue'
assert not game.board[1][3].next
game.color_tile((1, 3), (2, 3))
# finish the flow
game.color_tile((2, 3), (3, 3))
assert game.board[3][3].color == 'blue'
assert game.board[2][3].next == game.board[3][3]
assert game.board[1][1].line_end() == game.board[3][3]
#interrupt another flow
game.color_tile((2, 2), (1, 2))
assert game.board[1][2].color == 'red'
assert not game.board[1][2].next
assert game.board[1][1].line_end() == game.board[1][1]
assert not game.board[1][3].next
def test_invalid_color_tile(self):
game = GameInstance(self.dim, self.valid_dots)
game.color_tile((1, 1), (1, 2))
# write on wrong colored dot
with pytest.raises(ValueError):
game.color_tile((1, 2), (2, 2))
# terminate at starting dot
with pytest.raises(ValueError):
game.color_tile((1, 2), (1, 1))
# draw off the game board
with pytest.raises(IndexError):
game.color_tile((0, 0), (-1, 0))
# draw nonadjacent tile
with pytest.raises(ValueError):
game.color_tile((0, 0), (1, 1))
# draw tile not from end of line
with pytest.raises(ValueError):
game.color_tile((1, 1), (0, 1))
def test_remove_line(self):
game = GameInstance(self.dim, self.valid_dots)
game.remove_line((1, 1))
assert game.board[1][1].line_end() == game.board[1][1]
game.board[1][1].next = game.board[1][2]
game.board[1][2].color = 'blue'
game.board[1][2].next = game.board[1][3]
game.board[1][3].color = 'blue'
game.remove_line((1, 1))
assert game.board[1][1].line_end() == game.board[1][1]
game.board[1][1].next = game.board[1][2]
game.board[1][2].color = 'blue'
game.board[1][2].next = game.board[1][3]
game.board[1][3].color = 'blue'
game.remove_line((1, 2))
assert game.board[1][1].line_end() == game.board[1][2]
def test_previous(self):
game = GameInstance(self.dim, self.valid_dots)
game.board[1][1].next = game.board[1][2]
game.board[1][2].color = 'blue'
game.board[1][2].next = game.board[1][3]
game.board[1][3].color = 'blue'
game.board[2][3].color = 'blue'
assert game._previous((1,3)) == (1,2)
assert game._previous((1,2)) == (1,1)
assert game._previous((1,1)) == None
assert game._previous((2,3)) == None
def test_won(self):
winnable_dots = [Dot(0, 0, 'green'), Dot(3, 3, 'green'),
Dot(1, 0, 'yellow'), Dot(3, 0, 'yellow'),
Dot(4, 0, 'orange'), Dot(2, 2, 'orange'),
Dot(0, 2, 'red'), Dot(3, 4, 'red'),
Dot(3, 2, 'blue'), Dot(4, 4, 'blue')]
game = GameInstance(self.dim, winnable_dots)
assert not game.game_won()
# green
game.color_tile((0, 0), (0, 1))
game.color_tile((0, 1), (1, 1))
game.color_tile((1, 1), (1, 2))
game.color_tile((1, 2), (1, 3))
game.color_tile((1, 3), (2, 3))
game.color_tile((2, 3), (3, 3))
assert not game.game_won()
# yellow
game.color_tile((1, 0), (2, 0))
game.color_tile((2, 0), (3, 0))
assert not game.game_won()
# orange
game.color_tile((4, 0), (4, 1))
game.color_tile((4, 1), (3, 1))
game.color_tile((3, 1), (2, 1))
game.color_tile((2, 1), (2, 2))
assert not game.game_won()
# blue
game.color_tile((3, 2), (4, 2))
game.color_tile((4, 2), (4, 3))
game.color_tile((4, 3), (4, 4))
assert not game.game_won()
# red
game.color_tile((0, 2), (0, 3))
game.color_tile((0, 3), (0, 4))
game.color_tile((0, 4), (1, 4))
game.color_tile((1, 4), (2, 4))
game.color_tile((2, 4), (3, 4))
assert game.game_won()