Rotating the display of the board - north is on top. Enhancing the display with row ids and column numbers and compass directions.
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 testTwoPlayers () {
55 List<Player> list = board.getPlayers();
56 assertEquals ("Two", 2, list.size ());
57 assertFalse ("Both are non-null", list.contains (null));
60 fail ("Modifications are not allowed");
61 } catch (UnsupportedOperationException ex) {
66 fail ("Modifications are not allowed");
67 } catch (UnsupportedOperationException ex) {
72 assertEquals (8, list.get (0).getXInternal());
73 assertEquals (0, list.get (0).getYInternal());
74 assertEquals (10, list.get (0).getFences ());
75 assertEquals (8, list.get (1).getXInternal());
76 assertEquals (16, list.get (1).getYInternal());
77 assertEquals (10, list.get (1).getFences ());
80 public void testFences () throws IllegalPositionException {
81 assertEquals ("No on board", 0, board.getFences ().size ());
83 board.getFences ().add (null);
84 fail ("Should be unmodifiable");
85 } catch (java.lang.UnsupportedOperationException ex) {
90 Board b = board.apply(Move.fence('A', 1, Orientation.HORIZONTAL));
91 assertEquals("One fence placed: ", 1, b.getFences().size());
92 Fence f = b.getFences().iterator().next();
93 assertEquals("Row", 1, f.getRow());
94 assertEquals("Column", 'A', f.getColumn());
98 Board b = board.apply(Move.fence('A', 1, Orientation.VERTICAL));
99 assertEquals("One fence placed: ", 1, b.getFences().size());
100 Fence f = b.getFences().iterator().next();
101 assertEquals("Row", 1, f.getRow());
102 assertEquals("Column", 'A', f.getColumn());
106 Board b = board.apply(Move.fence('H', 8, Orientation.HORIZONTAL));
107 assertEquals("One fence placed: ", 1, b.getFences().size());
108 Fence f = b.getFences().iterator().next();
109 assertEquals("Row", 8, f.getRow());
110 assertEquals("Column", 'H', f.getColumn());
114 Board b = board.apply(Move.fence('H', 8, Orientation.VERTICAL));
115 assertEquals("One fence placed: ", 1, b.getFences().size());
116 Fence f = b.getFences().iterator().next();
117 assertEquals("Row", 8, f.getRow());
118 assertEquals("Column", 'H', f.getColumn());
122 public void testFourTimesInAgainstEachOtherResultsInInvalidPosition () throws Exception {
125 for (int i = 0; i < 3; i++) {
126 b = move(b, 0, Player.Direction.NORTH);
127 b = move(b, 1, Player.Direction.SOUTH);
130 b = move(b, 0, Player.Direction.NORTH);
132 b = move(b, 1, Player.Direction.SOUTH);
133 fail ("Now the positions of two players are supposed to be the same, this results in exception");
134 } catch (IllegalPositionException ex) {
140 public void testCrossFences () throws Exception {
141 Board b1 = board.fence (board.getPlayers ().get(0), 'A', 1, Fence.Orientation.HORIZONTAL);
144 b1.fence (b1.getPlayers ().get(1), 'A', 1, Fence.Orientation.VERTICAL);
145 fail ("This must fail, as the fences overlap");
146 } catch (IllegalPositionException ex) {
151 public void testPawnsCanJumpOverEachOther () throws Exception {
154 for (int i = 0; i < 3; i++) {
155 b = move(b, 0, Player.Direction.NORTH);
156 b = move(b, 1, Player.Direction.SOUTH);
159 b = move(b, 0, Player.Direction.NORTH);
162 b = move(b, 1, Player.Direction.SOUTH, Player.Direction.SOUTH);
165 public void testCannotJumpOverFence () throws Exception {
166 Board b = fence (board, 0, 'D', 8, Fence.Orientation.HORIZONTAL);
167 assertEquals("One fence is present", 1, b.getFences().size());
168 final Fence f = b.getFences().iterator().next();
169 assertEquals("Row is 8", 8, f.getRow());
170 assertEquals("Column is D", 'D', f.getColumn());
172 move(b, 1, Player.Direction.SOUTH);
173 fail ("This shall not be allowed, as there is the fence");
174 } catch (IllegalPositionException ex) {
180 public void testSideJumpsNotAllowedWhenThereIsNoFence () throws Exception {
183 for (int i = 0; i < 3; i++) {
184 b = move(b, 0, Player.Direction.NORTH);
185 b = move(b, 1, Player.Direction.SOUTH);
188 b = move(b, 0, Player.Direction.NORTH);
191 b = move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
192 fail ("Cannot just jump aside");
193 } catch (IllegalPositionException ex) {
198 public void testSideJumpsAllowedWhenThereAFence () throws Exception {
201 for (int i = 0; i < 3; i++) {
202 b = move(b, 0, Player.Direction.NORTH);
203 b = move(b, 1, Player.Direction.SOUTH);
206 b = move(b, 0, Player.Direction.NORTH);
208 assertEquals (8, b.getPlayers ().get (0).getXInternal());
209 assertEquals (8, b.getPlayers ().get (0).getYInternal());
211 b = fence(b, 0, 'D', 4, Fence.Orientation.HORIZONTAL);
213 // we can over jump to west
214 move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
216 move(b, 1, Player.Direction.SOUTH, Player.Direction.EAST);
219 public void testPlaceAllFences() throws Exception {
222 public void testPlaceAllFences2() throws Exception {
226 private void doPlaceAllFences(int player) throws Exception {
230 for (int i = 1; i <= 5; i++) {
231 b = fence(b, player, 'A', i, Fence.Orientation.HORIZONTAL);
232 b = fence(b, player, 'D', i, Fence.Orientation.HORIZONTAL);
234 assertEquals("Two less" + i, cnt, b.getPlayers().get(player).getFences());
238 fence(b, player, 'F', 7, Fence.Orientation.VERTICAL);
239 fail("We shall run out of fences");
240 } catch (IllegalPositionException ex) {
245 public void testAlwaysHasToHaveAccessToEndLine () throws Exception {
248 b = b.fence (b.getPlayers ().get (0), 'E', 1, Fence.Orientation.HORIZONTAL);
249 b = b.fence (b.getPlayers ().get (0), 'F', 1, Fence.Orientation.VERTICAL);
252 b = b.fence (b.getPlayers ().get (0), 'D', 1, Fence.Orientation.VERTICAL);
253 fail ("This is not allowed as player 0 cannot now reach the final line");
254 } catch (IllegalPositionException ex) {
260 public void testEqualsOfPlayers () throws Exception {
261 Player p1 = new Player (1, 1, 10, Player.Direction.EAST);
262 Player p2 = new Player (1, 1, 10, Player.Direction.EAST);
263 Player p3 = new Player (2, 1, 10, Player.Direction.EAST);
264 Player p4 = new Player (1, 1, 10, Player.Direction.WEST);
265 Player p5 = new Player (1, 2, 10, Player.Direction.EAST);
266 Player p6 = new Player (1, 1, 5, Player.Direction.EAST);
268 assertEquals ("p1 == p2", p1, p2);
269 if (p2.equals (p3)) fail ();
270 if (p2.equals (p4)) fail ();
271 if (p2.equals (p5)) fail ();
272 if (p2.equals (p6)) fail ();
273 if (p3.equals (p6)) fail ();
274 if (p4.equals (p3)) fail ();
275 if (p6.equals (p3)) fail ();
276 if (p5.equals (p3)) fail ();
277 if (p5.equals (p3)) fail ();
278 if (p4.equals (p3)) fail ();
279 if (p3.equals (p4)) fail ();
280 if (p3.equals (p5)) fail ();
281 if (p4.equals (p5)) fail ();
282 if (p5.equals (p4)) fail ();
285 public void testEqualsOfFences () throws Exception {
286 Fence f1 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
287 Fence f2 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
288 Fence f3 = new Fence (3, 1, Fence.Orientation.HORIZONTAL);
289 Fence f4 = new Fence (1, 3, Fence.Orientation.HORIZONTAL);
290 Fence f5 = new Fence (1, 1, Fence.Orientation.VERTICAL);
292 assertEquals ("f1 == f2", f1, f2);
293 if (f1.equals (f3)) fail ();
294 if (f3.equals (f1)) fail ();
295 if (f5.equals (f1)) fail ();
296 if (f1.equals (f5)) fail ();
297 if (f4.equals (f1)) fail ();
298 if (f1.equals (f4)) fail ();
301 public void testEqualsOfBoards1 () throws Exception {
302 Board b1 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
303 Board b2 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
304 Board b3 = board.move (board.getPlayers ().get (0), Player.Direction.EAST);
306 if (b1.equals (b3)) fail ();
307 if (b3.equals (b1)) fail ();
309 assertEquals ("b1 == b2", b1, b2);
311 public void testEqualsOfBoards2 () throws Exception {
312 Board b1 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
313 Board b2 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
314 Board b3 = board.fence (board.getPlayers ().get (0), 'D', 3, Fence.Orientation.HORIZONTAL);
315 Board b4 = board.fence (board.getPlayers ().get (0), 'E', 4, Fence.Orientation.HORIZONTAL);
316 Board b5 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.VERTICAL);
318 if (b1.equals (b3)) fail ();
319 if (b3.equals (b1)) fail ();
320 if (b1.equals (b4)) fail ();
321 if (b1.equals (b5)) fail ();
322 if (b5.equals (b1)) fail ();
323 if (b4.equals (b1)) fail ();
325 assertEquals ("b1 == b2", b1, b2);
327 public void testEqualsOfBoardsWhenJumpOver () throws Exception {
330 for (int i = 0; i < 3; i++) {
331 b = move(b, 0, Player.Direction.NORTH);
332 b = move(b, 1, Player.Direction.SOUTH);
335 Board b1 = move(b, 0, Player.Direction.NORTH);
337 Board b2 = move(b, 1, Player.Direction.SOUTH);
338 b2 = b2.move (b2.getPlayers ().get (0), Player.Direction.NORTH, Player.Direction.NORTH);
340 if (b1.equals (b2)) fail ("Not the same, pawns are reverted");
341 if (b2.equals (b1)) fail ("Not the same, pawns are reverted");
344 public void testMoveAltersCurrentPlayer() throws Exception {
345 assertEquals("First player ready", board.getCurrentPlayer(), board.getPlayers().get(0));
346 Board b = apply(board, Move.EAST);
348 for (int i = 0; i < 7; i++) {
349 assertEquals("Snd player ready", b.getCurrentPlayer(), b.getPlayers().get(1));
350 b = apply(b, Move.SOUTH);
351 assertEquals("First player ready", b.getCurrentPlayer(), b.getPlayers().get(0));
352 b = apply(b, Move.NORTH);
355 Board fin = b.apply(Move.NORTH).apply(Move.NORTH);
356 assertNotNull("There is a winner", fin.getWinner());
357 assertEquals("And the winner is", fin.getPlayers().get(0), fin.getWinner());
359 assertFalse("No next move can be applied", fin.isApplicable(Move.fence('D', 3, Fence.Orientation.HORIZONTAL)));
362 fin.apply(Move.EAST);
363 fail("No moves allow when we are in final position");
364 } catch (IllegalPositionException ex) {
370 public void testDetectInvalidFence() {
372 Move m = Move.fence('D', 9, Orientation.HORIZONTAL);
373 fail("Move shall not be allowed: " + m);
374 } catch (IllegalPositionException ex) {
379 public void testEqualityOfMoves() throws Exception {
381 for (Direction m1 : Direction.values()) {
382 for (Direction m2 : Direction.values()) {
383 Move both1 = Move.jump(m1, m2);
384 Move both2 = Move.jump(m1, m2);
385 Move opposite = Move.jump(m2, m1);
387 assertTrue("Both boths are equal", both1.equals(both2));
388 assertFalse("Not north", Move.NORTH.equals(both1));
389 assertFalse("Not east", Move.EAST.equals(both1));
390 assertFalse("Not west", Move.WEST.equals(both1));
391 assertFalse("Not south", Move.SOUTH.equals(both1));
395 assertFalse("Not equal to opposite", both1.equals(opposite));
399 Move f1 = Move.fence('D', 6, Orientation.HORIZONTAL);
400 Move f2 = Move.fence('D', 6, Orientation.HORIZONTAL);
401 Move f3 = Move.fence('D', 6, Orientation.VERTICAL);
402 Move f4 = Move.fence('E', 6, Orientation.VERTICAL);
403 Move f5 = Move.fence('E', 5, Orientation.VERTICAL);
405 assertEquals(f1, f2);
406 assertFalse(f1.equals(f3));
407 assertFalse(f4.equals(f5));
408 assertFalse(f3.equals(f5));