quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Jan 2010 22:51:17 +0100
branchstatistics-and-elo
changeset 179 c5fbddc4c590
parent 178 4b78d4f028b3
child 219 d836818f5554
permissions -rw-r--r--
Renaming Board.board2HashCode to getCode()
     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.io.IOException;
    31 import java.io.StringReader;
    32 import java.util.List;
    33 import junit.framework.TestCase;
    34 
    35 /**
    36  *
    37  * @author Jaroslav Tulach <jtulach@netbeans.org>
    38  */
    39 public abstract class BoardCase extends TestCase {
    40     protected Board board;
    41 
    42     protected BoardCase(String n) {
    43         super(n);
    44         board = Board.empty();
    45     }
    46 
    47     protected abstract Board move(Board b, int player, Player.Direction... where)
    48     throws IllegalPositionException;
    49 
    50     protected abstract Board fence(Board b, int player, char x, int y, Fence.Orientation orie)
    51     throws IllegalPositionException;
    52 
    53     protected abstract Board apply(Board b, Move move) throws IllegalPositionException;
    54 
    55 
    56     public void testResignWhite() throws IllegalPositionException {
    57         List<Player> list = board.getPlayers();
    58         assertEquals ("Two", 2, list.size ());
    59         Board b = board.apply(Move.RESIGN);
    60         assertEquals(b.getPlayers().get(1), b.getWinner());
    61         try {
    62             b.apply(Move.EAST);
    63             fail("No more moves allowed, the player resigned");
    64         } catch (IllegalPositionException ex) {
    65             // OK
    66         }
    67         assertNull("No player", b.getCurrentPlayer());
    68     }
    69     public void testResignBlack() throws IllegalPositionException {
    70         List<Player> list = board.getPlayers();
    71         assertEquals ("Two", 2, list.size ());
    72         Board b = board.apply(Move.NORTH).apply(Move.RESIGN);
    73         assertEquals(b.getPlayers().get(0), b.getWinner());
    74         try {
    75             b.apply(Move.EAST);
    76             fail("No more moves allowed, the player resigned");
    77         } catch (IllegalPositionException ex) {
    78             // OK
    79         }
    80         assertNull("No player", b.getCurrentPlayer());
    81     }
    82     public void testTwoPlayers () {
    83         List<Player> list = board.getPlayers();
    84         assertEquals ("Two", 2, list.size ());
    85         assertFalse ("Both are non-null", list.contains (null));
    86         try {
    87             list.add (null);
    88             fail ("Modifications are not allowed");
    89         } catch (UnsupportedOperationException ex) {
    90             // ok
    91         }
    92         try {
    93             list.remove (0);
    94             fail ("Modifications are not allowed");
    95         } catch (UnsupportedOperationException ex) {
    96             // ok
    97         }
    98         
    99         
   100         assertEquals (8, list.get (0).getXInternal());
   101         assertEquals (0, list.get (0).getYInternal());
   102         assertEquals (10, list.get (0).getFences ());
   103         assertEquals (8, list.get (1).getXInternal());
   104         assertEquals (16, list.get (1).getYInternal());
   105         assertEquals (10, list.get (1).getFences ());
   106     }
   107     
   108     public void testFences () throws IllegalPositionException {
   109         assertEquals ("No on board", 0, board.getFences ().size ());
   110         try {
   111             board.getFences ().add (null);
   112             fail ("Should be unmodifiable");
   113         } catch (java.lang.UnsupportedOperationException ex) {
   114             // ok
   115         }
   116 
   117         {
   118             Board b = board.apply(Move.fence('A', 1, Orientation.HORIZONTAL));
   119             assertEquals("One fence placed: ", 1, b.getFences().size());
   120             Fence f = b.getFences().iterator().next();
   121             assertEquals("Row", 1, f.getRow());
   122             assertEquals("Column", 'A', f.getColumn());
   123         }
   124 
   125         {
   126             Board b = board.apply(Move.fence('A', 1, Orientation.VERTICAL));
   127             assertEquals("One fence placed: ", 1, b.getFences().size());
   128             Fence f = b.getFences().iterator().next();
   129             assertEquals("Row", 1, f.getRow());
   130             assertEquals("Column", 'A', f.getColumn());
   131         }
   132 
   133         {
   134             Board b = board.apply(Move.fence('H', 8, Orientation.HORIZONTAL));
   135             assertEquals("One fence placed: ", 1, b.getFences().size());
   136             Fence f = b.getFences().iterator().next();
   137             assertEquals("Row", 8, f.getRow());
   138             assertEquals("Column", 'H', f.getColumn());
   139         }
   140 
   141         {
   142             Board b = board.apply(Move.fence('H', 8, Orientation.VERTICAL));
   143             assertEquals("One fence placed: ", 1, b.getFences().size());
   144             Fence f = b.getFences().iterator().next();
   145             assertEquals("Row", 8, f.getRow());
   146             assertEquals("Column", 'H', f.getColumn());
   147         }
   148     }
   149     public void testTwoFencesInTheSamePosition() throws IllegalPositionException {
   150         assertEquals ("No on board", 0, board.getFences ().size ());
   151         Board one = board.apply(Move.fence('A', 1, Orientation.HORIZONTAL));
   152         try {
   153             Board snd = one.apply(Move.fence('A', 1, Orientation.HORIZONTAL));
   154             fail("Cannot place fence twice to the same place");
   155         } catch (IllegalPositionException ex) {
   156             // OK
   157         }
   158     }
   159     
   160     public void testSelfDestructionForbidden() throws Exception {
   161         Board b = board;
   162 
   163         b = fence(b, 0, 'D', 1, Orientation.VERTICAL);
   164         b = move(b, 1, Player.Direction.SOUTH);
   165         b = fence(b, 0, 'E', 1, Orientation.VERTICAL);
   166         b = move(b, 1, Player.Direction.SOUTH);
   167         try {
   168             b = fence(b, 0, 'E', 2, Orientation.HORIZONTAL);
   169             fail ("Forbidden. Player 0 has no way to reach the end");
   170         } catch (IllegalPositionException ex) {
   171             // ok
   172         }
   173 
   174     }
   175     public void testRealSelfDestructionForbidden() throws Exception {
   176         String b = "" +
   177             "                         [N]    \n" +
   178             "\n" +
   179 "            A   B   C   D   E   F   G   H\n" +
   180 "            |   |   |   |   |   |   |   |\n" +
   181 "        +*----------------------------------+\n" +
   182 "        |                                   |\n" +
   183 "     8--|-------+-------+-------+-------+   |--8\n" +
   184 "        |               |           |       |\n" +
   185 "     7--|   +   +   +   |-------+   |   +   |--7\n" +
   186 "        |               |       |   |       |\n" +
   187 "     6--|   +   +   +   +   +   |   +   +   |--6\n" +
   188 "        |                   |   |           |\n" +
   189 "     5--|   +   +   +   +   |-------+   +   |--5\n" +
   190 "[W]     |                   |               |     [E]\n" +
   191 "     4--|   +   +   +   +   +-------+   +   |--4\n" +
   192 "        |                           |   |   |\n" +
   193 "     3--|   +   +   +   +   +   +   |   |   |--3\n" +
   194 "        |                         P | Q |   |\n" +
   195 "     2--|   +   +   +   +   +   +   +   +   |--2\n" +
   196 "        |                       |           |\n" +
   197 "     1--|   +   +   +   +   +   |   +-------|--1\n" +
   198 "        |                       |           |\n" +
   199 "        +||||-------------------------------+\n" +
   200 "            |   |   |   |   |   |   |   |\n" +
   201 "            A   B   C   D   E   F   G   H\n" +
   202 "\n" +
   203 "                         [S]                 \n";
   204 
   205         b = picture2board(b).toString();
   206         Board begin = Board.valueOf(b);
   207 
   208         try {
   209             Board bad = fence(begin, 0, 'G', 2, Orientation.HORIZONTAL);
   210             fail("Not allowed move:\n" + bad.toString());
   211         } catch (IllegalPositionException ex) {
   212             // OK
   213         }
   214     }
   215     public void testFourTimesInAgainstEachOtherResultsInInvalidPosition () throws Exception {
   216         Board b = board;
   217         
   218         for (int i = 0; i < 3; i++) {
   219             b = move(b, 0, Player.Direction.NORTH);
   220             b = move(b, 1, Player.Direction.SOUTH);
   221         }
   222         
   223         b = move(b, 0, Player.Direction.NORTH);
   224         try {
   225             b = move(b, 1, Player.Direction.SOUTH);
   226             fail ("Now the positions of two players are supposed to be the same, this results in exception");
   227         } catch (IllegalPositionException ex) {
   228             // ok
   229         }
   230         
   231     }
   232 
   233     public void testCrossFences () throws Exception {
   234         Board b1 = board.fence (board.getPlayers ().get(0), 'A', 1, Fence.Orientation.HORIZONTAL);
   235         
   236         try {
   237             b1.fence (b1.getPlayers ().get(1), 'A', 1, Fence.Orientation.VERTICAL);
   238             fail ("This must fail, as the fences overlap");
   239         } catch (IllegalPositionException ex) {
   240             // ok
   241         }
   242     }
   243     
   244     public void testPawnsCanJumpOverEachOther () throws Exception {
   245         Board b = board;
   246         
   247         for (int i = 0; i < 3; i++) {
   248             b = move(b, 0, Player.Direction.NORTH);
   249             b = move(b, 1, Player.Direction.SOUTH);
   250         }
   251         
   252         b = move(b, 0, Player.Direction.NORTH);
   253         
   254         // jump over
   255         b = move(b, 1, Player.Direction.SOUTH, Player.Direction.SOUTH);
   256     }
   257 
   258     public void testCannotJumpOverFence () throws Exception {
   259         Board b = fence (board, 0, 'D', 8, Fence.Orientation.HORIZONTAL);
   260         assertEquals("One fence is present", 1, b.getFences().size());
   261         final Fence f = b.getFences().iterator().next();
   262         assertEquals("Row is 8", 8, f.getRow());
   263         assertEquals("Column is D", 'D', f.getColumn());
   264         try {
   265             move(b, 1, Player.Direction.SOUTH);
   266             fail ("This shall not be allowed, as there is the fence");
   267         } catch (IllegalPositionException ex) {
   268             // ok
   269         }
   270     }
   271     
   272 
   273     public void testSideJumpsNotAllowedWhenThereIsNoFence () throws Exception {
   274         Board b = board;
   275         
   276         for (int i = 0; i < 3; i++) {
   277             b = move(b, 0, Player.Direction.NORTH);
   278             b = move(b, 1, Player.Direction.SOUTH);
   279         }
   280         
   281         b = move(b, 0, Player.Direction.NORTH);
   282         
   283         try {
   284             b = move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
   285             fail ("Cannot just jump aside");
   286         } catch (IllegalPositionException ex) {
   287             // ok
   288         }
   289     }
   290     
   291     public void testSideJumpsAllowedWhenThereAFence () throws Exception {
   292         Board b = board;
   293         
   294         for (int i = 0; i < 3; i++) {
   295             b = move(b, 0, Player.Direction.NORTH);
   296             b = move(b, 1, Player.Direction.SOUTH);
   297         }
   298         
   299         b = move(b, 0, Player.Direction.NORTH);
   300         
   301         assertEquals (8, b.getPlayers ().get (0).getXInternal());
   302         assertEquals (8, b.getPlayers ().get (0).getYInternal());
   303         
   304         b = fence(b, 0, 'D', 4, Fence.Orientation.HORIZONTAL);
   305         
   306         // we can over jump to west
   307         move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
   308         // as well as east
   309         move(b, 1, Player.Direction.SOUTH, Player.Direction.EAST);
   310     }
   311 
   312     public void testPlaceAllFences() throws Exception {
   313         doPlaceAllFences(0);
   314     }
   315     public void testPlaceAllFences2() throws Exception {
   316         doPlaceAllFences(1);
   317     }
   318 
   319     private void doPlaceAllFences(int player) throws Exception {
   320         Board b = board;
   321 
   322         int cnt = 10;
   323         for (int i = 1; i <= 5; i++) {
   324             b = fence(b, player, 'A', i, Fence.Orientation.HORIZONTAL);
   325             b = fence(b, player, 'D', i, Fence.Orientation.HORIZONTAL);
   326             cnt -= 2;
   327             assertEquals("Two less" + i, cnt, b.getPlayers().get(player).getFences());
   328         }
   329 
   330         try {
   331             fence(b, player, 'F', 7, Fence.Orientation.VERTICAL);
   332             fail("We shall run out of fences");
   333         } catch (IllegalPositionException ex) {
   334             // OK
   335         }
   336     }
   337     
   338     public void testAlwaysHasToHaveAccessToEndLine () throws Exception {
   339         Board b = board;
   340         
   341         b = b.fence (b.getPlayers ().get (0), 'E', 1, Fence.Orientation.HORIZONTAL);
   342         b = b.fence (b.getPlayers ().get (0), 'F', 1, Fence.Orientation.VERTICAL);
   343 
   344         try {
   345             b = b.fence (b.getPlayers ().get (0), 'D', 1, Fence.Orientation.VERTICAL);
   346             fail ("This is not allowed as player 0 cannot now reach the final line");
   347         } catch (IllegalPositionException ex) {
   348             // ok
   349         }
   350         
   351     }
   352     
   353     public void testEqualsOfPlayers () throws Exception {
   354         Player p1 = new Player (1, 1, 10, Player.Direction.EAST);
   355         Player p2 = new Player (1, 1, 10, Player.Direction.EAST);
   356         Player p3 = new Player (2, 1, 10, Player.Direction.EAST);
   357         Player p4 = new Player (1, 1, 10, Player.Direction.WEST);
   358         Player p5 = new Player (1, 2, 10, Player.Direction.EAST);
   359         Player p6 = new Player (1, 1, 5, Player.Direction.EAST);
   360         
   361         assertEquals ("p1 == p2", p1, p2);
   362         if (p2.equals (p3)) fail ();
   363         if (p2.equals (p4)) fail ();
   364         if (p2.equals (p5)) fail ();
   365         if (p2.equals (p6)) fail ();
   366         if (p3.equals (p6)) fail ();
   367         if (p4.equals (p3)) fail ();
   368         if (p6.equals (p3)) fail ();
   369         if (p5.equals (p3)) fail ();
   370         if (p5.equals (p3)) fail ();
   371         if (p4.equals (p3)) fail ();
   372         if (p3.equals (p4)) fail ();
   373         if (p3.equals (p5)) fail ();
   374         if (p4.equals (p5)) fail ();
   375         if (p5.equals (p4)) fail ();
   376     }
   377     
   378     public void testEqualsOfFences () throws Exception {
   379         Fence f1 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
   380         Fence f2 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
   381         Fence f3 = new Fence (3, 1, Fence.Orientation.HORIZONTAL);
   382         Fence f4 = new Fence (1, 3, Fence.Orientation.HORIZONTAL);
   383         Fence f5 = new Fence (1, 1, Fence.Orientation.VERTICAL);
   384         
   385         assertEquals ("f1 == f2", f1, f2);
   386         if (f1.equals (f3)) fail ();
   387         if (f3.equals (f1)) fail ();
   388         if (f5.equals (f1)) fail ();
   389         if (f1.equals (f5)) fail ();
   390         if (f4.equals (f1)) fail ();
   391         if (f1.equals (f4)) fail ();
   392     }
   393     
   394     public void testEqualsOfBoards1 () throws Exception {
   395         Board b1 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
   396         Board b2 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
   397         Board b3 = board.move (board.getPlayers ().get (0), Player.Direction.EAST);
   398         
   399         if (b1.equals (b3)) fail ();
   400         if (b3.equals (b1)) fail ();
   401         
   402         assertEquals ("b1 == b2", b1, b2);
   403     }
   404     public void testEqualsOfBoards2 () throws Exception {
   405         Board b1 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
   406         Board b2 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
   407         Board b3 = board.fence (board.getPlayers ().get (0), 'D', 3, Fence.Orientation.HORIZONTAL);
   408         Board b4 = board.fence (board.getPlayers ().get (0), 'E', 4, Fence.Orientation.HORIZONTAL);
   409         Board b5 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.VERTICAL);
   410         
   411         if (b1.equals (b3)) fail ();
   412         if (b3.equals (b1)) fail ();
   413         if (b1.equals (b4)) fail ();
   414         if (b1.equals (b5)) fail ();
   415         if (b5.equals (b1)) fail ();
   416         if (b4.equals (b1)) fail ();
   417         
   418         assertEquals ("b1 == b2", b1, b2);
   419     }
   420     public void testEqualsOfBoardsWhenJumpOver () throws Exception {
   421         Board b = board;
   422         
   423         for (int i = 0; i < 3; i++) {
   424             b = move(b, 0, Player.Direction.NORTH);
   425             b = move(b, 1, Player.Direction.SOUTH);
   426         }
   427         
   428         Board b1 = move(b, 0, Player.Direction.NORTH);
   429         
   430         Board b2 = move(b, 1, Player.Direction.SOUTH);
   431         b2 = b2.move (b2.getPlayers ().get (0), Player.Direction.NORTH, Player.Direction.NORTH);
   432         
   433         if (b1.equals (b2)) fail ("Not the same, pawns are reverted");
   434         if (b2.equals (b1)) fail ("Not the same, pawns are reverted");
   435     }
   436 
   437     public void testMoveAltersCurrentPlayer() throws Exception {
   438         assertEquals("First player ready", board.getCurrentPlayer(), board.getPlayers().get(0));
   439         Board b = apply(board, Move.EAST);
   440 
   441         for (int i = 0; i < 7; i++) {
   442             assertEquals("Snd player ready", b.getCurrentPlayer(), b.getPlayers().get(1));
   443             b = apply(b, Move.SOUTH);
   444             assertEquals("First player ready", b.getCurrentPlayer(), b.getPlayers().get(0));
   445             b = apply(b, Move.NORTH);
   446         }
   447 
   448         Board fin = b.apply(Move.NORTH).apply(Move.NORTH);
   449         assertNotNull("There is a winner", fin.getWinner());
   450         assertEquals("And the winner is", fin.getPlayers().get(0), fin.getWinner());
   451 
   452         assertFalse("No next move can be applied", fin.isApplicable(Move.fence('D', 3, Fence.Orientation.HORIZONTAL)));
   453 
   454         try {
   455             fin.apply(Move.EAST);
   456             fail("No moves allow when we are in final position");
   457         } catch (IllegalPositionException ex) {
   458             // OK
   459         }
   460 
   461     }
   462 
   463     public void testDetectInvalidFence() {
   464         try {
   465             Move m = Move.fence('D', 9, Orientation.HORIZONTAL);
   466             fail("Move shall not be allowed: " + m);
   467         } catch (IllegalPositionException ex) {
   468             // OK
   469         }
   470     }
   471 
   472     public void testEqualityOfMoves() throws Exception {
   473 
   474         for (Direction m1 : Direction.values()) {
   475             for (Direction m2 : Direction.values()) {
   476                 Move both1 = Move.jump(m1, m2);
   477                 Move both2 = Move.jump(m1, m2);
   478                 Move opposite = Move.jump(m2, m1);
   479 
   480                 assertTrue("Both boths are equal", both1.equals(both2));
   481                 assertFalse("Not north", Move.NORTH.equals(both1));
   482                 assertFalse("Not east", Move.EAST.equals(both1));
   483                 assertFalse("Not west", Move.WEST.equals(both1));
   484                 assertFalse("Not south", Move.SOUTH.equals(both1));
   485                 if (m1 == m2) {
   486                     continue;
   487                 }
   488                 assertFalse("Not equal to opposite", both1.equals(opposite));
   489             }
   490         }
   491 
   492         Move f1 = Move.fence('D', 6, Orientation.HORIZONTAL);
   493         Move f2 = Move.fence('D', 6, Orientation.HORIZONTAL);
   494         Move f3 = Move.fence('D', 6, Orientation.VERTICAL);
   495         Move f4 = Move.fence('E', 6, Orientation.VERTICAL);
   496         Move f5 = Move.fence('E', 5, Orientation.VERTICAL);
   497 
   498         assertEquals(f1, f2);
   499         assertFalse(f1.equals(f3));
   500         assertFalse(f4.equals(f5));
   501         assertFalse(f3.equals(f5));
   502     }
   503     static Board picture2board(String text) throws IOException, IllegalPositionException {
   504         StringReader sr = new StringReader(text);
   505         return Board.read(sr);
   506     }
   507 }