quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 05 Sep 2010 11:04:26 +0200
changeset 253 ee02205edf13
parent 245 ba49bfb120f7
child 264 d60370059c3c
permissions -rw-r--r--
Showing x when a move to certain position is impossible, showing o when it is fine. Adding Board.findMove method to keep the allowed move logic.
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
    }
jaroslav@253
   233
    public void testCanNorth() {
jaroslav@253
   234
        Move m = board.findMove(board.getCurrentPlayer(), 4, 1);
jaroslav@253
   235
        assertEquals("Go north", Move.NORTH, m);
jaroslav@253
   236
    }
jaroslav@253
   237
    public void testCanEast() {
jaroslav@253
   238
        Move m = board.findMove(board.getCurrentPlayer(), 5, 0);
jaroslav@253
   239
        assertEquals("Go east", Move.EAST, m);
jaroslav@253
   240
    }
jaroslav@253
   241
    public void testCanWest() {
jaroslav@253
   242
        Move m = board.findMove(board.getCurrentPlayer(), 3, 0);
jaroslav@253
   243
        assertEquals("Go west", Move.WEST, m);
jaroslav@253
   244
    }
jaroslav@253
   245
    public void testCannotWestAndNorth() {
jaroslav@253
   246
        Move m = board.findMove(board.getCurrentPlayer(), 3, 1);
jaroslav@253
   247
        assertNull("No way", m);
jaroslav@253
   248
    }
jaroslav@253
   249
    public void testCanJumpStraight () throws Exception {
jaroslav@253
   250
        Board b = board;
jaroslav@253
   251
        
jaroslav@253
   252
        for (int i = 0; i < 3; i++) {
jaroslav@253
   253
            b = move(b, 0, Player.Direction.NORTH);
jaroslav@253
   254
            b = move(b, 1, Player.Direction.SOUTH);
jaroslav@253
   255
        }
jaroslav@253
   256
        b = apply(b, Move.NORTH);
jaroslav@253
   257
        Move m = b.findMove(b.getPlayers().get(0), 4, 6);
jaroslav@253
   258
        assertEquals("N+N", Move.jump(Direction.NORTH, Direction.NORTH), m);
jaroslav@253
   259
    }
jaroslav@253
   260
    public void testCanJumSptraightAndTurn () throws Exception {
jaroslav@253
   261
        Board b = board;
jaroslav@253
   262
        
jaroslav@253
   263
        for (int i = 0; i < 3; i++) {
jaroslav@253
   264
            b = move(b, 0, Player.Direction.NORTH);
jaroslav@253
   265
            b = move(b, 1, Player.Direction.SOUTH);
jaroslav@253
   266
        }
jaroslav@253
   267
        b = apply(b, Move.fence('A', 1, Orientation.HORIZONTAL));
jaroslav@253
   268
        b = apply(b, Move.SOUTH);
jaroslav@253
   269
        b = apply(b, Move.fence('D', 5, Orientation.HORIZONTAL));
jaroslav@253
   270
        b = apply(b, Move.fence('A', 8, Orientation.HORIZONTAL));
jaroslav@253
   271
        
jaroslav@253
   272
        Move m = b.findMove(b.getCurrentPlayer(), 4, 5);
jaroslav@253
   273
        assertNull("No N+N", m);
jaroslav@253
   274
        m = b.findMove(b.getCurrentPlayer(), 5, 4);
jaroslav@253
   275
        assertEquals("N+E is OK", Move.jump(Direction.NORTH, Direction.EAST), m);
jaroslav@253
   276
    }
jtulach@0
   277
jtulach@0
   278
    public void testCrossFences () throws Exception {
jtulach@15
   279
        Board b1 = board.fence (board.getPlayers ().get(0), 'A', 1, Fence.Orientation.HORIZONTAL);
jtulach@0
   280
        
jtulach@0
   281
        try {
jtulach@15
   282
            b1.fence (b1.getPlayers ().get(1), 'A', 1, Fence.Orientation.VERTICAL);
jtulach@0
   283
            fail ("This must fail, as the fences overlap");
jtulach@0
   284
        } catch (IllegalPositionException ex) {
jtulach@0
   285
            // ok
jtulach@0
   286
        }
jtulach@0
   287
    }
