KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > sampleproject > connectfour > PlayingStandTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2005, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.sampleproject.connectfour;
38
39 import junit.framework.TestCase;
40
41 public class PlayingStandTest extends TestCase {
42
43     public void testFourConnected() throws GameOverException {
44         PlayingStand stand = new PlayingStand();
45         assertFalse(stand.areFourConnected());
46
47         stand.dropRed(0);
48         stand.dropBlack(6);
49         assertFalse(stand.areFourConnected());
50
51         stand.dropRed(0);
52         stand.dropBlack(6);
53         assertFalse(stand.areFourConnected());
54
55         stand.dropRed(0);
56         stand.dropBlack(6);
57         assertFalse(stand.areFourConnected());
58
59         stand.dropRed(0);
60         assertTrue(stand.areFourConnected());
61
62         try {
63             stand.dropBlack(6);
64             fail("Game is over, should be an exception");
65         } catch (GameOverException expected) {
66         }
67     }
68
69     public void testFourConnectedHorizontally() throws GameOverException {
70         PlayingStand stand = new PlayingStand();
71         createRedWinsHorizontally(stand);
72         assertTrue(stand.areFourConnected());
73
74         try {
75             stand.dropBlack(6);
76             fail("Game is over, should be an exception");
77         } catch (GameOverException expected) {
78         }
79     }
80
81     private void createRedWinsHorizontally(PlayingStand stand) {
82         assertFalse(stand.areFourConnected());
83
84         stand.dropRed(0);
85         stand.dropBlack(6);
86         assertFalse(stand.areFourConnected());
87
88         stand.dropRed(1);
89         stand.dropBlack(6);
90         assertFalse(stand.areFourConnected());
91
92         stand.dropRed(2);
93         stand.dropBlack(6);
94         assertFalse(stand.areFourConnected());
95
96         stand.dropRed(3);
97     }
98
99     public void testFourConnectedDiagonally() throws GameOverException {
100         PlayingStand stand = new PlayingStand();
101         createRedWinsDiagonallyUpward(stand);
102         assertTrue(stand.areFourConnected());
103
104         try {
105             stand.dropBlack(6);
106             fail("Game is over, should be an exception");
107         } catch (GameOverException expected) {
108         }
109
110         assertEquals(Chip.RED, stand.getWinner());
111     }
112
113     public void testStandControlsTurns() throws GameOverException {
114         PlayingStand stand = new PlayingStand();
115         stand.dropRed(1);
116         try {
117             stand.dropRed(1);
118             fail("Expected an exception");
119         } catch (OutOfTurnException expected) {
120         }
121
122         stand.dropBlack(1);
123         try {
124             stand.dropBlack(1);
125             fail("Expected an exception");
126         } catch (OutOfTurnException expected) {
127         }
128
129         stand.dropRed(1);
130         stand.dropBlack(2);
131     }
132
133     public void testFullColumn() {
134         PlayingStand stand = new PlayingStand();
135         stand.dropRed(0);
136         stand.dropBlack(0);
137         stand.dropRed(0);
138         stand.dropBlack(0);
139         stand.dropRed(0);
140         stand.dropBlack(0);
141
142         try {
143             stand.dropRed(0);
144             fail("Expected an exception");
145         } catch (FullColumnException expected) {
146         }
147     }
148
149     public void testNonExistentColumn() {
150         PlayingStand stand = new PlayingStand();
151         try {
152             stand.dropRed(-1);
153             fail("Expected an exception");
154         } catch (InvalidColumnException expected) {
155         }
156
157         try {
158             stand.dropBlack(-1);
159             fail("Expected an exception");
160         } catch (InvalidColumnException expected) {
161         }
162
163         try {
164             stand.dropRed(7);
165             fail("Expected an exception");
166         } catch (InvalidColumnException expected) {
167         }
168
169         try {
170             stand.dropRed(8);
171             fail("Expected an exception");
172         } catch (InvalidColumnException expected) {
173         }
174
175         try {
176             stand.dropRed(10000);
177             fail("Expected an exception");
178         } catch (InvalidColumnException expected) {
179         }
180
181         try {
182             stand.dropRed(Integer.MAX_VALUE);
183             fail("Expected an exception");
184         } catch (InvalidColumnException expected) {
185         }
186
187         try {
188             stand.dropRed(Integer.MIN_VALUE);
189             fail("Expected an exception");
190         } catch (InvalidColumnException expected) {
191         }
192     }
193
194     public void testNoWinner() {
195         PlayingStand stand = new PlayingStand();
196
197         fillWholeStandWithoutWinner(stand);
198
199         assertFalse(stand.areFourConnected());
200         assertTrue(stand.isGameOver());
201         assertNull(stand.getWinner());
202
203         try {
204             stand.dropRed(0);
205             fail("Expected an exception");
206         } catch (GameOverException expected) {
207         }
208     }
209
210     /**
211      * 5|
212      * 4|
213      * 3| R
214      * 2| B R
215      * 1| R B R
216      * 0| B R B R B
217      * --------------
218      * 0 1 2 3 4 5 6
219      */

220     public void testDownwardDiagonalWins() {
221         PlayingStand stand = new PlayingStand();
222         stand.dropRed(3);
223         stand.dropBlack(2);
224         stand.dropRed(2);
225         stand.dropBlack(0);
226         stand.dropRed(1);
227         stand.dropBlack(1);
228         stand.dropRed(1);
229         stand.dropBlack(4);
230         stand.dropRed(0);
231         stand.dropBlack(0);
232         stand.dropRed(0);
233
234         assertTrue(stand.areFourConnected());
235         assertTrue(stand.isGameOver());
236     }
237
238     public void testWinningPlacement() {
239         PlayingStand stand = new PlayingStand();
240         createRedWinsDiagonallyUpward(stand);
241
242         PlayingStand.WinningPlacement placement = stand.getWinningPlacement();
243         assertNotNull(placement);
244
245         Cell startCell = placement.getStartingCell();
246         assertEquals(0, startCell.getColumn());
247         assertEquals(0, startCell.getRow());
248         assertEquals(Direction.UPWARD_DIAGONAL, placement.getDirection());
249
250         stand = new PlayingStand();
251         createRedWinsHorizontally(stand);
252
253         placement = stand.getWinningPlacement();
254         assertEquals(0, placement.getStartingCell().getColumn());
255         assertEquals(0, placement.getStartingCell().getRow());
256         assertEquals(Direction.HORIZONTAL, placement.getDirection());
257     }
258
259     public void testNoWinningPlacementBeforeGameOver() {
260         PlayingStand stand = new PlayingStand();
261
262         try {
263             stand.getWinningPlacement();
264             fail("Expected an exception");
265         } catch (GameNotOverException expected) {
266         }
267
268         fillWholeStandWithoutWinner(stand);
269         try {
270             stand.getWinningPlacement();
271             fail("Expected an exception");
272         } catch (StalemateException expected) {
273         }
274     }
275
276     private void createRedWinsDiagonallyUpward(PlayingStand stand) {
277         assertFalse(stand.areFourConnected());
278
279         stand.dropRed(0);
280         stand.dropBlack(1);
281         assertFalse(stand.areFourConnected());
282
283         stand.dropRed(1);
284         stand.dropBlack(2);
285         assertFalse(stand.areFourConnected());
286
287         stand.dropRed(2);
288         stand.dropBlack(3);
289         stand.dropRed(2);
290         assertFalse(stand.areFourConnected());
291
292         stand.dropBlack(5);
293         stand.dropRed(3);
294         stand.dropBlack(3);
295         stand.dropRed(3);
296     }
297
298     private void fillWholeStandWithoutWinner(PlayingStand stand) {
299         stand.dropRed(0);
300         stand.dropBlack(1);
301         stand.dropRed(0);
302         stand.dropBlack(1);
303         stand.dropRed(0);
304         stand.dropBlack(1);
305
306         stand.dropRed(1);
307         stand.dropBlack(0);
308         stand.dropRed(1);
309         stand.dropBlack(0);
310         stand.dropRed(1);
311         stand.dropBlack(0);
312
313         stand.dropRed(2);
314         stand.dropBlack(3);
315         stand.dropRed(2);
316         stand.dropBlack(3);
317         stand.dropRed(2);
318         stand.dropBlack(3);
319
320         stand.dropRed(3);
321         stand.dropBlack(2);
322         stand.dropRed(3);
323         stand.dropBlack(2);
324         stand.dropRed(3);
325         stand.dropBlack(2);
326
327         stand.dropRed(4);
328         stand.dropBlack(5);
329         stand.dropRed(4);
330         stand.dropBlack(5);
331         stand.dropRed(4);
332         stand.dropBlack(5);
333
334         stand.dropRed(5);
335         stand.dropBlack(4);
336         stand.dropRed(5);
337         stand.dropBlack(4);
338         stand.dropRed(5);
339         stand.dropBlack(4);
340
341         stand.dropRed(6);
342         stand.dropBlack(6);
343         stand.dropRed(6);
344         stand.dropBlack(6);
345         stand.dropRed(6);
346         stand.dropBlack(6);
347     }
348 }
349
Popular Tags