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