jtulach@0
   288
    
jtulach@0
   289
    public void testPawnsCanJumpOverEachOther () 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@0
   299
        // jump over
jtulach@15
   300
        b = move(b, 1, Player.Direction.SOUTH, Player.Direction.SOUTH);
jtulach@0
   301
    }
jtulach@0
   302
jaroslav@237
   303
    public void testJumpBackForbidden() throws Exception {
jaroslav@237
   304
        Board b = board;
jaroslav@237
   305
jaroslav@237
   306
        for (int i = 0; i < 3; i++) {
jaroslav@237
   307
            b = move(b, 0, Player.Direction.NORTH);
jaroslav@237
   308
            b = move(b, 1, Player.Direction.SOUTH);
jaroslav@237
   309
        }
jaroslav@237
   310
jaroslav@237
   311
        b = move(b, 0, Player.Direction.NORTH);
jaroslav@237
   312
jaroslav@237
   313
        b = fence(b, 1, 'D', 6, Orientation.HORIZONTAL);
jaroslav@237
   314
jaroslav@237
   315
        try {
jaroslav@237
   316
            b = move(b, 0, Player.Direction.NORTH, Player.Direction.NORTH);
jaroslav@237
   317
            fail("Can't jump over a pawn when there is a fence");
jaroslav@237
   318
        } catch (IllegalPositionException ex) {
jaroslav@237
   319
            // OK
jaroslav@237
   320
        }
jaroslav@237
   321
jaroslav@237
   322
        try {
jaroslav@237
   323
            b = move(b, 0, Player.Direction.NORTH, Player.Direction.SOUTH);
jaroslav@237
   324
            fail("Can't bounce from the fence back neither");
jaroslav@237
   325
        } catch (IllegalPositionException ex) {
jaroslav@237
   326
            // OK
jaroslav@237
   327
        }
jaroslav@237
   328
    }
jaroslav@237
   329
jtulach@0
   330
    public void testCannotJumpOverFence () throws Exception {
jtulach@15
   331
        Board b = fence (board, 0, 'D', 8, Fence.Orientation.HORIZONTAL);
jtulach@21
   332
        assertEquals("One fence is present", 1, b.getFences().size());
jtulach@21
   333
        final Fence f = b.getFences().iterator().next();
jtulach@21
   334
        assertEquals("Row is 8", 8, f.getRow());
jtulach@21
   335
        assertEquals("Column is D", 'D', f.getColumn());
jtulach@0
   336
        try {
jtulach@15
   337
            move(b, 1, Player.Direction.SOUTH);
jtulach@0
   338
            fail ("This shall not be allowed, as there is the fence");
jtulach@0
   339
        } catch (IllegalPositionException ex) {
jtulach@0
   340
            // ok
jtulach@0
   341
        }
jtulach@0
   342
    }
jtulach@0
   343
    
jtulach@0
   344
jtulach@0
   345
    public void testSideJumpsNotAllowedWhenThereIsNoFence () throws Exception {
jtulach@0
   346
        Board b = board;
jtulach@0
   347
        
jtulach@0
   348
        for (int i = 0; i < 3; i++) {
jtulach@15
   349
            b = move(b, 0, Player.Direction.NORTH);
jtulach@15
   350
            b = move(b, 1, Player.Direction.SOUTH);
jtulach@0
   351
        }
jtulach@0
   352
        
jtulach@15
   353
        b = move(b, 0, Player.Direction.NORTH);
jtulach@0
   354
        
jtulach@0
   355
        try {
jtulach@15
   356
            b = move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
jtulach@0
   357
            fail ("Cannot just jump aside");
jtulach@0
   358
        } catch (IllegalPositionException ex) {
jtulach@0
   359
            // ok
jtulach@0
   360
        }
jtulach@0
   361
    }
jtulach@0
   362
    
