2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * The contents of this file are subject to the terms of either the GNU
5 * General Public License Version 2 only ("GPL") or the Common
6 * Development and Distribution License("CDDL") (collectively, the
7 * "License"). You may not use this file except in compliance with the
8 * License. You can obtain a copy of the License at
9 * http://www.netbeans.org/cddl-gplv2.html
10 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
11 * specific language governing permissions and limitations under the
12 * License. When distributing the software, include this License Header
13 * Notice in each file and include the License file at
14 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
15 * particular file as subject to the "Classpath" exception as provided
16 * by Sun in the GPL Version 2 section of the License file that
17 * accompanied this code. If applicable, add the following below the
18 * License Header, with the fields enclosed by brackets [] replaced by
19 * your own identifying information:
20 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Portions Copyrighted 2009 Jaroslav Tulach
26 package cz.xelfi.quoridor;
28 import cz.xelfi.quoridor.Fence.Orientation;
29 import cz.xelfi.quoridor.Player.Direction;
30 import java.util.List;
31 import junit.framework.TestCase;
35 * @author Jaroslav Tulach <jtulach@netbeans.org>
37 public abstract class BoardCase extends TestCase {
38 protected Board board;
40 protected BoardCase(String n) {
42 board = Board.empty();
45 protected abstract Board move(Board b, int player, Player.Direction... where)
46 throws IllegalPositionException;
48 protected abstract Board fence(Board b, int player, char x, int y, Fence.Orientation orie)
49 throws IllegalPositionException;
51 protected abstract Board apply(Board b, Move move) throws IllegalPositionException;
54 public void testResignWhite() throws IllegalPositionException {
55 List<Player> list = board.getPlayers();
56 assertEquals ("Two", 2, list.size ());
57 Board b = board.apply(Move.RESIGN);
58 assertEquals(b.getPlayers().get(1), b.getWinner());
61 fail("No more moves allowed, the player resigned");
62 } catch (IllegalPositionException ex) {
65 assertNull("No player", b.getCurrentPlayer());
67 public void testResignBlack() throws IllegalPositionException {
68 List<Player> list = board.getPlayers();
69 assertEquals ("Two", 2, list.size ());
70 Board b = board.apply(Move.NORTH).apply(Move.RESIGN);
71 assertEquals(b.getPlayers().get(0), b.getWinner());
74 fail("No more moves allowed, the player resigned");
75 } catch (IllegalPositionException ex) {
78 assertNull("No player", b.getCurrentPlayer());
80 public void testTwoPlayers () {
81 List<Player> list = board.getPlayers();
82 assertEquals ("Two", 2, list.size ());
83 assertFalse ("Both are non-null", list.contains (null));
86 fail ("Modifications are not allowed");
87 } catch (UnsupportedOperationException ex) {
92 fail ("Modifications are not allowed");
93 } catch (UnsupportedOperationException ex) {
98 assertEquals (8, list.get (0).getXInternal());
99 assertEquals (0, list.get (0).getYInternal());
100 assertEquals (10, list.get (0).getFences ());
101 assertEquals (8, list.get (1).getXInternal());
102 assertEquals (16, list.get (1).getYInternal());
103 assertEquals (10, list.get (1).getFences ());
106 public void testFences () throws IllegalPositionException {
107 assertEquals ("No on board", 0, board.getFences ().size ());
109 board.getFences ().add (null);
110 fail ("Should be unmodifiable");
111 } catch (java.lang.UnsupportedOperationException ex) {
116 Board b = board.apply(Move.fence('A', 1, Orientation.HORIZONTAL));
117 assertEquals("One fence placed: ", 1, b.getFences().size());
118 Fence f = b.getFences().iterator().next();
119 assertEquals("Row", 1, f.getRow());
120 assertEquals("Column", 'A', f.getColumn());
124 Board b = board.apply(Move.fence('A', 1, Orientation.VERTICAL));
125 assertEquals("One fence placed: ", 1, b.getFences().size());
126 Fence f = b.getFences().iterator().next();
127 assertEquals("Row", 1, f.getRow());
128 assertEquals("Column", 'A', f.getColumn());
132 Board b = board.apply(Move.fence('H', 8, Orientation.HORIZONTAL));
133 assertEquals("One fence placed: ", 1, b.getFences().size());
134 Fence f = b.getFences().iterator().next();
135 assertEquals("Row", 8, f.getRow());
136 assertEquals("Column", 'H', f.getColumn());
140 Board b = board.apply(Move.fence('H', 8, Orientation.VERTICAL));
141 assertEquals("One fence placed: ", 1, b.getFences().size());
142 Fence f = b.getFences().iterator().next();
143 assertEquals("Row", 8, f.getRow());
144 assertEquals("Column", 'H', f.getColumn());
148 public void testFourTimesInAgainstEachOtherResultsInInvalidPosition () throws Exception {
151 for (int i = 0; i < 3; i++) {
152 b = move(b, 0, Player.Direction.NORTH);
153 b = move(b, 1, Player.Direction.SOUTH);
156 b = move(b, 0, Player.Direction.NORTH);
158 b = move(b, 1, Player.Direction.SOUTH);
159 fail ("Now the positions of two players are supposed to be the same, this results in exception");
160 } catch (IllegalPositionException ex) {
166 public void testCrossFences () throws Exception {
167 Board b1 = board.fence (board.getPlayers ().get(0), 'A', 1, Fence.Orientation.HORIZONTAL);
170 b1.fence (b1.getPlayers ().get(1), 'A', 1, Fence.Orientation.VERTICAL);
171 fail ("This must fail, as the fences overlap");
172 } catch (IllegalPositionException ex) {
177 public void testPawnsCanJumpOverEachOther () throws Exception {
180 for (int i = 0; i < 3; i++) {
181 b = move(b, 0, Player.Direction.NORTH);
182 b = move(b, 1, Player.Direction.SOUTH);
185 b = move(b, 0, Player.Direction.NORTH);
188 b = move(b, 1, Player.Direction.SOUTH, Player.Direction.SOUTH);
191 public void testCannotJumpOverFence () throws Exception {
192 Board b = fence (board, 0, 'D', 8, Fence.Orientation.HORIZONTAL);
193 assertEquals("One fence is present", 1, b.getFences().size());
194 final Fence f = b.getFences().iterator().next();
195 assertEquals("Row is 8", 8, f.getRow());
196 assertEquals("Column is D", 'D', f.getColumn());
198 move(b, 1, Player.Direction.SOUTH);
199 fail ("This shall not be allowed, as there is the fence");
200 } catch (IllegalPositionException ex) {
206 public void testSideJumpsNotAllowedWhenThereIsNoFence () throws Exception {
209 for (int i = 0; i < 3; i++) {
210 b = move(b, 0, Player.Direction.NORTH);
211 b = move(b, 1, Player.Direction.SOUTH);
214 b = move(b, 0, Player.Direction.NORTH);
217 b = move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
218 fail ("Cannot just jump aside");
219 } catch (IllegalPositionException ex) {
224 public void testSideJumpsAllowedWhenThereAFence () throws Exception {
227 for (int i = 0; i < 3; i++) {
228 b = move(b, 0, Player.Direction.NORTH);
229 b = move(b, 1, Player.Direction.SOUTH);
232 b = move(b, 0, Player.Direction.NORTH);
234 assertEquals (8, b.getPlayers ().get (0).getXInternal());
235 assertEquals (8, b.getPlayers ().get (0).getYInternal());
237 b = fence(b, 0, 'D', 4, Fence.Orientation.HORIZONTAL);
239 // we can over jump to west
240 move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
242 move(b, 1, Player.Direction.SOUTH, Player.Direction.EAST);
245 public void testPlaceAllFences() throws Exception {
248 public void testPlaceAllFences2() throws Exception {
252 private void doPlaceAllFences(int player) throws Exception {
256 for (int i = 1; i <= 5; i++) {
257 b = fence(b, player, 'A', i, Fence.Orientation.HORIZONTAL);
258 b = fence(b, player, 'D', i, Fence.Orientation.HORIZONTAL);
260 assertEquals("Two less" + i, cnt, b.getPlayers().get(player).getFences());
264 fence(b, player, 'F', 7, Fence.Orientation.VERTICAL);
265 fail("We shall run out of fences");
266 } catch (IllegalPositionException ex) {
271 public void testAlwaysHasToHaveAccessToEndLine () throws Exception {
274 b = b.fence (b.getPlayers ().get (0), 'E', 1, Fence.Orientation.HORIZONTAL);
275 b = b.fence (b.getPlayers ().get (0), 'F', 1, Fence.Orientation.VERTICAL);
278 b = b.fence (b.getPlayers ().get (0), 'D', 1, Fence.Orientation.VERTICAL);
279 fail ("This is not allowed as player 0 cannot now reach the final line");
280 } catch (IllegalPositionException ex) {
286 public void testEqualsOfPlayers () throws Exception {
287 Player p1 = new Player (1, 1, 10, Player.Direction.EAST);
288 Player p2 = new Player (1, 1, 10, Player.Direction.EAST);
289 Player p3 = new Player (2, 1, 10, Player.Direction.EAST);
290 Player p4 = new Player (1, 1, 10, Player.Direction.WEST);
291 Player p5 = new Player (1, 2, 10, Player.Direction.EAST);
292 Player p6 = new Player (1, 1, 5, Player.Direction.EAST);
294 assertEquals ("p1 == p2", p1, p2);
295 if (p2.equals (p3)) fail ();
296 if (p2.equals (p4)) fail ();
297 if (p2.equals (p5)) fail ();
298 if (p2.equals (p6)) fail ();
299 if (p3.equals (p6)) fail ();
300 if (p4.equals (p3)) fail ();
301 if (p6.equals (p3)) fail ();
302 if (p5.equals (p3)) fail ();
303 if (p5.equals (p3)) fail ();
304 if (p4.equals (p3)) fail ();
305 if (p3.equals (p4)) fail ();
306 if (p3.equals (p5)) fail ();
307 if (p4.equals (p5)) fail ();
308 if (p5.equals (p4)) fail ();
311 public void testEqualsOfFences () throws Exception {
312 Fence f1 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
313 Fence f2 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
314 Fence f3 = new Fence (3, 1, Fence.Orientation.HORIZONTAL);
315 Fence f4 = new Fence (1, 3, Fence.Orientation.HORIZONTAL);
316 Fence f5 = new Fence (1, 1, Fence.Orientation.VERTICAL);
318 assertEquals ("f1 == f2", f1, f2);
319 if (f1.equals (f3)) fail ();
320 if (f3.equals (f1)) fail ();
321 if (f5.equals (f1)) fail ();
322 if (f1.equals (f5)) fail ();
323 if (f4.equals (f1)) fail ();
324 if (f1.equals (f4)) fail ();
327 public void testEqualsOfBoards1 () throws Exception {
328 Board b1 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
329 Board b2 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
330 Board b3 = board.move (board.getPlayers ().get (0), Player.Direction.EAST);
332 if (b1.equals (b3)) fail ();
333 if (b3.equals (b1)) fail ();
335 assertEquals ("b1 == b2", b1, b2);
337 public void testEqualsOfBoards2 () throws Exception {
338 Board b1 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
339 Board b2 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
340 Board b3 = board.fence (board.getPlayers ().get (0), 'D', 3, Fence.Orientation.HORIZONTAL);
341 Board b4 = board.fence (board.getPlayers ().get (0), 'E', 4, Fence.Orientation.HORIZONTAL);
342 Board b5 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.VERTICAL);
344 if (b1.equals (b3)) fail ();
345 if (b3.equals (b1)) fail ();
346 if (b1.equals (b4)) fail ();
347 if (b1.equals (b5)) fail ();
348 if (b5.equals (b1)) fail ();
349 if (b4.equals (b1)) fail ();
351 assertEquals ("b1 == b2", b1, b2);
353 public void testEqualsOfBoardsWhenJumpOver () throws Exception {
356 for (int i = 0; i < 3; i++) {
357 b = move(b, 0, Player.Direction.NORTH);
358 b = move(b, 1, Player.Direction.SOUTH);
361 Board b1 = move(b, 0, Player.Direction.NORTH);
363 Board b2 = move(b, 1, Player.Direction.SOUTH);
364 b2 = b2.move (b2.getPlayers ().get (0), Player.Direction.NORTH, Player.Direction.NORTH);
366 if (b1.equals (b2)) fail ("Not the same, pawns are reverted");
367 if (b2.equals (b1)) fail ("Not the same, pawns are reverted");
370 public void testMoveAltersCurrentPlayer() throws Exception {
371 assertEquals("First player ready", board.getCurrentPlayer(), board.getPlayers().get(0));
372 Board b = apply(board, Move.EAST);
374 for (int i = 0; i < 7; i++) {
375 assertEquals("Snd player ready", b.getCurrentPlayer(), b.getPlayers().get(1));
376 b = apply(b, Move.SOUTH);
377 assertEquals("First player ready", b.getCurrentPlayer(), b.getPlayers().get(0));
378 b = apply(b, Move.NORTH);
381 Board fin = b.apply(Move.NORTH).apply(Move.NORTH);
382 assertNotNull("There is a winner", fin.getWinner());
383 assertEquals("And the winner is", fin.getPlayers().get(0), fin.getWinner());
385 assertFalse("No next move can be applied", fin.isApplicable(Move.fence('D', 3, Fence.Orientation.HORIZONTAL)));
388 fin.apply(Move.EAST);
389 fail("No moves allow when we are in final position");
390 } catch (IllegalPositionException ex) {
396 public void testDetectInvalidFence() {
398 Move m = Move.fence('D', 9, Orientation.HORIZONTAL);
399 fail("Move shall not be allowed: " + m);
400 } catch (IllegalPositionException ex) {
405 public void testEqualityOfMoves() throws Exception {
407 for (Direction m1 : Direction.values()) {
408 for (Direction m2 : Direction.values()) {
409 Move both1 = Move.jump(m1, m2);
410 Move both2 = Move.jump(m1, m2);
411 Move opposite = Move.jump(m2, m1);
413 assertTrue("Both boths are equal", both1.equals(both2));
414 assertFalse("Not north", Move.NORTH.equals(both1));
415 assertFalse("Not east", Move.EAST.equals(both1));
416 assertFalse("Not west", Move.WEST.equals(both1));
417 assertFalse("Not south", Move.SOUTH.equals(both1));
421 assertFalse("Not equal to opposite", both1.equals(opposite));
425 Move f1 = Move.fence('D', 6, Orientation.HORIZONTAL);
426 Move f2 = Move.fence('D', 6, Orientation.HORIZONTAL);
427 Move f3 = Move.fence('D', 6, Orientation.VERTICAL);
428 Move f4 = Move.fence('E', 6, Orientation.VERTICAL);
429 Move f5 = Move.fence('E', 5, Orientation.VERTICAL);
431 assertEquals(f1, f2);
432 assertFalse(f1.equals(f3));
433 assertFalse(f4.equals(f5));
434 assertFalse(f3.equals(f5));