quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 29 Jul 2009 21:35:04 +0200
changeset 40 e45bc8ad2eaf
parent 31 8e6a6857c7b5
child 75 6802034b7a6f
permissions -rw-r--r--
Rotating the display of the board - north is on top. Enhancing the display with row ids and column numbers and compass directions.
     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 testTwoPlayers () {
    55         List<Player> list = board.getPlayers();
    56         assertEquals ("Two", 2, list.size ());
    57         assertFalse ("Both are non-null", list.contains (null));
    58         try {
    59             list.add (null);
    60             fail ("Modifications are not allowed");
    61         } catch (UnsupportedOperationException ex) {
    62             // ok
    63         }
    64         try {
    65             list.remove (0);
    66             fail ("Modifications are not allowed");
    67         } catch (UnsupportedOperationException ex) {
    68             // ok
    69         }
    70         
    71         
    72         assertEquals (8, list.get (0).getXInternal());
    73         assertEquals (0, list.get (0).getYInternal());
    74         assertEquals (10, list.get (0).getFences ());
    75         assertEquals (8, list.get (1).getXInternal());
    76         assertEquals (16, list.get (1).getYInternal());
    77         assertEquals (10, list.get (1).getFences ());
    78     }
    79     
    80     public void testFences () throws IllegalPositionException {
    81         assertEquals ("No on board", 0, board.getFences ().size ());
    82         try {
    83             board.getFences ().add (null);
    84             fail ("Should be unmodifiable");
    85         } catch (java.lang.UnsupportedOperationException ex) {
    86             // ok
    87         }
    88 
    89         {
    90             Board b = board.apply(Move.fence('A', 1, Orientation.HORIZONTAL));
    91             assertEquals("One fence placed: ", 1, b.getFences().size());
    92             Fence f = b.getFences().iterator().next();
    93             assertEquals("Row", 1, f.getRow());
    94             assertEquals("Column", 'A', f.getColumn());
    95         }
    96 
    97         {
    98             Board b = board.apply(Move.fence('A', 1, Orientation.VERTICAL));
    99             assertEquals("One fence placed: ", 1, b.getFences().size());
   100             Fence f = b.getFences().iterator().next();
   101             assertEquals("Row", 1, f.getRow());
   102             assertEquals("Column", 'A', f.getColumn());
   103         }
   104 
   105         {
   106             Board b = board.apply(Move.fence('H', 8, Orientation.HORIZONTAL));
   107             assertEquals("One fence placed: ", 1, b.getFences().size());
   108             Fence f = b.getFences().iterator().next();
   109             assertEquals("Row", 8, f.getRow());
   110             assertEquals("Column", 'H', f.getColumn());
   111         }
   112 
   113         {
   114             Board b = board.apply(Move.fence('H', 8, Orientation.VERTICAL));
   115             assertEquals("One fence placed: ", 1, b.getFences().size());
   116             Fence f = b.getFences().iterator().next();
   117             assertEquals("Row", 8, f.getRow());
   118             assertEquals("Column", 'H', f.getColumn());
   119         }
   120     }
   121     
   122     public void testFourTimesInAgainstEachOtherResultsInInvalidPosition () throws Exception {
   123         Board b = board;
   124         
   125         for (int i = 0; i < 3; i++) {
   126             b = move(b, 0, Player.Direction.NORTH);
   127             b = move(b, 1, Player.Direction.SOUTH);
   128         }
   129         
   130         b = move(b, 0, Player.Direction.NORTH);
   131         try {
   132             b = move(b, 1, Player.Direction.SOUTH);
   133             fail ("Now the positions of two players are supposed to be the same, this results in exception");
   134         } catch (IllegalPositionException ex) {
   135             // ok
   136         }
   137         
   138     }
   139 
   140     public void testCrossFences () throws Exception {
   141         Board b1 = board.fence (board.getPlayers ().get(0), 'A', 1, Fence.Orientation.HORIZONTAL);
   142         
   143         try {
   144             b1.fence (b1.getPlayers ().get(1), 'A', 1, Fence.Orientation.VERTICAL);
   145             fail ("This must fail, as the fences overlap");
   146         } catch (IllegalPositionException ex) {
   147             // ok
   148         }
   149     }
   150     
   151     public void testPawnsCanJumpOverEachOther () throws Exception {
   152         Board b = board;
   153         
   154         for (int i = 0; i < 3; i++) {
   155             b = move(b, 0, Player.Direction.NORTH);
   156             b = move(b, 1, Player.Direction.SOUTH);
   157         }
   158         
   159         b = move(b, 0, Player.Direction.NORTH);
   160         
   161         // jump over
   162         b = move(b, 1, Player.Direction.SOUTH, Player.Direction.SOUTH);
   163     }
   164 
   165     public void testCannotJumpOverFence () throws Exception {
   166         Board b = fence (board, 0, 'D', 8, Fence.Orientation.HORIZONTAL);
   167         assertEquals("One fence is present", 1, b.getFences().size());
   168         final Fence f = b.getFences().iterator().next();
   169         assertEquals("Row is 8", 8, f.getRow());
   170         assertEquals("Column is D", 'D', f.getColumn());
   171         try {
   172             move(b, 1, Player.Direction.SOUTH);
   173             fail ("This shall not be allowed, as there is the fence");
   174         } catch (IllegalPositionException ex) {
   175             // ok
   176         }
   177     }
   178     
   179 
   180     public void testSideJumpsNotAllowedWhenThereIsNoFence () throws Exception {
   181         Board b = board;
   182         
   183         for (int i = 0; i < 3; i++) {
   184             b = move(b, 0, Player.Direction.NORTH);
   185             b = move(b, 1, Player.Direction.SOUTH);
   186         }
   187         
   188         b = move(b, 0, Player.Direction.NORTH);
   189         
   190         try {
   191             b = move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
   192             fail ("Cannot just jump aside");
   193         } catch (IllegalPositionException ex) {
   194             // ok
   195         }
   196     }
   197     
   198     public void testSideJumpsAllowedWhenThereAFence () throws Exception {
   199         Board b = board;
   200         
   201         for (int i = 0; i < 3; i++) {
   202             b = move(b, 0, Player.Direction.NORTH);
   203             b = move(b, 1, Player.Direction.SOUTH);
   204         }
   205         
   206         b = move(b, 0, Player.Direction.NORTH);
   207         
   208         assertEquals (8, b.getPlayers ().get (0).getXInternal());
   209         assertEquals (8, b.getPlayers ().get (0).getYInternal());
   210         
   211         b = fence(b, 0, 'D', 4, Fence.Orientation.HORIZONTAL);
   212         
   213         // we can over jump to west
   214         move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
   215         // as well as east
   216         move(b, 1, Player.Direction.SOUTH, Player.Direction.EAST);
   217     }
   218 
   219     public void testPlaceAllFences() throws Exception {
   220         doPlaceAllFences(0);
   221     }
   222     public void testPlaceAllFences2() throws Exception {
   223         doPlaceAllFences(1);
   224     }
   225 
   226     private void doPlaceAllFences(int player) throws Exception {
   227         Board b = board;
   228 
   229         int cnt = 10;
   230         for (int i = 1; i <= 5; i++) {
   231             b = fence(b, player, 'A', i, Fence.Orientation.HORIZONTAL);
   232             b = fence(b, player, 'D', i, Fence.Orientation.HORIZONTAL);
   233             cnt -= 2;
   234             assertEquals("Two less" + i, cnt, b.getPlayers().get(player).getFences());
   235         }
   236 
   237         try {
   238             fence(b, player, 'F', 7, Fence.Orientation.VERTICAL);
   239             fail("We shall run out of fences");
   240         } catch (IllegalPositionException ex) {
   241             // OK
   242         }
   243     }
   244     
   245     public void testAlwaysHasToHaveAccessToEndLine () throws Exception {
   246         Board b = board;
   247         
   248         b = b.fence (b.getPlayers ().get (0), 'E', 1, Fence.Orientation.HORIZONTAL);
   249         b = b.fence (b.getPlayers ().get (0), 'F', 1, Fence.Orientation.VERTICAL);
   250 
   251         try {
   252             b = b.fence (b.getPlayers ().get (0), 'D', 1, Fence.Orientation.VERTICAL);
   253             fail ("This is not allowed as player 0 cannot now reach the final line");
   254         } catch (IllegalPositionException ex) {
   255             // ok
   256         }
   257         
   258     }
   259     
   260     public void testEqualsOfPlayers () throws Exception {
   261         Player p1 = new Player (1, 1, 10, Player.Direction.EAST);
   262         Player p2 = new Player (1, 1, 10, Player.Direction.EAST);
   263         Player p3 = new Player (2, 1, 10, Player.Direction.EAST);
   264         Player p4 = new Player (1, 1, 10, Player.Direction.WEST);
   265         Player p5 = new Player (1, 2, 10, Player.Direction.EAST);
   266         Player p6 = new Player (1, 1, 5, Player.Direction.EAST);
   267         
   268         assertEquals ("p1 == p2", p1, p2);
   269         if (p2.equals (p3)) fail ();
   270         if (p2.equals (p4)) fail ();
   271         if (p2.equals (p5)) fail ();
   272         if (p2.equals (p6)) fail ();
   273         if (p3.equals (p6)) fail ();
   274         if (p4.equals (p3)) fail ();
   275         if (p6.equals (p3)) fail ();
   276         if (p5.equals (p3)) fail ();
   277         if (p5.equals (p3)) fail ();
   278         if (p4.equals (p3)) fail ();
   279         if (p3.equals (p4)) fail ();
   280         if (p3.equals (p5)) fail ();
   281         if (p4.equals (p5)) fail ();
   282         if (p5.equals (p4)) fail ();
   283     }
   284     
   285     public void testEqualsOfFences () throws Exception {
   286         Fence f1 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
   287         Fence f2 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
   288         Fence f3 = new Fence (3, 1, Fence.Orientation.HORIZONTAL);
   289         Fence f4 = new Fence (1, 3, Fence.Orientation.HORIZONTAL);
   290         Fence f5 = new Fence (1, 1, Fence.Orientation.VERTICAL);
   291         
   292         assertEquals ("f1 == f2", f1, f2);
   293         if (f1.equals (f3)) fail ();
   294         if (f3.equals (f1)) fail ();
   295         if (f5.equals (f1)) fail ();
   296         if (f1.equals (f5)) fail ();
   297         if (f4.equals (f1)) fail ();
   298         if (f1.equals (f4)) fail ();
   299     }
   300     
   301     public void testEqualsOfBoards1 () throws Exception {
   302         Board b1 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
   303         Board b2 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
   304         Board b3 = board.move (board.getPlayers ().get (0), Player.Direction.EAST);
   305         
   306         if (b1.equals (b3)) fail ();
   307         if (b3.equals (b1)) fail ();
   308         
   309         assertEquals ("b1 == b2", b1, b2);
   310     }
   311     public void testEqualsOfBoards2 () throws Exception {
   312         Board b1 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
   313         Board b2 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
   314         Board b3 = board.fence (board.getPlayers ().get (0), 'D', 3, Fence.Orientation.HORIZONTAL);
   315         Board b4 = board.fence (board.getPlayers ().get (0), 'E', 4, Fence.Orientation.HORIZONTAL);
   316         Board b5 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.VERTICAL);
   317         
   318         if (b1.equals (b3)) fail ();
   319         if (b3.equals (b1)) fail ();
   320         if (b1.equals (b4)) fail ();
   321         if (b1.equals (b5)) fail ();
   322         if (b5.equals (b1)) fail ();
   323         if (b4.equals (b1)) fail ();
   324         
   325         assertEquals ("b1 == b2", b1, b2);
   326     }
   327     public void testEqualsOfBoardsWhenJumpOver () throws Exception {
   328         Board b = board;
   329         
   330         for (int i = 0; i < 3; i++) {
   331             b = move(b, 0, Player.Direction.NORTH);
   332             b = move(b, 1, Player.Direction.SOUTH);
   333         }
   334         
   335         Board b1 = move(b, 0, Player.Direction.NORTH);
   336         
   337         Board b2 = move(b, 1, Player.Direction.SOUTH);
   338         b2 = b2.move (b2.getPlayers ().get (0), Player.Direction.NORTH, Player.Direction.NORTH);
   339         
   340         if (b1.equals (b2)) fail ("Not the same, pawns are reverted");
   341         if (b2.equals (b1)) fail ("Not the same, pawns are reverted");
   342     }
   343 
   344     public void testMoveAltersCurrentPlayer() throws Exception {
   345         assertEquals("First player ready", board.getCurrentPlayer(), board.getPlayers().get(0));
   346         Board b = apply(board, Move.EAST);
   347 
   348         for (int i = 0; i < 7; i++) {
   349             assertEquals("Snd player ready", b.getCurrentPlayer(), b.getPlayers().get(1));
   350             b = apply(b, Move.SOUTH);
   351             assertEquals("First player ready", b.getCurrentPlayer(), b.getPlayers().get(0));
   352             b = apply(b, Move.NORTH);
   353         }
   354 
   355         Board fin = b.apply(Move.NORTH).apply(Move.NORTH);
   356         assertNotNull("There is a winner", fin.getWinner());
   357         assertEquals("And the winner is", fin.getPlayers().get(0), fin.getWinner());
   358 
   359         assertFalse("No next move can be applied", fin.isApplicable(Move.fence('D', 3, Fence.Orientation.HORIZONTAL)));
   360 
   361         try {
   362             fin.apply(Move.EAST);
   363             fail("No moves allow when we are in final position");
   364         } catch (IllegalPositionException ex) {
   365             // OK
   366         }
   367 
   368     }
   369 
   370     public void testDetectInvalidFence() {
   371         try {
   372             Move m = Move.fence('D', 9, Orientation.HORIZONTAL);
   373             fail("Move shall not be allowed: " + m);
   374         } catch (IllegalPositionException ex) {
   375             // OK
   376         }
   377     }
   378 
   379     public void testEqualityOfMoves() throws Exception {
   380 
   381         for (Direction m1 : Direction.values()) {
   382             for (Direction m2 : Direction.values()) {
   383                 Move both1 = Move.jump(m1, m2);
   384                 Move both2 = Move.jump(m1, m2);
   385                 Move opposite = Move.jump(m2, m1);
   386 
   387                 assertTrue("Both boths are equal", both1.equals(both2));
   388                 assertFalse("Not north", Move.NORTH.equals(both1));
   389                 assertFalse("Not east", Move.EAST.equals(both1));
   390                 assertFalse("Not west", Move.WEST.equals(both1));
   391                 assertFalse("Not south", Move.SOUTH.equals(both1));
   392                 if (m1 == m2) {
   393                     continue;
   394                 }
   395                 assertFalse("Not equal to opposite", both1.equals(opposite));
   396             }
   397         }
   398 
   399         Move f1 = Move.fence('D', 6, Orientation.HORIZONTAL);
   400         Move f2 = Move.fence('D', 6, Orientation.HORIZONTAL);
   401         Move f3 = Move.fence('D', 6, Orientation.VERTICAL);
   402         Move f4 = Move.fence('E', 6, Orientation.VERTICAL);
   403         Move f5 = Move.fence('E', 5, Orientation.VERTICAL);
   404 
   405         assertEquals(f1, f2);
   406         assertFalse(f1.equals(f3));
   407         assertFalse(f4.equals(f5));
   408         assertFalse(f3.equals(f5));
   409     }
   410 }