Initial version of statistics and ELO rating. Donated by Martin Rexa
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());
147 public void testTwoFencesInTheSamePosition() throws IllegalPositionException {
148 assertEquals ("No on board", 0, board.getFences ().size ());
149 Board one = board.apply(Move.fence('A', 1, Orientation.HORIZONTAL));
151 Board snd = one.apply(Move.fence('A', 1, Orientation.HORIZONTAL));
152 fail("Cannot place fence twice to the same place");
153 } catch (IllegalPositionException ex) {
158 public void testSelfDestructionForbidden() throws Exception {
161 b = fence(b, 0, 'D', 1, Orientation.VERTICAL);
162 b = move(b, 1, Player.Direction.SOUTH);
163 b = fence(b, 0, 'E', 1, Orientation.VERTICAL);
164 b = move(b, 1, Player.Direction.SOUTH);
166 b = fence(b, 0, 'E', 2, Orientation.HORIZONTAL);
167 fail ("Forbidden. Player 0 has no way to reach the end");
168 } catch (IllegalPositionException ex) {
173 public void testRealSelfDestructionForbidden() throws Exception {
177 " A B C D E F G H\n" +
178 " | | | | | | | |\n" +
179 " +*----------------------------------+\n" +
181 " 8--|-------+-------+-------+-------+ |--8\n" +
183 " 7--| + + + |-------+ | + |--7\n" +
185 " 6--| + + + + + | + + |--6\n" +
187 " 5--| + + + + |-------+ + |--5\n" +
189 " 4--| + + + + +-------+ + |--4\n" +
191 " 3--| + + + + + + | | |--3\n" +
193 " 2--| + + + + + + + + |--2\n" +
195 " 1--| + + + + + | +-------|--1\n" +
197 " +||||-------------------------------+\n" +
198 " | | | | | | | |\n" +
199 " A B C D E F G H\n" +
203 b = Board.picture2board(b).toString();
204 Board begin = Board.valueOf(b);
207 Board bad = fence(begin, 0, 'G', 2, Orientation.HORIZONTAL);
208 fail("Not allowed move:\n" + bad.toString());
209 } catch (IllegalPositionException ex) {
213 public void testFourTimesInAgainstEachOtherResultsInInvalidPosition () throws Exception {
216 for (int i = 0; i < 3; i++) {
217 b = move(b, 0, Player.Direction.NORTH);
218 b = move(b, 1, Player.Direction.SOUTH);
221 b = move(b, 0, Player.Direction.NORTH);
223 b = move(b, 1, Player.Direction.SOUTH);
224 fail ("Now the positions of two players are supposed to be the same, this results in exception");
225 } catch (IllegalPositionException ex) {
231 public void testCrossFences () throws Exception {
232 Board b1 = board.fence (board.getPlayers ().get(0), 'A', 1, Fence.Orientation.HORIZONTAL);
235 b1.fence (b1.getPlayers ().get(1), 'A', 1, Fence.Orientation.VERTICAL);
236 fail ("This must fail, as the fences overlap");
237 } catch (IllegalPositionException ex) {
242 public void testPawnsCanJumpOverEachOther () throws Exception {
245 for (int i = 0; i < 3; i++) {
246 b = move(b, 0, Player.Direction.NORTH);
247 b = move(b, 1, Player.Direction.SOUTH);
250 b = move(b, 0, Player.Direction.NORTH);
253 b = move(b, 1, Player.Direction.SOUTH, Player.Direction.SOUTH);
256 public void testCannotJumpOverFence () throws Exception {
257 Board b = fence (board, 0, 'D', 8, Fence.Orientation.HORIZONTAL);
258 assertEquals("One fence is present", 1, b.getFences().size());
259 final Fence f = b.getFences().iterator().next();
260 assertEquals("Row is 8", 8, f.getRow());
261 assertEquals("Column is D", 'D', f.getColumn());
263 move(b, 1, Player.Direction.SOUTH);
264 fail ("This shall not be allowed, as there is the fence");
265 } catch (IllegalPositionException ex) {
271 public void testSideJumpsNotAllowedWhenThereIsNoFence () throws Exception {
274 for (int i = 0; i < 3; i++) {
275 b = move(b, 0, Player.Direction.NORTH);
276 b = move(b, 1, Player.Direction.SOUTH);
279 b = move(b, 0, Player.Direction.NORTH);
282 b = move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
283 fail ("Cannot just jump aside");
284 } catch (IllegalPositionException ex) {
289 public void testSideJumpsAllowedWhenThereAFence () throws Exception {
292 for (int i = 0; i < 3; i++) {
293 b = move(b, 0, Player.Direction.NORTH);
294 b = move(b, 1, Player.Direction.SOUTH);
297 b = move(b, 0, Player.Direction.NORTH);
299 assertEquals (8, b.getPlayers ().get (0).getXInternal());
300 assertEquals (8, b.getPlayers ().get (0).getYInternal());
302 b = fence(b, 0, 'D', 4, Fence.Orientation.HORIZONTAL);
304 // we can over jump to west
305 move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
307 move(b, 1, Player.Direction.SOUTH, Player.Direction.EAST);
310 public void testPlaceAllFences() throws Exception {
313 public void testPlaceAllFences2() throws Exception {
317 private void doPlaceAllFences(int player) throws Exception {
321 for (int i = 1; i <= 5; i++) {
322 b = fence(b, player, 'A', i, Fence.Orientation.HORIZONTAL);
323 b = fence(b, player, 'D', i, Fence.Orientation.HORIZONTAL);
325 assertEquals("Two less" + i, cnt, b.getPlayers().get(player).getFences());
329 fence(b, player, 'F', 7, Fence.Orientation.VERTICAL);
330 fail("We shall run out of fences");
331 } catch (IllegalPositionException ex) {
336 public void testAlwaysHasToHaveAccessToEndLine () throws Exception {
339 b = b.fence (b.getPlayers ().get (0), 'E', 1, Fence.Orientation.HORIZONTAL);
340 b = b.fence (b.getPlayers ().get (0), 'F', 1, Fence.Orientation.VERTICAL);
343 b = b.fence (b.getPlayers ().get (0), 'D', 1, Fence.Orientation.VERTICAL);
344 fail ("This is not allowed as player 0 cannot now reach the final line");
345 } catch (IllegalPositionException ex) {
351 public void testEqualsOfPlayers () throws Exception {
352 Player p1 = new Player (1, 1, 10, Player.Direction.EAST);
353 Player p2 = new Player (1, 1, 10, Player.Direction.EAST);
354 Player p3 = new Player (2, 1, 10, Player.Direction.EAST);
355 Player p4 = new Player (1, 1, 10, Player.Direction.WEST);
356 Player p5 = new Player (1, 2, 10, Player.Direction.EAST);
357 Player p6 = new Player (1, 1, 5, Player.Direction.EAST);
359 assertEquals ("p1 == p2", p1, p2);
360 if (p2.equals (p3)) fail ();
361 if (p2.equals (p4)) fail ();
362 if (p2.equals (p5)) fail ();
363 if (p2.equals (p6)) fail ();
364 if (p3.equals (p6)) fail ();
365 if (p4.equals (p3)) fail ();
366 if (p6.equals (p3)) fail ();
367 if (p5.equals (p3)) fail ();
368 if (p5.equals (p3)) fail ();
369 if (p4.equals (p3)) fail ();
370 if (p3.equals (p4)) fail ();
371 if (p3.equals (p5)) fail ();
372 if (p4.equals (p5)) fail ();
373 if (p5.equals (p4)) fail ();
376 public void testEqualsOfFences () throws Exception {
377 Fence f1 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
378 Fence f2 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
379 Fence f3 = new Fence (3, 1, Fence.Orientation.HORIZONTAL);
380 Fence f4 = new Fence (1, 3, Fence.Orientation.HORIZONTAL);
381 Fence f5 = new Fence (1, 1, Fence.Orientation.VERTICAL);
383 assertEquals ("f1 == f2", f1, f2);
384 if (f1.equals (f3)) fail ();
385 if (f3.equals (f1)) fail ();
386 if (f5.equals (f1)) fail ();
387 if (f1.equals (f5)) fail ();
388 if (f4.equals (f1)) fail ();
389 if (f1.equals (f4)) fail ();
392 public void testEqualsOfBoards1 () throws Exception {
393 Board b1 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
394 Board b2 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
395 Board b3 = board.move (board.getPlayers ().get (0), Player.Direction.EAST);
397 if (b1.equals (b3)) fail ();
398 if (b3.equals (b1)) fail ();
400 assertEquals ("b1 == b2", b1, b2);
402 public void testEqualsOfBoards2 () throws Exception {
403 Board b1 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
404 Board b2 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
405 Board b3 = board.fence (board.getPlayers ().get (0), 'D', 3, Fence.Orientation.HORIZONTAL);
406 Board b4 = board.fence (board.getPlayers ().get (0), 'E', 4, Fence.Orientation.HORIZONTAL);
407 Board b5 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.VERTICAL);
409 if (b1.equals (b3)) fail ();
410 if (b3.equals (b1)) fail ();
411 if (b1.equals (b4)) fail ();
412 if (b1.equals (b5)) fail ();
413 if (b5.equals (b1)) fail ();
414 if (b4.equals (b1)) fail ();
416 assertEquals ("b1 == b2", b1, b2);
418 public void testEqualsOfBoardsWhenJumpOver () throws Exception {
421 for (int i = 0; i < 3; i++) {
422 b = move(b, 0, Player.Direction.NORTH);
423 b = move(b, 1, Player.Direction.SOUTH);
426 Board b1 = move(b, 0, Player.Direction.NORTH);
428 Board b2 = move(b, 1, Player.Direction.SOUTH);
429 b2 = b2.move (b2.getPlayers ().get (0), Player.Direction.NORTH, Player.Direction.NORTH);
431 if (b1.equals (b2)) fail ("Not the same, pawns are reverted");
432 if (b2.equals (b1)) fail ("Not the same, pawns are reverted");
435 public void testMoveAltersCurrentPlayer() throws Exception {
436 assertEquals("First player ready", board.getCurrentPlayer(), board.getPlayers().get(0));
437 Board b = apply(board, Move.EAST);
439 for (int i = 0; i < 7; i++) {
440 assertEquals("Snd player ready", b.getCurrentPlayer(), b.getPlayers().get(1));
441 b = apply(b, Move.SOUTH);
442 assertEquals("First player ready", b.getCurrentPlayer(), b.getPlayers().get(0));
443 b = apply(b, Move.NORTH);
446 Board fin = b.apply(Move.NORTH).apply(Move.NORTH);
447 assertNotNull("There is a winner", fin.getWinner());
448 assertEquals("And the winner is", fin.getPlayers().get(0), fin.getWinner());
450 assertFalse("No next move can be applied", fin.isApplicable(Move.fence('D', 3, Fence.Orientation.HORIZONTAL)));
453 fin.apply(Move.EAST);
454 fail("No moves allow when we are in final position");
455 } catch (IllegalPositionException ex) {
461 public void testDetectInvalidFence() {
463 Move m = Move.fence('D', 9, Orientation.HORIZONTAL);
464 fail("Move shall not be allowed: " + m);
465 } catch (IllegalPositionException ex) {
470 public void testEqualityOfMoves() throws Exception {
472 for (Direction m1 : Direction.values()) {
473 for (Direction m2 : Direction.values()) {
474 Move both1 = Move.jump(m1, m2);
475 Move both2 = Move.jump(m1, m2);
476 Move opposite = Move.jump(m2, m1);
478 assertTrue("Both boths are equal", both1.equals(both2));
479 assertFalse("Not north", Move.NORTH.equals(both1));
480 assertFalse("Not east", Move.EAST.equals(both1));
481 assertFalse("Not west", Move.WEST.equals(both1));
482 assertFalse("Not south", Move.SOUTH.equals(both1));
486 assertFalse("Not equal to opposite", both1.equals(opposite));
490 Move f1 = Move.fence('D', 6, Orientation.HORIZONTAL);
491 Move f2 = Move.fence('D', 6, Orientation.HORIZONTAL);
492 Move f3 = Move.fence('D', 6, Orientation.VERTICAL);
493 Move f4 = Move.fence('E', 6, Orientation.VERTICAL);
494 Move f5 = Move.fence('E', 5, Orientation.VERTICAL);
496 assertEquals(f1, f2);
497 assertFalse(f1.equals(f3));
498 assertFalse(f4.equals(f5));
499 assertFalse(f3.equals(f5));