jtulach@0
   363
    public void testSideJumpsAllowedWhenThereAFence () throws Exception {
jtulach@0
   364
        Board b = board;
jtulach@0
   365
        
jtulach@0
   366
        for (int i = 0; i < 3; i++) {
jtulach@15
   367
            b = move(b, 0, Player.Direction.NORTH);
jtulach@15
   368
            b = move(b, 1, Player.Direction.SOUTH);
jtulach@0
   369
        }
jtulach@0
   370
        
jtulach@15
   371
        b = move(b, 0, Player.Direction.NORTH);
jtulach@0
   372
        
jtulach@26
   373
        assertEquals (8, b.getPlayers ().get (0).getXInternal());
jtulach@26
   374
        assertEquals (8, b.getPlayers ().get (0).getYInternal());
jtulach@0
   375
        
jtulach@15
   376
        b = fence(b, 0, 'D', 4, Fence.Orientation.HORIZONTAL);
jtulach@0
   377
        
jtulach@0
   378
        // we can over jump to west
jtulach@15
   379
        move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
jtulach@0
   380
        // as well as east
jtulach@15
   381
        move(b, 1, Player.Direction.SOUTH, Player.Direction.EAST);
jtulach@0
   382
    }
jtulach@11
   383
jtulach@11
   384
    public void testPlaceAllFences() throws Exception {
jtulach@11
   385
        doPlaceAllFences(0);
jtulach@11
   386
    }
jtulach@11
   387
    public void testPlaceAllFences2() throws Exception {
jtulach@11
   388
        doPlaceAllFences(1);
jtulach@11
   389
    }
jtulach@11
   390
jtulach@11
   391
    private void doPlaceAllFences(int player) throws Exception {
jtulach@11
   392
        Board b = board;
jtulach@11
   393
jtulach@11
   394
        int cnt = 10;
jtulach@11
   395
        for (int i = 1; i <= 5; i++) {
jtulach@15
   396
            b = fence(b, player, 'A', i, Fence.Orientation.HORIZONTAL);
jtulach@15
   397
            b = fence(b, player, 'D', i, Fence.Orientation.HORIZONTAL);
jtulach@11
   398
            cnt -= 2;
jtulach@11
   399
            assertEquals("Two less" + i, cnt, b.getPlayers().get(player).getFences());
jtulach@11
   400
        }
jtulach@11
   401
jtulach@11
   402
        try {
jtulach@15
   403
            fence(b, player, 'F', 7, Fence.Orientation.VERTICAL);
jtulach@11
   404
            fail("We shall run out of fences");
jtulach@11
   405
        } catch (IllegalPositionException ex) {
jtulach@11
   406
            // OK
jtulach@11
   407
        }
jtulach@11
   408
    }
jtulach@0
   409
    
jtulach@0
   410
    public void testAlwaysHasToHaveAccessToEndLine () throws Exception {
jtulach@0
   411
        Board b = board;
jtulach@0
   412
        
jtulach@15
   413
        b = b.fence (b.getPlayers ().get (0), 'E', 1, Fence.Orientation.HORIZONTAL);
jtulach@15
   414
        b = b.fence (b.getPlayers ().get (0), 'F', 1, Fence.Orientation.VERTICAL);
jtulach@0
   415
jtulach@0
   416
        try {
jtulach@15
   417
            b = b.fence (b.getPlayers ().get (0), 'D', 1, Fence.Orientation.VERTICAL);
jtulach@0
   418
            fail ("This is not allowed as player 0 cannot now reach the final line");
jtulach@0
   419
        } catch (IllegalPositionException ex) {
jtulach@0
   420
            // ok
jtulach@0
   421
        }
jtulach@0
   422
        
jtulach@0
   423
    }
jtulach@0
   424
    
