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