OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 library pop_pop_win.game_test; | |
5 | |
6 import 'dart:math'; | |
7 | |
8 import 'package:pop_pop_win/src/game.dart'; | |
9 import 'package:unittest/unittest.dart'; | |
10 | |
11 import 'test_util.dart'; | |
12 | |
13 void main() { | |
14 test('initial values', _testInitial); | |
15 test('setFlag', _testSetFlag); | |
16 test('cannot reveal flagged', _testCannotRevealFlagged); | |
17 test('cannot flag revealed', _testCannotFlagRevealed); | |
18 test('reveal zero', _testRevealZero); | |
19 test('loss', _testLoss); | |
20 test('win', _testWin); | |
21 test('random winner', _testRandomField); | |
22 test('good chord', _testGoodChord); | |
23 test('bad chord', _testBadChord); | |
24 test('no-op chord', _testNoopChord); | |
25 test('canReveal', _testCanReveal); | |
26 test('canFlag', _testCanFlag); | |
27 test('cannot re-reveal', _testCannotReReveal); | |
28 } | |
29 | |
30 void _testCannotReReveal() { | |
31 var f = getSampleField(); | |
32 var g = new Game(f); | |
33 | |
34 expect(g.canReveal(5, 3), isTrue); | |
35 var r = g.reveal(5, 3); | |
36 | |
37 g.setFlag(4, 2, true); | |
38 | |
39 expect(g.canReveal(5, 3), isTrue); | |
40 r = g.reveal(5, 3); | |
41 | |
42 expect(g.canReveal(5, 3), isFalse); | |
43 } | |
44 | |
45 void _testCanFlag() { | |
46 var f = getSampleField(); | |
47 var g = new Game(f); | |
48 | |
49 expect(g.canToggleFlag(0, 0), isTrue); | |
50 expect(g.state, GameState.reset); | |
51 g.setFlag(0, 0, true); | |
52 expect(g.state, GameState.started); | |
53 expect(g.canToggleFlag(0, 0), isTrue); | |
54 g.setFlag(0, 0, false); | |
55 expect(g.canToggleFlag(0, 0), isTrue); | |
56 | |
57 expect(g.canToggleFlag(5, 4), isTrue); | |
58 g.reveal(5, 4); | |
59 expect(g.canToggleFlag(5, 4), isFalse); | |
60 } | |
61 | |
62 void _testCanReveal() { | |
63 var f = getSampleField(); | |
64 var g = new Game(f); | |
65 | |
66 expect(g.canReveal(0, 0), isTrue); | |
67 expect(g.state, GameState.reset); | |
68 g.setFlag(0, 0, true); | |
69 expect(g.state, GameState.started); | |
70 expect(g.canReveal(0, 0), isFalse); | |
71 | |
72 expect(g.canReveal(5, 4), isTrue); | |
73 g.reveal(5, 4); | |
74 expect(g.canReveal(5, 4), isFalse); | |
75 | |
76 g.setFlag(4, 2, true); | |
77 expect(g.canReveal(5, 3), isTrue); | |
78 expect(g.canReveal(4, 3), isFalse); | |
79 g.setFlag(3, 2, true); | |
80 expect(g.canReveal(4, 3), isTrue); | |
81 | |
82 // now we'll over flag | |
83 expect(g.canReveal(5, 3), isTrue); | |
84 g.setFlag(5, 2, true); | |
85 expect(g.canReveal(5, 3), isFalse); | |
86 } | |
87 | |
88 void _testBadChord() { | |
89 var f = getSampleField(); | |
90 var g = new Game(f); | |
91 | |
92 expect(g.bombsLeft, equals(13)); | |
93 var startReveals = f.length - 13; | |
94 expect(g.revealsLeft, equals(startReveals)); | |
95 expect(g.state, equals(GameState.reset)); | |
96 | |
97 g.reveal(2, 3); | |
98 g.setFlag(1, 2, true); | |
99 g.setFlag(3, 2, true); | |
100 | |
101 expect(g.bombsLeft, equals(11)); | |
102 expect(g.revealsLeft, equals(startReveals - 1)); | |
103 | |
104 var revealed = g.reveal(2, 3); | |
105 expect(revealed, isNull); | |
106 expect(g.state, equals(GameState.lost)); | |
107 } | |
108 | |
109 // Adjacent flag count != square count | |
110 // so nothing happens | |
111 void _testNoopChord() { | |
112 var f = getSampleField(); | |
113 var g = new Game(f); | |
114 | |
115 expect(g.bombsLeft, equals(13)); | |
116 var startReveals = f.length - 13; | |
117 expect(g.revealsLeft, equals(startReveals)); | |
118 expect(g.state, equals(GameState.reset)); | |
119 | |
120 var revealed = g.reveal(2, 3); | |
121 expect(revealed, unorderedEquals([const Point(2, 3)])); | |
122 | |
123 g.setFlag(2, 2, true); | |
124 | |
125 expect(g.bombsLeft, equals(12)); | |
126 expect(g.revealsLeft, equals(startReveals - 1)); | |
127 | |
128 expect(() => g.reveal(2, 3), throwsException); | |
129 } | |
130 | |
131 void _testGoodChord() { | |
132 var f = getSampleField(); | |
133 var g = new Game(f); | |
134 | |
135 expect(g.bombsLeft, equals(13)); | |
136 var startReveals = f.length - 13; | |
137 expect(g.revealsLeft, equals(startReveals)); | |
138 expect(g.state, equals(GameState.reset)); | |
139 | |
140 g.reveal(2, 3); | |
141 g.setFlag(2, 2, true); | |
142 g.setFlag(3, 2, true); | |
143 | |
144 expect(g.bombsLeft, equals(11)); | |
145 expect(g.revealsLeft, equals(startReveals - 1)); | |
146 | |
147 g.reveal(2, 3); | |
148 expect(g.bombsLeft, equals(11)); | |
149 expect(g.revealsLeft, equals(startReveals - 11)); | |
150 expect(g.duration, isNot(isNull)); | |
151 } | |
152 | |
153 // Test 5 random fields five times | |
154 void _testRandomField() { | |
155 var rnd = new Random(); | |
156 for (int i = 0; i < 5; i++) { | |
157 var f = new Field(); | |
158 | |
159 for (int j = 0; j < 5; j++) { | |
160 var g = new Game(f); | |
161 while (g.revealsLeft > 0) { | |
162 var x = rnd.nextInt(f.width); | |
163 var y = rnd.nextInt(f.height); | |
164 if (g.getSquareState(x, y) == SquareState.hidden) { | |
165 if (f.get(x, y)) { | |
166 g.setFlag(x, y, true); | |
167 } else if (!f.get(x, y)) { | |
168 g.reveal(x, y); | |
169 } | |
170 } | |
171 } | |
172 expect(g.state == GameState.won, isTrue); | |
173 } | |
174 } | |
175 } | |
176 | |
177 void _testRevealZero() { | |
178 var f = getSampleField(); | |
179 var g = new Game(f); | |
180 | |
181 expect(g.bombsLeft, equals(13)); | |
182 var startReveals = f.length - 13; | |
183 expect(g.revealsLeft, equals(startReveals)); | |
184 expect(g.state, equals(GameState.reset)); | |
185 | |
186 g.reveal(5, 4); | |
187 expect(g.revealsLeft, equals(startReveals - 10)); | |
188 } | |
189 | |
190 void _testInitial() { | |
191 var f = getSampleField(); | |
192 var g = new Game(f); | |
193 | |
194 expect(g.bombsLeft, equals(13)); | |
195 expect(g.revealsLeft, equals(f.length - 13)); | |
196 expect(g.state, equals(GameState.reset)); | |
197 expect(g.duration, isNull); | |
198 | |
199 for (int x = 0; x < f.width; x++) { | |
200 for (int y = 0; y < f.height; y++) { | |
201 expect(g.getSquareState(x, y), equals(SquareState.hidden)); | |
202 } | |
203 } | |
204 } | |
205 | |
206 void _testSetFlag() { | |
207 var g = new Game(getSampleField()); | |
208 | |
209 expect(g.getSquareState(0, 0), equals(SquareState.hidden)); | |
210 g.setFlag(0, 0, true); | |
211 expect(g.getSquareState(0, 0), equals(SquareState.flagged)); | |
212 expect(g.bombsLeft, equals(12)); | |
213 expect(g.state, equals(GameState.started)); | |
214 } | |
215 | |
216 void _testCannotRevealFlagged() { | |
217 var g = new Game(getSampleField()); | |
218 | |
219 expect(g.getSquareState(0, 0), equals(SquareState.hidden)); | |
220 g.setFlag(0, 0, true); | |
221 expect(g.getSquareState(0, 0), equals(SquareState.flagged)); | |
222 expect(g.bombsLeft, equals(12)); | |
223 expect(g.state, equals(GameState.started)); | |
224 | |
225 expect(() => g.reveal(0, 0), throwsException); | |
226 } | |
227 | |
228 void _testCannotFlagRevealed() { | |
229 var g = new Game(getSampleField()); | |
230 | |
231 expect(g.getSquareState(1, 1), equals(SquareState.hidden)); | |
232 g.reveal(1, 1); | |
233 expect(g.getSquareState(1, 1), equals(SquareState.revealed)); | |
234 expect(g.state, equals(GameState.started)); | |
235 | |
236 expect(() => g.setFlag(1, 1, true), throwsException); | |
237 } | |
238 | |
239 void _testLoss() { | |
240 var g = new Game(getSampleField()); | |
241 | |
242 expect(g.getSquareState(0, 0), equals(SquareState.hidden)); | |
243 var revealed = g.reveal(0, 0); | |
244 expect(revealed, isNull); | |
245 expect(g.state, equals(GameState.lost)); | |
246 expect(g.getSquareState(0, 0), equals(SquareState.bomb)); | |
247 } | |
248 | |
249 void _testWin() { | |
250 var f = getSampleField(); | |
251 var g = new Game(f); | |
252 | |
253 int bombsLleft = f.bombCount; | |
254 expect(g.revealsLeft, equals(f.length - 13)); | |
255 int revealsLeft = g.revealsLeft; | |
256 for (int x = 0; x < f.width; x++) { | |
257 for (int y = 0; y < f.height; y++) { | |
258 if (f.get(x, y)) { | |
259 g.setFlag(x, y, true); | |
260 bombsLleft--; | |
261 expect(g.bombsLeft, equals(bombsLleft)); | |
262 } else if (g.getSquareState(x, y) == SquareState.hidden) { | |
263 revealsLeft -= g.reveal(x, y).length; | |
264 expect(revealsLeft, equals(g.revealsLeft)); | |
265 } else { | |
266 expect(g.getSquareState(x, y), equals(SquareState.revealed)); | |
267 } | |
268 expect(g.state, isNot(equals(GameState.reset))); | |
269 expect(g.state, isNot(equals(GameState.lost))); | |
270 } | |
271 } | |
272 | |
273 expect(g.state, equals(GameState.won)); | |
274 } | |
OLD | NEW |