jtulach@0
   425
    public void testEqualsOfPlayers () throws Exception {
jtulach@15
   426
        Player p1 = new Player (1, 1, 10, Player.Direction.EAST);
jtulach@15
   427
        Player p2 = new Player (1, 1, 10, Player.Direction.EAST);
jtulach@15
   428
        Player p3 = new Player (2, 1, 10, Player.Direction.EAST);
jtulach@15
   429
        Player p4 = new Player (1, 1, 10, Player.Direction.WEST);
jtulach@15
   430
        Player p5 = new Player (1, 2, 10, Player.Direction.EAST);
jtulach@15
   431
        Player p6 = new Player (1, 1, 5, Player.Direction.EAST);
jtulach@0
   432
        
jtulach@0
   433
        assertEquals ("p1 == p2", p1, p2);
jtulach@0
   434
        if (p2.equals (p3)) fail ();
jtulach@0
   435
        if (p2.equals (p4)) fail ();
jtulach@0
   436
        if (p2.equals (p5)) fail ();
jtulach@0
   437
        if (p2.equals (p6)) fail ();
jtulach@0
   438
        if (p3.equals (p6)) fail ();
jtulach@0
   439
        if (p4.equals (p3)) fail ();
jtulach@0
   440
        if (p6.equals (p3)) fail ();
jtulach@0
   441
        if (p5.equals (p3)) fail ();
jtulach@0
   442
        if (p5.equals (p3)) fail ();
jtulach@0
   443
        if (p4.equals (p3)) fail ();
jtulach@0
   444
        if (p3.equals (p4)) fail ();
jtulach@0
   445
        if (p3.equals (p5)) fail ();
jtulach@0
   446
        if (p4.equals (p5)) fail ();
jtulach@0
   447
        if (p5.equals (p4)) fail ();
jtulach@0
   448
    }
jtulach@0
   449
    
jtulach@0
   450
    public void testEqualsOfFences () throws Exception {
jtulach@15
   451
        Fence f1 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
jtulach@15
   452
        Fence f2 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
jtulach@15
   453
        Fence f3 = new Fence (3, 1, Fence.Orientation.HORIZONTAL);
jtulach@15
   454
        Fence f4 = new Fence (1, 3, Fence.Orientation.HORIZONTAL);
jtulach@15
   455
        Fence f5 = new Fence (1, 1, Fence.Orientation.VERTICAL);
jtulach@0
   456
        
jtulach@0
   457
        assertEquals ("f1 == f2", f1, f2);
jtulach@0
   458
        if (f1.equals (f3)) fail ();
jtulach@0
   459
        if (f3.equals (f1)) fail ();
jtulach@0
   460
        if (f5.equals (f1)) fail ();
jtulach@0
   461
        if (f1.equals (f5)) fail ();
jtulach@0
   462
        if (f4.equals (f1)) fail ();
jtulach@0
   463
        if (f1.equals (f4)) fail ();
jtulach@0
   464
    }
jtulach@0
   465
    
jtulach@0
   466
    public void testEqualsOfBoards1 () throws Exception {
jtulach@15
   467
        Board b1 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
jtulach@15
   468
        Board b2 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
jtulach@15
   469
        Board b3 = board.move (board.getPlayers ().get (0), Player.Direction.EAST);
jtulach@0
   470
        
jtulach@0
   471
        if (b1.equals (b3)) fail ();
jtulach@0
   472
        if (b3.equals (b1)) fail ();
jtulach@0
   473
        
jtulach@0
   474
        assertEquals ("b1 == b2", b1, b2);
jtulach@0
   475
    }
jtulach@0
   476
    public void testEqualsOfBoards2 () throws Exception {
jtulach@15
   477
        Board b1 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
jtulach@15
   478
        Board b2 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
jtulach@15
   479
        Board b3 = board.fence (board.getPlayers ().get (0), 'D', 3, Fence.Orientation.HORIZONTAL);
jtulach@15
   480
        Board b4 = board.fence (board.getPlayers ().get (0), 'E', 4, Fence.Orientation.HORIZONTAL);
jtulach@15
   481
        Board b5 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.VERTICAL);
jtulach@0
   482
        
jtulach@0
   483
        if (b1.equals (b3)) fail ();
jtulach@0
   484
        if (b3.equals (b1)) fail ();
jtulach@0
   485
        if (b1.equals (b4)) fail ();
jtulach@0
   486
        if (b1.equals (b5)) fail ();
jtulach@0
   487
        if (b5.equals (b1)) fail ();
jtulach@0
   488
        if (b4.equals (b1)) fail ();
jtulach@0
   489
        
jtulach@0
   490
        assertEquals ("b1 == b2", b1, b2);
jtulach@0
   491
    }
