quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Jan 2010 22:34:17 +0100
branchstatistics-and-elo
changeset 178 4b78d4f028b3
parent 107 152aedcc45d0
child 179 c5fbddc4c590
permissions -rw-r--r--
Initial version of statistics and ELO rating. Donated by Martin Rexa
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     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]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 package cz.xelfi.quoridor;
    27 
    28 import cz.xelfi.quoridor.Fence.Orientation;
    29 import cz.xelfi.quoridor.Player.Direction;
    30 import java.util.List;
    31 import junit.framework.TestCase;
    32 
    33 /**
    34  *
    35  * @author Jaroslav Tulach <jtulach@netbeans.org>
    36  */
    37 public abstract class BoardCase extends TestCase {
    38     protected Board board;
    39 
    40     protected BoardCase(String n) {
    41         super(n);
    42         board = Board.empty();
    43     }
    44 
    45     protected abstract Board move(Board b, int player, Player.Direction... where)
    46     throws IllegalPositionException;
    47 
    48     protected abstract Board fence(Board b, int player, char x, int y, Fence.Orientation orie)
    49     throws IllegalPositionException;
    50 
    51     protected abstract Board apply(Board b, Move move) throws IllegalPositionException;
    52 
    53 
    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());
    59         try {
    60             b.apply(Move.EAST);
    61             fail("No more moves allowed, the player resigned");
    62         } catch (IllegalPositionException ex) {
    63             // OK
    64         }
    65         assertNull("No player", b.getCurrentPlayer());
    66     }
    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());
    72         try {
    73             b.apply(Move.EAST);
    74             fail("No more moves allowed, the player resigned");
    75         } catch (IllegalPositionException ex) {
    76             // OK
    77         }
    78         assertNull("No player", b.getCurrentPlayer());
    79     }
    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));
    84         try {
    85             list.add (null);
    86             fail ("Modifications are not allowed");
    87         } catch (UnsupportedOperationException ex) {
    88             // ok
    89         }
    90         try {
    91             list.remove (0);
    92             fail ("Modifications are not allowed");
    93         } catch (UnsupportedOperationException ex) {
    94             // ok
    95         }
    96         
    97         
    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 ());
   104     }
   105     
   106     public void testFences () throws IllegalPositionException {
   107         assertEquals ("No on board", 0, board.getFences ().size ());
   108         try {
   109             board.getFences ().add (null);
   110             fail ("Should be unmodifiable");
   111         } catch (java.lang.UnsupportedOperationException ex) {
   112             // ok
   113         }
   114 
   115         {
   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());
   121         }
   122 
   123         {
   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());
   129         }
   130 
   131         {
   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());
   137         }
   138 
   139         {
   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());
   145         }
   146     }
   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));
   150         try {
   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) {
   154             // OK
   155         }
   156     }
   157     
   158     public void testSelfDestructionForbidden() throws Exception {
   159         Board b = board;
   160 
   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);
   165         try {
   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) {
   169             // ok
   170         }
   171 
   172     }
   173     public void testRealSelfDestructionForbidden() throws Exception {
   174         String b = "" +
   175             "                         [N]    \n" +
   176             "\n" +
   177 "            A   B   C   D   E   F   G   H\n" +
   178 "            |   |   |   |   |   |   |   |\n" +
   179 "        +*----------------------------------+\n" +
   180 "        |                                   |\n" +
   181 "     8--|-------+-------+-------+-------+   |--8\n" +
   182 "        |               |           |       |\n" +
   183 "     7--|   +   +   +   |-------+   |   +   |--7\n" +
   184 "        |               |       |   |       |\n" +
   185 "     6--|   +   +   +   +   +   |   +   +   |--6\n" +
   186 "        |                   |   |           |\n" +
   187 "     5--|   +   +   +   +   |-------+   +   |--5\n" +
   188 "[W]     |                   |               |     [E]\n" +
   189 "     4--|   +   +   +   +   +-------+   +   |--4\n" +
   190 "        |                           |   |   |\n" +
   191 "     3--|   +   +   +   +   +   +   |   |   |--3\n" +
   192 "        |                         P | Q |   |\n" +
   193 "     2--|   +   +   +   +   +   +   +   +   |--2\n" +
   194 "        |                       |           |\n" +
   195 "     1--|   +   +   +   +   +   |   +-------|--1\n" +
   196 "        |                       |           |\n" +
   197 "        +||||-------------------------------+\n" +
   198 "            |   |   |   |   |   |   |   |\n" +
   199 "            A   B   C   D   E   F   G   H\n" +
   200 "\n" +
   201 "                         [S]                 \n";
   202 
   203         b = Board.picture2board(b).toString();
   204         Board begin = Board.valueOf(b);
   205 
   206         try {
   207             Board bad = fence(begin, 0, 'G', 2, Orientation.HORIZONTAL);
   208             fail("Not allowed move:\n" + bad.toString());
   209         } catch (IllegalPositionException ex) {
   210             // OK
   211         }
   212     }
   213     public void testFourTimesInAgainstEachOtherResultsInInvalidPosition () throws Exception {
   214         Board b = board;
   215         
   216         for (int i = 0; i < 3; i++) {
   217             b = move(b, 0, Player.Direction.NORTH);
   218             b = move(b, 1, Player.Direction.SOUTH);
   219         }
   220         
   221         b = move(b, 0, Player.Direction.NORTH);
   222         try {
   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) {
   226             // ok
   227         }
   228         
   229     }
   230 
   231     public void testCrossFences () throws Exception {
   232         Board b1 = board.fence (board.getPlayers ().get(0), 'A', 1, Fence.Orientation.HORIZONTAL);
   233         
   234         try {
   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) {
   238             // ok
   239         }
   240     }
   241     
   242     public void testPawnsCanJumpOverEachOther () throws Exception {
   243         Board b = board;
   244         
   245         for (int i = 0; i < 3; i++) {
   246             b = move(b, 0, Player.Direction.NORTH);
   247             b = move(b, 1, Player.Direction.SOUTH);
   248         }
   249         
   250         b = move(b, 0, Player.Direction.NORTH);
   251         
   252         // jump over
   253         b = move(b, 1, Player.Direction.SOUTH, Player.Direction.SOUTH);
   254     }
   255 
   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());
   262         try {
   263             move(b, 1, Player.Direction.SOUTH);
   264             fail ("This shall not be allowed, as there is the fence");
   265         } catch (IllegalPositionException ex) {
   266             // ok
   267         }
   268     }
   269     
   270 
   271     public void testSideJumpsNotAllowedWhenThereIsNoFence () throws Exception {
   272         Board b = board;
   273         
   274         for (int i = 0; i < 3; i++) {
   275             b = move(b, 0, Player.Direction.NORTH);
   276             b = move(b, 1, Player.Direction.SOUTH);
   277         }
   278         
   279         b = move(b, 0, Player.Direction.NORTH);
   280         
   281         try {
   282             b = move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
   283             fail ("Cannot just jump aside");
   284         } catch (IllegalPositionException ex) {
   285             // ok
   286         }
   287     }
   288     
   289     public void testSideJumpsAllowedWhenThereAFence () throws Exception {
   290         Board b = board;
   291         
   292         for (int i = 0; i < 3; i++) {
   293             b = move(b, 0, Player.Direction.NORTH);
   294             b = move(b, 1, Player.Direction.SOUTH);
   295         }
   296         
   297         b = move(b, 0, Player.Direction.NORTH);
   298         
   299         assertEquals (8, b.getPlayers ().get (0).getXInternal());
   300         assertEquals (8, b.getPlayers ().get (0).getYInternal());
   301         
   302         b = fence(b, 0, 'D', 4, Fence.Orientation.HORIZONTAL);
   303         
   304         // we can over jump to west
   305         move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
   306         // as well as east
   307         move(b, 1, Player.Direction.SOUTH, Player.Direction.EAST);
   308     }
   309 
   310     public void testPlaceAllFences() throws Exception {
   311         doPlaceAllFences(0);
   312     }
   313     public void testPlaceAllFences2() throws Exception {
   314         doPlaceAllFences(1);
   315     }
   316 
   317     private void doPlaceAllFences(int player) throws Exception {
   318         Board b = board;
   319 
   320         int cnt = 10;
   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);
   324             cnt -= 2;
   325             assertEquals("Two less" + i, cnt, b.getPlayers().get(player).getFences());
   326         }
   327 
   328         try {
   329             fence(b, player, 'F', 7, Fence.Orientation.VERTICAL);
   330             fail("We shall run out of fences");
   331         } catch (IllegalPositionException ex) {
   332             // OK
   333         }
   334     }
   335     
   336     public void testAlwaysHasToHaveAccessToEndLine () throws Exception {
   337         Board b = board;
   338         
   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);
   341 
   342         try {
   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) {
   346             // ok
   347         }
   348         
   349     }
   350     
   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);
   358         
   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 ();
   374     }
   375     
   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);
   382         
   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 ();
   390     }
   391     
   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);
   396         
   397         if (b1.equals (b3)) fail ();
   398         if (b3.equals (b1)) fail ();
   399         
   400         assertEquals ("b1 == b2", b1, b2);
   401     }
   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);
   408         
   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 ();
   415         
   416         assertEquals ("b1 == b2", b1, b2);
   417     }
   418     public void testEqualsOfBoardsWhenJumpOver () throws Exception {
   419         Board b = board;
   420         
   421         for (int i = 0; i < 3; i++) {
   422             b = move(b, 0, Player.Direction.NORTH);
   423             b = move(b, 1, Player.Direction.SOUTH);
   424         }
   425         
   426         Board b1 = move(b, 0, Player.Direction.NORTH);
   427         
   428         Board b2 = move(b, 1, Player.Direction.SOUTH);
   429         b2 = b2.move (b2.getPlayers ().get (0), Player.Direction.NORTH, Player.Direction.NORTH);
   430         
   431         if (b1.equals (b2)) fail ("Not the same, pawns are reverted");
   432         if (b2.equals (b1)) fail ("Not the same, pawns are reverted");
   433     }
   434 
   435     public void testMoveAltersCurrentPlayer() throws Exception {
   436         assertEquals("First player ready", board.getCurrentPlayer(), board.getPlayers().get(0));
   437         Board b = apply(board, Move.EAST);
   438 
   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);
   444         }
   445 
   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());
   449 
   450         assertFalse("No next move can be applied", fin.isApplicable(Move.fence('D', 3, Fence.Orientation.HORIZONTAL)));
   451 
   452         try {
   453             fin.apply(Move.EAST);
   454             fail("No moves allow when we are in final position");
   455         } catch (IllegalPositionException ex) {
   456             // OK
   457         }
   458 
   459     }
   460 
   461     public void testDetectInvalidFence() {
   462         try {
   463             Move m = Move.fence('D', 9, Orientation.HORIZONTAL);
   464             fail("Move shall not be allowed: " + m);
   465         } catch (IllegalPositionException ex) {
   466             // OK
   467         }
   468     }
   469 
   470     public void testEqualityOfMoves() throws Exception {
   471 
   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);
   477 
   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));
   483                 if (m1 == m2) {
   484                     continue;
   485                 }
   486                 assertFalse("Not equal to opposite", both1.equals(opposite));
   487             }
   488         }
   489 
   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);
   495 
   496         assertEquals(f1, f2);
   497         assertFalse(f1.equals(f3));
   498         assertFalse(f4.equals(f5));
   499         assertFalse(f3.equals(f5));
   500     }
   501 }