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