jtulach@0
   492
    public void testEqualsOfBoardsWhenJumpOver () throws Exception {
jtulach@0
   493
        Board b = board;
jtulach@0
   494
        
jtulach@0
   495
        for (int i = 0; i < 3; i++) {
jtulach@15
   496
            b = move(b, 0, Player.Direction.NORTH);
jtulach@15
   497
            b = move(b, 1, Player.Direction.SOUTH);
jtulach@0
   498
        }
jtulach@0
   499
        
jtulach@15
   500
        Board b1 = move(b, 0, Player.Direction.NORTH);
jtulach@0
   501
        
jtulach@15
   502
        Board b2 = move(b, 1, Player.Direction.SOUTH);
jtulach@15
   503
        b2 = b2.move (b2.getPlayers ().get (0), Player.Direction.NORTH, Player.Direction.NORTH);
jtulach@0
   504
        
jtulach@0
   505
        if (b1.equals (b2)) fail ("Not the same, pawns are reverted");
jtulach@0
   506
        if (b2.equals (b1)) fail ("Not the same, pawns are reverted");
jtulach@0
   507
    }
jtulach@11
   508
jtulach@13
   509
    public void testMoveAltersCurrentPlayer() throws Exception {
jtulach@13
   510
        assertEquals("First player ready", board.getCurrentPlayer(), board.getPlayers().get(0));
jtulach@13
   511
        Board b = apply(board, Move.EAST);
jtulach@13
   512
jtulach@14
   513
        for (int i = 0; i < 7; i++) {
jtulach@13
   514
            assertEquals("Snd player ready", b.getCurrentPlayer(), b.getPlayers().get(1));
jtulach@13
   515
            b = apply(b, Move.SOUTH);
jtulach@13
   516
            assertEquals("First player ready", b.getCurrentPlayer(), b.getPlayers().get(0));
jtulach@13
   517
            b = apply(b, Move.NORTH);
jtulach@13
   518
        }
jtulach@14
   519
jtulach@14
   520
        Board fin = b.apply(Move.NORTH).apply(Move.NORTH);
jtulach@14
   521
        assertNotNull("There is a winner", fin.getWinner());
jtulach@14
   522
        assertEquals("And the winner is", fin.getPlayers().get(0), fin.getWinner());
jtulach@14
   523
jtulach@17
   524
        assertFalse("No next move can be applied", fin.isApplicable(Move.fence('D', 3, Fence.Orientation.HORIZONTAL)));
jtulach@17
   525
jtulach@14
   526
        try {
jtulach@14
   527
            fin.apply(Move.EAST);
jtulach@14
   528
            fail("No moves allow when we are in final position");
jtulach@14
   529
        } catch (IllegalPositionException ex) {
jtulach@14
   530
            // OK
jtulach@14
   531
        }
jtulach@14
   532
jtulach@13
   533
    }
jtulach@30
   534
jtulach@31
   535
    public void testDetectInvalidFence() {
jtulach@31
   536
        try {
jtulach@31
   537
            Move m = Move.fence('D', 9, Orientation.HORIZONTAL);
jtulach@31
   538
            fail("Move shall not be allowed: " + m);
jtulach@31
   539
        } catch (IllegalPositionException ex) {
jtulach@31
   540
            // OK
jtulach@31
   541
        }
jtulach@31
   542
    }
jtulach@31
   543
jtulach@30
   544
    public void testEqualityOfMoves() throws Exception {
jtulach@30
   545
jtulach@30
   546
        for (Direction m1 : Direction.values()) {
jtulach@30
   547
            for (Direction m2 : Direction.values()) {
jtulach@30
   548
                Move both1 = Move.jump(m1, m2);
jtulach@30
   549
                Move both2 = Move.jump(m1, m2);
jtulach@30
   550
                Move opposite = Move.jump(m2, m1);
jtulach@30
   551
jtulach@30
   552
                assertTrue("Both boths are equal", both1.equals(both2));
jtulach@30
   553
                assertFalse("Not north", Move.NORTH.equals(both1));
jtulach@30
   554
                assertFalse("Not east", Move.EAST.equals(both1));
jtulach@30
   555
                assertFalse("Not west", Move.WEST.equals(both1));
jtulach@30
   556
                assertFalse("Not south", Move.SOUTH.equals(both1));
jtulach@30
   557
                if (m1 == m2) {
jtulach@30
   558
                    continue;
jtulach@30
   559
                }
jtulach@30
   560
                assertFalse("Not equal to opposite", both1.equals(opposite));
jtulach@30
   561
            }
jtulach@30
   562
        }
jtulach@30
   563
jtulach@30
   564
        Move f1 = Move.fence('D', 6, Orientation.HORIZONTAL);
jtulach@30
   565
        Move f2 = Move.fence('D', 6, Orientation.HORIZONTAL);
jtulach@30
   566
        Move f3 = Move.fence('D', 6, Orientation.VERTICAL);
jtulach@30
   567
        Move f4 = Move.fence('E', 6, Orientation.VERTICAL);
jtulach@30
   568
        Move f5 = Move.fence('E', 5, Orientation.VERTICAL);
jtulach@30
   569
jtulach@30
   570
        assertEquals(f1, f2);
jtulach@30
   571
        assertFalse(f1.equals(f3));
jtulach@30
   572
        assertFalse(f4.equals(f5));
jtulach@30
   573
        assertFalse(f3.equals(f5));
jtulach@30
   574
    }
