quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 10 Sep 2009 23:19:40 +0200
changeset 75 6802034b7a6f
parent 40 e45bc8ad2eaf
child 92 de3dd5710a5c
permissions -rw-r--r--
Support for giving up the game
     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     
   148     public void testFourTimesInAgainstEachOtherResultsInInvalidPosition () throws Exception {
   149         Board b = board;
   150         
   151         for (int i = 0; i < 3; i++) {
   152             b = move(b, 0, Player.Direction.NORTH);
   153             b = move(b, 1, Player.Direction.SOUTH);
   154         }
   155         
   156         b = move(b, 0, Player.Direction.NORTH);
   157         try {
   158             b = move(b, 1, Player.Direction.SOUTH);
   159             fail ("Now the positions of two players are supposed to be the same, this results in exception");
   160         } catch (IllegalPositionException ex) {
   161             // ok
   162         }
   163         
   164     }
   165 
   166     public void testCrossFences () throws Exception {
   167         Board b1 = board.fence (board.getPlayers ().get(0), 'A', 1, Fence.Orientation.HORIZONTAL);
   168         
   169         try {
   170             b1.fence (b1.getPlayers ().get(1), 'A', 1, Fence.Orientation.VERTICAL);
   171             fail ("This must fail, as the fences overlap");
   172         } catch (IllegalPositionException ex) {
   173             // ok
   174         }
   175     }
   176     
   177     public void testPawnsCanJumpOverEachOther () throws Exception {
   178         Board b = board;
   179         
   180         for (int i = 0; i < 3; i++) {
   181             b = move(b, 0, Player.Direction.NORTH);
   182             b = move(b, 1, Player.Direction.SOUTH);
   183         }
   184         
   185         b = move(b, 0, Player.Direction.NORTH);
   186         
   187         // jump over
   188         b = move(b, 1, Player.Direction.SOUTH, Player.Direction.SOUTH);
   189     }
   190 
   191     public void testCannotJumpOverFence () throws Exception {
   192         Board b = fence (board, 0, 'D', 8, Fence.Orientation.HORIZONTAL);
   193         assertEquals("One fence is present", 1, b.getFences().size());
   194         final Fence f = b.getFences().iterator().next();
   195         assertEquals("Row is 8", 8, f.getRow());
   196         assertEquals("Column is D", 'D', f.getColumn());
   197         try {
   198             move(b, 1, Player.Direction.SOUTH);
   199             fail ("This shall not be allowed, as there is the fence");
   200         } catch (IllegalPositionException ex) {
   201             // ok
   202         }
   203     }
   204     
   205 
   206     public void testSideJumpsNotAllowedWhenThereIsNoFence () throws Exception {
   207         Board b = board;
   208         
   209         for (int i = 0; i < 3; i++) {
   210             b = move(b, 0, Player.Direction.NORTH);
   211             b = move(b, 1, Player.Direction.SOUTH);
   212         }
   213         
   214         b = move(b, 0, Player.Direction.NORTH);
   215         
   216         try {
   217             b = move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
   218             fail ("Cannot just jump aside");
   219         } catch (IllegalPositionException ex) {
   220             // ok
   221         }
   222     }
   223     
   224     public void testSideJumpsAllowedWhenThereAFence () throws Exception {
   225         Board b = board;
   226         
   227         for (int i = 0; i < 3; i++) {
   228             b = move(b, 0, Player.Direction.NORTH);
   229             b = move(b, 1, Player.Direction.SOUTH);
   230         }
   231         
   232         b = move(b, 0, Player.Direction.NORTH);
   233         
   234         assertEquals (8, b.getPlayers ().get (0).getXInternal());
   235         assertEquals (8, b.getPlayers ().get (0).getYInternal());
   236         
   237         b = fence(b, 0, 'D', 4, Fence.Orientation.HORIZONTAL);
   238         
   239         // we can over jump to west
   240         move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
   241         // as well as east
   242         move(b, 1, Player.Direction.SOUTH, Player.Direction.EAST);
   243     }
   244 
   245     public void testPlaceAllFences() throws Exception {
   246         doPlaceAllFences(0);
   247     }
   248     public void testPlaceAllFences2() throws Exception {
   249         doPlaceAllFences(1);
   250     }
   251 
   252     private void doPlaceAllFences(int player) throws Exception {
   253         Board b = board;
   254 
   255         int cnt = 10;
   256         for (int i = 1; i <= 5; i++) {
   257             b = fence(b, player, 'A', i, Fence.Orientation.HORIZONTAL);
   258             b = fence(b, player, 'D', i, Fence.Orientation.HORIZONTAL);
   259             cnt -= 2;
   260             assertEquals("Two less" + i, cnt, b.getPlayers().get(player).getFences());
   261         }
   262 
   263         try {
   264             fence(b, player, 'F', 7, Fence.Orientation.VERTICAL);
   265             fail("We shall run out of fences");
   266         } catch (IllegalPositionException ex) {
   267             // OK
   268         }
   269     }
   270     
   271     public void testAlwaysHasToHaveAccessToEndLine () throws Exception {
   272         Board b = board;
   273         
   274         b = b.fence (b.getPlayers ().get (0), 'E', 1, Fence.Orientation.HORIZONTAL);
   275         b = b.fence (b.getPlayers ().get (0), 'F', 1, Fence.Orientation.VERTICAL);
   276 
   277         try {
   278             b = b.fence (b.getPlayers ().get (0), 'D', 1, Fence.Orientation.VERTICAL);
   279             fail ("This is not allowed as player 0 cannot now reach the final line");
   280         } catch (IllegalPositionException ex) {
   281             // ok
   282         }
   283         
   284     }
   285     
   286     public void testEqualsOfPlayers () throws Exception {
   287         Player p1 = new Player (1, 1, 10, Player.Direction.EAST);
   288         Player p2 = new Player (1, 1, 10, Player.Direction.EAST);
   289         Player p3 = new Player (2, 1, 10, Player.Direction.EAST);
   290         Player p4 = new Player (1, 1, 10, Player.Direction.WEST);
   291         Player p5 = new Player (1, 2, 10, Player.Direction.EAST);
   292         Player p6 = new Player (1, 1, 5, Player.Direction.EAST);
   293         
   294         assertEquals ("p1 == p2", p1, p2);
   295         if (p2.equals (p3)) fail ();
   296         if (p2.equals (p4)) fail ();
   297         if (p2.equals (p5)) fail ();
   298         if (p2.equals (p6)) fail ();
   299         if (p3.equals (p6)) fail ();
   300         if (p4.equals (p3)) fail ();
   301         if (p6.equals (p3)) fail ();
   302         if (p5.equals (p3)) fail ();
   303         if (p5.equals (p3)) fail ();
   304         if (p4.equals (p3)) fail ();
   305         if (p3.equals (p4)) fail ();
   306         if (p3.equals (p5)) fail ();
   307         if (p4.equals (p5)) fail ();
   308         if (p5.equals (p4)) fail ();
   309     }
   310     
   311     public void testEqualsOfFences () throws Exception {
   312         Fence f1 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
   313         Fence f2 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
   314         Fence f3 = new Fence (3, 1, Fence.Orientation.HORIZONTAL);
   315         Fence f4 = new Fence (1, 3, Fence.Orientation.HORIZONTAL);
   316         Fence f5 = new Fence (1, 1, Fence.Orientation.VERTICAL);
   317         
   318         assertEquals ("f1 == f2", f1, f2);
   319         if (f1.equals (f3)) fail ();
   320         if (f3.equals (f1)) fail ();
   321         if (f5.equals (f1)) fail ();
   322         if (f1.equals (f5)) fail ();
   323         if (f4.equals (f1)) fail ();
   324         if (f1.equals (f4)) fail ();
   325     }
   326     
   327     public void testEqualsOfBoards1 () throws Exception {
   328         Board b1 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
   329         Board b2 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
   330         Board b3 = board.move (board.getPlayers ().get (0), Player.Direction.EAST);
   331         
   332         if (b1.equals (b3)) fail ();
   333         if (b3.equals (b1)) fail ();
   334         
   335         assertEquals ("b1 == b2", b1, b2);
   336     }
   337     public void testEqualsOfBoards2 () throws Exception {
   338         Board b1 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
   339         Board b2 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
   340         Board b3 = board.fence (board.getPlayers ().get (0), 'D', 3, Fence.Orientation.HORIZONTAL);
   341         Board b4 = board.fence (board.getPlayers ().get (0), 'E', 4, Fence.Orientation.HORIZONTAL);
   342         Board b5 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.VERTICAL);
   343         
   344         if (b1.equals (b3)) fail ();
   345         if (b3.equals (b1)) fail ();
   346         if (b1.equals (b4)) fail ();
   347         if (b1.equals (b5)) fail ();
   348         if (b5.equals (b1)) fail ();
   349         if (b4.equals (b1)) fail ();
   350         
   351         assertEquals ("b1 == b2", b1, b2);
   352     }
   353     public void testEqualsOfBoardsWhenJumpOver () throws Exception {
   354         Board b = board;
   355         
   356         for (int i = 0; i < 3; i++) {
   357             b = move(b, 0, Player.Direction.NORTH);
   358             b = move(b, 1, Player.Direction.SOUTH);
   359         }
   360         
   361         Board b1 = move(b, 0, Player.Direction.NORTH);
   362         
   363         Board b2 = move(b, 1, Player.Direction.SOUTH);
   364         b2 = b2.move (b2.getPlayers ().get (0), Player.Direction.NORTH, Player.Direction.NORTH);
   365         
   366         if (b1.equals (b2)) fail ("Not the same, pawns are reverted");
   367         if (b2.equals (b1)) fail ("Not the same, pawns are reverted");
   368     }
   369 
   370     public void testMoveAltersCurrentPlayer() throws Exception {
   371         assertEquals("First player ready", board.getCurrentPlayer(), board.getPlayers().get(0));
   372         Board b = apply(board, Move.EAST);
   373 
   374         for (int i = 0; i < 7; i++) {
   375             assertEquals("Snd player ready", b.getCurrentPlayer(), b.getPlayers().get(1));
   376             b = apply(b, Move.SOUTH);
   377             assertEquals("First player ready", b.getCurrentPlayer(), b.getPlayers().get(0));
   378             b = apply(b, Move.NORTH);
   379         }
   380 
   381         Board fin = b.apply(Move.NORTH).apply(Move.NORTH);
   382         assertNotNull("There is a winner", fin.getWinner());
   383         assertEquals("And the winner is", fin.getPlayers().get(0), fin.getWinner());
   384 
   385         assertFalse("No next move can be applied", fin.isApplicable(Move.fence('D', 3, Fence.Orientation.HORIZONTAL)));
   386 
   387         try {
   388             fin.apply(Move.EAST);
   389             fail("No moves allow when we are in final position");
   390         } catch (IllegalPositionException ex) {
   391             // OK
   392         }
   393 
   394     }
   395 
   396     public void testDetectInvalidFence() {
   397         try {
   398             Move m = Move.fence('D', 9, Orientation.HORIZONTAL);
   399             fail("Move shall not be allowed: " + m);
   400         } catch (IllegalPositionException ex) {
   401             // OK
   402         }
   403     }
   404 
   405     public void testEqualityOfMoves() throws Exception {
   406 
   407         for (Direction m1 : Direction.values()) {
   408             for (Direction m2 : Direction.values()) {
   409                 Move both1 = Move.jump(m1, m2);
   410                 Move both2 = Move.jump(m1, m2);
   411                 Move opposite = Move.jump(m2, m1);
   412 
   413                 assertTrue("Both boths are equal", both1.equals(both2));
   414                 assertFalse("Not north", Move.NORTH.equals(both1));
   415                 assertFalse("Not east", Move.EAST.equals(both1));
   416                 assertFalse("Not west", Move.WEST.equals(both1));
   417                 assertFalse("Not south", Move.SOUTH.equals(both1));
   418                 if (m1 == m2) {
   419                     continue;
   420                 }
   421                 assertFalse("Not equal to opposite", both1.equals(opposite));
   422             }
   423         }
   424 
   425         Move f1 = Move.fence('D', 6, Orientation.HORIZONTAL);
   426         Move f2 = Move.fence('D', 6, Orientation.HORIZONTAL);
   427         Move f3 = Move.fence('D', 6, Orientation.VERTICAL);
   428         Move f4 = Move.fence('E', 6, Orientation.VERTICAL);
   429         Move f5 = Move.fence('E', 5, Orientation.VERTICAL);
   430 
   431         assertEquals(f1, f2);
   432         assertFalse(f1.equals(f3));
   433         assertFalse(f4.equals(f5));
   434         assertFalse(f3.equals(f5));
   435     }
   436 }