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