jaroslav@219
   575
jaroslav@219
   576
    public void testBrokenWriteOfAGameDanVsJarda() throws Exception {
jaroslav@219
   577
        Board b = board.apply(Move.NORTH).apply(Move.SOUTH).
jaroslav@219
   578
            apply(Move.NORTH).apply(Move.SOUTH).
jaroslav@219
   579
            apply(Move.NORTH).apply(Move.SOUTH).
jaroslav@219
   580
            apply(Move.WEST).apply(Move.fence('C', 6, Orientation.HORIZONTAL)).
jaroslav@219
   581
            apply(Move.EAST).apply(Move.SOUTH).
jaroslav@219
   582
            apply(Move.fence('E', 2, Orientation.HORIZONTAL)).apply(Move.fence('E', 5, Orientation.HORIZONTAL)).
jaroslav@219
   583
            apply(Move.fence('F', 3, Orientation.VERTICAL)).apply(Move.fence('D', 6, Orientation.VERTICAL)).
jaroslav@219
   584
            apply(Move.jump(Direction.NORTH, Direction.EAST)).apply(Move.fence('F', 5, Orientation.VERTICAL)).
jaroslav@219
   585
            apply(Move.fence('E', 1, Orientation.VERTICAL)).apply(Move.fence('E', 4, Orientation.VERTICAL)).
jaroslav@219
   586
            apply(Move.fence('D', 8, Orientation.HORIZONTAL)).apply(Move.fence('D', 7, Orientation.HORIZONTAL)).
jaroslav@219
   587
            apply(Move.fence('D', 4, Orientation.HORIZONTAL)).apply(Move.fence('E', 8, Orientation.VERTICAL)).
jaroslav@219
   588
            apply(Move.fence('C', 3, Orientation.HORIZONTAL)).apply(Move.WEST).
jaroslav@219
   589
            apply(Move.fence('D', 2, Orientation.VERTICAL)).apply(Move.fence('A', 4, Orientation.HORIZONTAL)).
jaroslav@219
   590
            apply(Move.fence('B', 2, Orientation.HORIZONTAL)).apply(Move.WEST).
jaroslav@219
   591
            apply(Move.SOUTH).apply(Move.SOUTH).
jaroslav@219
   592
            apply(Move.SOUTH).apply(Move.WEST);
jaroslav@219
   593
        Board m = move(b, 0, Direction.WEST);
jaroslav@219
   594
        Board f = fence(m, 1, 'A', 1, Orientation.VERTICAL);
jaroslav@219
   595
        Board l = fence(f, 0, 'A', 3, Orientation.VERTICAL);
jaroslav@219
   596
    }
jaroslav@245
   597
    
