quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 22 Sep 2009 23:06:16 +0200
changeset 107 152aedcc45d0
parent 92 de3dd5710a5c
child 178 4b78d4f028b3
permissions -rw-r--r--
Cannot place the same fence twice
     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         Board begin = Board.valueOf(b);
   204 
   205         try {
   206             Board bad = fence(begin, 0, 'G', 2, Orientation.HORIZONTAL);
   207             fail("Not allowed move:\n" + bad.toString());
   208         } catch (IllegalPositionException ex) {
   209             // OK
   210         }
   211     }
   212     public void testFourTimesInAgainstEachOtherResultsInInvalidPosition () throws Exception {
   213         Board b = board;
   214         
   215         for (int i = 0; i < 3; i++) {
   216             b = move(b, 0, Player.Direction.NORTH);
   217             b = move(b, 1, Player.Direction.SOUTH);
   218         }
   219         
   220         b = move(b, 0, Player.Direction.NORTH);
   221         try {
   222             b = move(b, 1, Player.Direction.SOUTH);
   223             fail ("Now the positions of two players are supposed to be the same, this results in exception");
   224         } catch (IllegalPositionException ex) {
   225             // ok
   226         }
   227         
   228     }
   229 
   230     public void testCrossFences () throws Exception {
   231         Board b1 = board.fence (board.getPlayers ().get(0), 'A', 1, Fence.Orientation.HORIZONTAL);
   232         
   233         try {
   234             b1.fence (b1.getPlayers ().get(1), 'A', 1, Fence.Orientation.VERTICAL);
   235             fail ("This must fail, as the fences overlap");
   236         } catch (IllegalPositionException ex) {
   237             // ok
   238         }
   239     }
   240     
   241     public void testPawnsCanJumpOverEachOther () throws Exception {
   242         Board b = board;
   243         
   244         for (int i = 0; i < 3; i++) {
   245             b = move(b, 0, Player.Direction.NORTH);
   246             b = move(b, 1, Player.Direction.SOUTH);
   247         }
   248         
   249         b = move(b, 0, Player.Direction.NORTH);
   250         
   251         // jump over
   252         b = move(b, 1, Player.Direction.SOUTH, Player.Direction.SOUTH);
   253     }
   254 
   255     public void testCannotJumpOverFence () throws Exception {
   256         Board b = fence (board, 0, 'D', 8, Fence.Orientation.HORIZONTAL);
   257         assertEquals("One fence is present", 1, b.getFences().size());
   258         final Fence f = b.getFences().iterator().next();
   259         assertEquals("Row is 8", 8, f.getRow());
   260         assertEquals("Column is D", 'D', f.getColumn());
   261         try {
   262             move(b, 1, Player.Direction.SOUTH);
   263             fail ("This shall not be allowed, as there is the fence");
   264         } catch (IllegalPositionException ex) {
   265             // ok
   266         }
   267     }
   268     
   269 
   270     public void testSideJumpsNotAllowedWhenThereIsNoFence () throws Exception {
   271         Board b = board;
   272         
   273         for (int i = 0; i < 3; i++) {
   274             b = move(b, 0, Player.Direction.NORTH);
   275             b = move(b, 1, Player.Direction.SOUTH);
   276         }
   277         
   278         b = move(b, 0, Player.Direction.NORTH);
   279         
   280         try {
   281             b = move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
   282             fail ("Cannot just jump aside");
   283         } catch (IllegalPositionException ex) {
   284             // ok
   285         }
   286     }
   287     
   288     public void testSideJumpsAllowedWhenThereAFence () throws Exception {
   289         Board b = board;
   290         
   291         for (int i = 0; i < 3; i++) {
   292             b = move(b, 0, Player.Direction.NORTH);
   293             b = move(b, 1, Player.Direction.SOUTH);
   294         }
   295         
   296         b = move(b, 0, Player.Direction.NORTH);
   297         
   298         assertEquals (8, b.getPlayers ().get (0).getXInternal());
   299         assertEquals (8, b.getPlayers ().get (0).getYInternal());
   300         
   301         b = fence(b, 0, 'D', 4, Fence.Orientation.HORIZONTAL);
   302         
   303         // we can over jump to west
   304         move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
   305         // as well as east
   306         move(b, 1, Player.Direction.SOUTH, Player.Direction.EAST);
   307     }
   308 
   309     public void testPlaceAllFences() throws Exception {
   310         doPlaceAllFences(0);
   311     }
   312     public void testPlaceAllFences2() throws Exception {
   313         doPlaceAllFences(1);
   314     }
   315 
   316     private void doPlaceAllFences(int player) throws Exception {
   317         Board b = board;
   318 
   319         int cnt = 10;
   320         for (int i = 1; i <= 5; i++) {
   321             b = fence(b, player, 'A', i, Fence.Orientation.HORIZONTAL);
   322             b = fence(b, player, 'D', i, Fence.Orientation.HORIZONTAL);
   323             cnt -= 2;
   324             assertEquals("Two less" + i, cnt, b.getPlayers().get(player).getFences());
   325         }
   326 
   327         try {
   328             fence(b, player, 'F', 7, Fence.Orientation.VERTICAL);
   329             fail("We shall run out of fences");
   330         } catch (IllegalPositionException ex) {
   331             // OK
   332         }
   333     }
   334     
   335     public void testAlwaysHasToHaveAccessToEndLine () throws Exception {
   336         Board b = board;
   337         
   338         b = b.fence (b.getPlayers ().get (0), 'E', 1, Fence.Orientation.HORIZONTAL);
   339         b = b.fence (b.getPlayers ().get (0), 'F', 1, Fence.Orientation.VERTICAL);
   340 
   341         try {
   342             b = b.fence (b.getPlayers ().get (0), 'D', 1, Fence.Orientation.VERTICAL);
   343             fail ("This is not allowed as player 0 cannot now reach the final line");
   344         } catch (IllegalPositionException ex) {
   345             // ok
   346         }
   347         
   348     }
   349     
   350     public void testEqualsOfPlayers () throws Exception {
   351         Player p1 = new Player (1, 1, 10, Player.Direction.EAST);
   352         Player p2 = new Player (1, 1, 10, Player.Direction.EAST);
   353         Player p3 = new Player (2, 1, 10, Player.Direction.EAST);
   354         Player p4 = new Player (1, 1, 10, Player.Direction.WEST);
   355         Player p5 = new Player (1, 2, 10, Player.Direction.EAST);
   356         Player p6 = new Player (1, 1, 5, Player.Direction.EAST);
   357         
   358         assertEquals ("p1 == p2", p1, p2);
   359         if (p2.equals (p3)) fail ();
   360         if (p2.equals (p4)) fail ();
   361         if (p2.equals (p5)) fail ();
   362         if (p2.equals (p6)) fail ();
   363         if (p3.equals (p6)) fail ();
   364         if (p4.equals (p3)) fail ();
   365         if (p6.equals (p3)) fail ();
   366         if (p5.equals (p3)) fail ();
   367         if (p5.equals (p3)) fail ();
   368         if (p4.equals (p3)) fail ();
   369         if (p3.equals (p4)) fail ();
   370         if (p3.equals (p5)) fail ();
   371         if (p4.equals (p5)) fail ();
   372         if (p5.equals (p4)) fail ();
   373     }
   374     
   375     public void testEqualsOfFences () throws Exception {
   376         Fence f1 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
   377         Fence f2 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
   378         Fence f3 = new Fence (3, 1, Fence.Orientation.HORIZONTAL);
   379         Fence f4 = new Fence (1, 3, Fence.Orientation.HORIZONTAL);
   380         Fence f5 = new Fence (1, 1, Fence.Orientation.VERTICAL);
   381         
   382         assertEquals ("f1 == f2", f1, f2);
   383         if (f1.equals (f3)) fail ();
   384         if (f3.equals (f1)) fail ();
   385         if (f5.equals (f1)) fail ();
   386         if (f1.equals (f5)) fail ();
   387         if (f4.equals (f1)) fail ();
   388         if (f1.equals (f4)) fail ();
   389     }
   390     
   391     public void testEqualsOfBoards1 () throws Exception {
   392         Board b1 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
   393         Board b2 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
   394         Board b3 = board.move (board.getPlayers ().get (0), Player.Direction.EAST);
   395         
   396         if (b1.equals (b3)) fail ();
   397         if (b3.equals (b1)) fail ();
   398         
   399         assertEquals ("b1 == b2", b1, b2);
   400     }
   401     public void testEqualsOfBoards2 () throws Exception {
   402         Board b1 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
   403         Board b2 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
   404         Board b3 = board.fence (board.getPlayers ().get (0), 'D', 3, Fence.Orientation.HORIZONTAL);
   405         Board b4 = board.fence (board.getPlayers ().get (0), 'E', 4, Fence.Orientation.HORIZONTAL);
   406         Board b5 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.VERTICAL);
   407         
   408         if (b1.equals (b3)) fail ();
   409         if (b3.equals (b1)) fail ();
   410         if (b1.equals (b4)) fail ();
   411         if (b1.equals (b5)) fail ();
   412         if (b5.equals (b1)) fail ();
   413         if (b4.equals (b1)) fail ();
   414         
   415         assertEquals ("b1 == b2", b1, b2);
   416     }
   417     public void testEqualsOfBoardsWhenJumpOver () throws Exception {
   418         Board b = board;
   419         
   420         for (int i = 0; i < 3; i++) {
   421             b = move(b, 0, Player.Direction.NORTH);
   422             b = move(b, 1, Player.Direction.SOUTH);
   423         }
   424         
   425         Board b1 = move(b, 0, Player.Direction.NORTH);
   426         
   427         Board b2 = move(b, 1, Player.Direction.SOUTH);
   428         b2 = b2.move (b2.getPlayers ().get (0), Player.Direction.NORTH, Player.Direction.NORTH);
   429         
   430         if (b1.equals (b2)) fail ("Not the same, pawns are reverted");
   431         if (b2.equals (b1)) fail ("Not the same, pawns are reverted");
   432     }
   433 
   434     public void testMoveAltersCurrentPlayer() throws Exception {
   435         assertEquals("First player ready", board.getCurrentPlayer(), board.getPlayers().get(0));
   436         Board b = apply(board, Move.EAST);
   437 
   438         for (int i = 0; i < 7; i++) {
   439             assertEquals("Snd player ready", b.getCurrentPlayer(), b.getPlayers().get(1));
   440             b = apply(b, Move.SOUTH);
   441             assertEquals("First player ready", b.getCurrentPlayer(), b.getPlayers().get(0));
   442             b = apply(b, Move.NORTH);
   443         }
   444 
   445         Board fin = b.apply(Move.NORTH).apply(Move.NORTH);
   446         assertNotNull("There is a winner", fin.getWinner());
   447         assertEquals("And the winner is", fin.getPlayers().get(0), fin.getWinner());
   448 
   449         assertFalse("No next move can be applied", fin.isApplicable(Move.fence('D', 3, Fence.Orientation.HORIZONTAL)));
   450 
   451         try {
   452             fin.apply(Move.EAST);
   453             fail("No moves allow when we are in final position");
   454         } catch (IllegalPositionException ex) {
   455             // OK
   456         }
   457 
   458     }
   459 
   460     public void testDetectInvalidFence() {
   461         try {
   462             Move m = Move.fence('D', 9, Orientation.HORIZONTAL);
   463             fail("Move shall not be allowed: " + m);
   464         } catch (IllegalPositionException ex) {
   465             // OK
   466         }
   467     }
   468 
   469     public void testEqualityOfMoves() throws Exception {
   470 
   471         for (Direction m1 : Direction.values()) {
   472             for (Direction m2 : Direction.values()) {
   473                 Move both1 = Move.jump(m1, m2);
   474                 Move both2 = Move.jump(m1, m2);
   475                 Move opposite = Move.jump(m2, m1);
   476 
   477                 assertTrue("Both boths are equal", both1.equals(both2));
   478                 assertFalse("Not north", Move.NORTH.equals(both1));
   479                 assertFalse("Not east", Move.EAST.equals(both1));
   480                 assertFalse("Not west", Move.WEST.equals(both1));
   481                 assertFalse("Not south", Move.SOUTH.equals(both1));
   482                 if (m1 == m2) {
   483                     continue;
   484                 }
   485                 assertFalse("Not equal to opposite", both1.equals(opposite));
   486             }
   487         }
   488 
   489         Move f1 = Move.fence('D', 6, Orientation.HORIZONTAL);
   490         Move f2 = Move.fence('D', 6, Orientation.HORIZONTAL);
   491         Move f3 = Move.fence('D', 6, Orientation.VERTICAL);
   492         Move f4 = Move.fence('E', 6, Orientation.VERTICAL);
   493         Move f5 = Move.fence('E', 5, Orientation.VERTICAL);
   494 
   495         assertEquals(f1, f2);
   496         assertFalse(f1.equals(f3));
   497         assertFalse(f4.equals(f5));
   498         assertFalse(f3.equals(f5));
   499     }
   500 }