jaroslav@245
   598
    public void testTomasHolyCannotJumpWS() throws Exception {
jaroslav@245
   599
/*
jaroslav@245
   600
N   S 
jaroslav@245
   601
N   S 
jaroslav@245
   602
N   S 
jaroslav@245
   603
 */
jaroslav@245
   604
        Board b = apply(board, Move.NORTH);
jaroslav@245
   605
        b = apply(b, Move.SOUTH);
jaroslav@245
   606
        b = apply(b, Move.NORTH);
jaroslav@245
   607
        b = apply(b, Move.SOUTH);
jaroslav@245
   608
        b = apply(b, Move.NORTH);
jaroslav@245
   609
        b = apply(b, Move.SOUTH);
jaroslav@245
   610
/*        
jaroslav@245
   611
HD3   HE6 
jaroslav@245
   612
HB6   HG3 
jaroslav@245
   613
VE3   HD4 
jaroslav@245
   614
HA4   VB5 
jaroslav@245
   615
 */
jaroslav@245
   616
        b = fence(b, 0, 'D', 3, Orientation.HORIZONTAL);
jaroslav@245
   617
        b = fence(b, 1, 'E', 6, Orientation.HORIZONTAL);
jaroslav@245
   618
        b = fence(b, 0, 'B', 6, Orientation.HORIZONTAL);
jaroslav@245
   619
        b = fence(b, 1, 'G', 3, Orientation.HORIZONTAL);
jaroslav@245
   620
        b = fence(b, 0, 'E', 3, Orientation.VERTICAL);
jaroslav@245
   621
        b = fence(b, 1, 'D', 4, Orientation.HORIZONTAL);
jaroslav@245
   622
        b = fence(b, 0, 'A', 4, Orientation.HORIZONTAL);
jaroslav@245
   623
        b = fence(b, 1, 'B', 5, Orientation.VERTICAL);
jaroslav@245
   624
/*        
jaroslav@245
   625
VE1   HC5 
jaroslav@245
   626
VE5   HF2 
jaroslav@245
   627
VH2   HH1 
jaroslav@245
   628
HB3   HF1 
jaroslav@245
   629
HA1   S 
jaroslav@245
   630
 */
jaroslav@245
   631
        b = fence(b, 0, 'E', 1, Orientation.VERTICAL);
jaroslav@245
   632
        b = fence(b, 1, 'C', 5, Orientation.HORIZONTAL);
jaroslav@245
   633
        b = fence(b, 0, 'E', 5, Orientation.VERTICAL);
jaroslav@245
   634
        b = fence(b, 1, 'F', 2, Orientation.HORIZONTAL);
jaroslav@245
   635
        b = fence(b, 0, 'H', 2, Orientation.VERTICAL);
jaroslav@245
   636
        b = fence(b, 1, 'H', 1, Orientation.HORIZONTAL);
jaroslav@245
   637
        b = fence(b, 0, 'B', 3, Orientation.HORIZONTAL);
jaroslav@245
   638
        b = fence(b, 1, 'F', 1, Orientation.HORIZONTAL);
jaroslav@245
   639
        b = fence(b, 0, 'A', 1, Orientation.HORIZONTAL);
jaroslav@245
   640
        b = apply(b, Move.SOUTH);
jaroslav@245
   641
        
jaroslav@245
   642
/*
jaroslav@245
   643
W   W 
jaroslav@245
   644
W   HC8 
jaroslav@245
   645
HC1   HE8 
jaroslav@245
   646
N                
jaroslav@245
   647
*/
jaroslav@245
   648
        b = apply(b, Move.WEST);
jaroslav@245
   649
        b = apply(b, Move.WEST);
jaroslav@245
   650
        b = apply(b, Move.WEST);
jaroslav@245
   651
        b = fence(b, 1, 'C', 8, Orientation.HORIZONTAL);
jaroslav@245
   652
        b = fence(b, 0, 'C', 1, Orientation.HORIZONTAL);
jaroslav@245
   653
        b = fence(b, 1, 'E', 8, Orientation.HORIZONTAL);
jaroslav@245
   654
        b = apply(b, Move.NORTH);
jaroslav@245
   655
/* and now try WS */
jaroslav@245
   656
        b = move(b, 1, Direction.WEST, Direction.SOUTH);
jaroslav@245
   657
        assertNotNull("Board is OK", b);
jaroslav@253
   658
    }
jaroslav@253
   659
jaroslav@253
   660
    static String toPicture(Board b) throws IOException {
jaroslav@245
   661
        StringWriter sw = new StringWriter();
jaroslav@245
   662
        b.write(sw);
jaroslav@253
   663
        return sw.toString();
jaroslav@245
   664
    }
jaroslav@219
   665
jaroslav@179
   666
    static Board picture2board(String text) throws IOException, IllegalPositionException {
jaroslav@179
   667
        StringReader sr = new StringReader(text);
jaroslav@179
   668
        return Board.read(sr);
jaroslav@179
   669
    }
jtulach@0
   670
}