quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 17 Jan 2010 14:36:03 +0100
changeset 219 d836818f5554
parent 179 c5fbddc4c590
child 237 38db4aae19d9
permissions -rw-r--r--
Decoding of vertical walls on column A was broken
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
jtulach@0
   259
    public void testCannotJumpOverFence () throws Exception {
jtulach@15
   260
        Board b = fence (board, 0, 'D', 8, Fence.Orientation.HORIZONTAL);
jtulach@21
   261
        assertEquals("One fence is present", 1, b.getFences().size());
jtulach@21
   262
        final Fence f = b.getFences().iterator().next();
jtulach@21
   263
        assertEquals("Row is 8", 8, f.getRow());
jtulach@21
   264
        assertEquals("Column is D", 'D', f.getColumn());
jtulach@0
   265
        try {
jtulach@15
   266
            move(b, 1, Player.Direction.SOUTH);
jtulach@0
   267
            fail ("This shall not be allowed, as there is the fence");
jtulach@0
   268
        } catch (IllegalPositionException ex) {
jtulach@0
   269
            // ok
jtulach@0
   270
        }
jtulach@0
   271
    }
jtulach@0
   272
    
jtulach@0
   273
jtulach@0
   274
    public void testSideJumpsNotAllowedWhenThereIsNoFence () throws Exception {
jtulach@0
   275
        Board b = board;
jtulach@0
   276
        
jtulach@0
   277
        for (int i = 0; i < 3; i++) {
jtulach@15
   278
            b = move(b, 0, Player.Direction.NORTH);
jtulach@15
   279
            b = move(b, 1, Player.Direction.SOUTH);
jtulach@0
   280
        }
jtulach@0
   281
        
jtulach@15
   282
        b = move(b, 0, Player.Direction.NORTH);
jtulach@0
   283
        
jtulach@0
   284
        try {
jtulach@15
   285
            b = move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
jtulach@0
   286
            fail ("Cannot just jump aside");
jtulach@0
   287
        } catch (IllegalPositionException ex) {
jtulach@0
   288
            // ok
jtulach@0
   289
        }
jtulach@0
   290
    }
jtulach@0
   291
    
jtulach@0
   292
    public void testSideJumpsAllowedWhenThereAFence () throws Exception {
jtulach@0
   293
        Board b = board;
jtulach@0
   294
        
jtulach@0
   295
        for (int i = 0; i < 3; i++) {
jtulach@15
   296
            b = move(b, 0, Player.Direction.NORTH);
jtulach@15
   297
            b = move(b, 1, Player.Direction.SOUTH);
jtulach@0
   298
        }
jtulach@0
   299
        
jtulach@15
   300
        b = move(b, 0, Player.Direction.NORTH);
jtulach@0
   301
        
jtulach@26
   302
        assertEquals (8, b.getPlayers ().get (0).getXInternal());
jtulach@26
   303
        assertEquals (8, b.getPlayers ().get (0).getYInternal());
jtulach@0
   304
        
jtulach@15
   305
        b = fence(b, 0, 'D', 4, Fence.Orientation.HORIZONTAL);
jtulach@0
   306
        
jtulach@0
   307
        // we can over jump to west
jtulach@15
   308
        move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
jtulach@0
   309
        // as well as east
jtulach@15
   310
        move(b, 1, Player.Direction.SOUTH, Player.Direction.EAST);
jtulach@0
   311
    }
jtulach@11
   312
jtulach@11
   313
    public void testPlaceAllFences() throws Exception {
jtulach@11
   314
        doPlaceAllFences(0);
jtulach@11
   315
    }
jtulach@11
   316
    public void testPlaceAllFences2() throws Exception {
jtulach@11
   317
        doPlaceAllFences(1);
jtulach@11
   318
    }
jtulach@11
   319
jtulach@11
   320
    private void doPlaceAllFences(int player) throws Exception {
jtulach@11
   321
        Board b = board;
jtulach@11
   322
jtulach@11
   323
        int cnt = 10;
jtulach@11
   324
        for (int i = 1; i <= 5; i++) {
jtulach@15
   325
            b = fence(b, player, 'A', i, Fence.Orientation.HORIZONTAL);
jtulach@15
   326
            b = fence(b, player, 'D', i, Fence.Orientation.HORIZONTAL);
jtulach@11
   327
            cnt -= 2;
jtulach@11
   328
            assertEquals("Two less" + i, cnt, b.getPlayers().get(player).getFences());
jtulach@11
   329
        }
jtulach@11
   330
jtulach@11
   331
        try {
jtulach@15
   332
            fence(b, player, 'F', 7, Fence.Orientation.VERTICAL);
jtulach@11
   333
            fail("We shall run out of fences");
jtulach@11
   334
        } catch (IllegalPositionException ex) {
jtulach@11
   335
            // OK
jtulach@11
   336
        }
jtulach@11
   337
    }
jtulach@0
   338
    
jtulach@0
   339
    public void testAlwaysHasToHaveAccessToEndLine () throws Exception {
jtulach@0
   340
        Board b = board;
jtulach@0
   341
        
jtulach@15
   342
        b = b.fence (b.getPlayers ().get (0), 'E', 1, Fence.Orientation.HORIZONTAL);
jtulach@15
   343
        b = b.fence (b.getPlayers ().get (0), 'F', 1, Fence.Orientation.VERTICAL);
jtulach@0
   344
jtulach@0
   345
        try {
jtulach@15
   346
            b = b.fence (b.getPlayers ().get (0), 'D', 1, Fence.Orientation.VERTICAL);
jtulach@0
   347
            fail ("This is not allowed as player 0 cannot now reach the final line");
jtulach@0
   348
        } catch (IllegalPositionException ex) {
jtulach@0
   349
            // ok
jtulach@0
   350
        }
jtulach@0
   351
        
jtulach@0
   352
    }
jtulach@0
   353
    
jtulach@0
   354
    public void testEqualsOfPlayers () throws Exception {
jtulach@15
   355
        Player p1 = new Player (1, 1, 10, Player.Direction.EAST);
jtulach@15
   356
        Player p2 = new Player (1, 1, 10, Player.Direction.EAST);
jtulach@15
   357
        Player p3 = new Player (2, 1, 10, Player.Direction.EAST);
jtulach@15
   358
        Player p4 = new Player (1, 1, 10, Player.Direction.WEST);
jtulach@15
   359
        Player p5 = new Player (1, 2, 10, Player.Direction.EAST);
jtulach@15
   360
        Player p6 = new Player (1, 1, 5, Player.Direction.EAST);
jtulach@0
   361
        
jtulach@0
   362
        assertEquals ("p1 == p2", p1, p2);
jtulach@0
   363
        if (p2.equals (p3)) fail ();
jtulach@0
   364
        if (p2.equals (p4)) fail ();
jtulach@0
   365
        if (p2.equals (p5)) fail ();
jtulach@0
   366
        if (p2.equals (p6)) fail ();
jtulach@0
   367
        if (p3.equals (p6)) fail ();
jtulach@0
   368
        if (p4.equals (p3)) fail ();
jtulach@0
   369
        if (p6.equals (p3)) fail ();
jtulach@0
   370
        if (p5.equals (p3)) fail ();
jtulach@0
   371
        if (p5.equals (p3)) fail ();
jtulach@0
   372
        if (p4.equals (p3)) fail ();
jtulach@0
   373
        if (p3.equals (p4)) fail ();
jtulach@0
   374
        if (p3.equals (p5)) fail ();
jtulach@0
   375
        if (p4.equals (p5)) fail ();
jtulach@0
   376
        if (p5.equals (p4)) fail ();
jtulach@0
   377
    }
jtulach@0
   378
    
jtulach@0
   379
    public void testEqualsOfFences () throws Exception {
jtulach@15
   380
        Fence f1 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
jtulach@15
   381
        Fence f2 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
jtulach@15
   382
        Fence f3 = new Fence (3, 1, Fence.Orientation.HORIZONTAL);
jtulach@15
   383
        Fence f4 = new Fence (1, 3, Fence.Orientation.HORIZONTAL);
jtulach@15
   384
        Fence f5 = new Fence (1, 1, Fence.Orientation.VERTICAL);
jtulach@0
   385
        
jtulach@0
   386
        assertEquals ("f1 == f2", f1, f2);
jtulach@0
   387
        if (f1.equals (f3)) fail ();
jtulach@0
   388
        if (f3.equals (f1)) fail ();
jtulach@0
   389
        if (f5.equals (f1)) fail ();
jtulach@0
   390
        if (f1.equals (f5)) fail ();
jtulach@0
   391
        if (f4.equals (f1)) fail ();
jtulach@0
   392
        if (f1.equals (f4)) fail ();
jtulach@0
   393
    }
jtulach@0
   394
    
jtulach@0
   395
    public void testEqualsOfBoards1 () throws Exception {
jtulach@15
   396
        Board b1 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
jtulach@15
   397
        Board b2 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
jtulach@15
   398
        Board b3 = board.move (board.getPlayers ().get (0), Player.Direction.EAST);
jtulach@0
   399
        
jtulach@0
   400
        if (b1.equals (b3)) fail ();
jtulach@0
   401
        if (b3.equals (b1)) fail ();
jtulach@0
   402
        
jtulach@0
   403
        assertEquals ("b1 == b2", b1, b2);
jtulach@0
   404
    }
jtulach@0
   405
    public void testEqualsOfBoards2 () throws Exception {
jtulach@15
   406
        Board b1 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
jtulach@15
   407
        Board b2 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
jtulach@15
   408
        Board b3 = board.fence (board.getPlayers ().get (0), 'D', 3, Fence.Orientation.HORIZONTAL);
jtulach@15
   409
        Board b4 = board.fence (board.getPlayers ().get (0), 'E', 4, Fence.Orientation.HORIZONTAL);
jtulach@15
   410
        Board b5 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.VERTICAL);
jtulach@0
   411
        
jtulach@0
   412
        if (b1.equals (b3)) fail ();
jtulach@0
   413
        if (b3.equals (b1)) fail ();
jtulach@0
   414
        if (b1.equals (b4)) fail ();
jtulach@0
   415
        if (b1.equals (b5)) fail ();
jtulach@0
   416
        if (b5.equals (b1)) fail ();
jtulach@0
   417
        if (b4.equals (b1)) fail ();
jtulach@0
   418
        
jtulach@0
   419
        assertEquals ("b1 == b2", b1, b2);
jtulach@0
   420
    }
jtulach@0
   421
    public void testEqualsOfBoardsWhenJumpOver () throws Exception {
jtulach@0
   422
        Board b = board;
jtulach@0
   423
        
jtulach@0
   424
        for (int i = 0; i < 3; i++) {
jtulach@15
   425
            b = move(b, 0, Player.Direction.NORTH);
jtulach@15
   426
            b = move(b, 1, Player.Direction.SOUTH);
jtulach@0
   427
        }
jtulach@0
   428
        
jtulach@15
   429
        Board b1 = move(b, 0, Player.Direction.NORTH);
jtulach@0
   430
        
jtulach@15
   431
        Board b2 = move(b, 1, Player.Direction.SOUTH);
jtulach@15
   432
        b2 = b2.move (b2.getPlayers ().get (0), Player.Direction.NORTH, Player.Direction.NORTH);
jtulach@0
   433
        
jtulach@0
   434
        if (b1.equals (b2)) fail ("Not the same, pawns are reverted");
jtulach@0
   435
        if (b2.equals (b1)) fail ("Not the same, pawns are reverted");
jtulach@0
   436
    }
jtulach@11
   437
jtulach@13
   438
    public void testMoveAltersCurrentPlayer() throws Exception {
jtulach@13
   439
        assertEquals("First player ready", board.getCurrentPlayer(), board.getPlayers().get(0));
jtulach@13
   440
        Board b = apply(board, Move.EAST);
jtulach@13
   441
jtulach@14
   442
        for (int i = 0; i < 7; i++) {
jtulach@13
   443
            assertEquals("Snd player ready", b.getCurrentPlayer(), b.getPlayers().get(1));
jtulach@13
   444
            b = apply(b, Move.SOUTH);
jtulach@13
   445
            assertEquals("First player ready", b.getCurrentPlayer(), b.getPlayers().get(0));
jtulach@13
   446
            b = apply(b, Move.NORTH);
jtulach@13
   447
        }
jtulach@14
   448
jtulach@14
   449
        Board fin = b.apply(Move.NORTH).apply(Move.NORTH);
jtulach@14
   450
        assertNotNull("There is a winner", fin.getWinner());
jtulach@14
   451
        assertEquals("And the winner is", fin.getPlayers().get(0), fin.getWinner());
jtulach@14
   452
jtulach@17
   453
        assertFalse("No next move can be applied", fin.isApplicable(Move.fence('D', 3, Fence.Orientation.HORIZONTAL)));
jtulach@17
   454
jtulach@14
   455
        try {
jtulach@14
   456
            fin.apply(Move.EAST);
jtulach@14
   457
            fail("No moves allow when we are in final position");
jtulach@14
   458
        } catch (IllegalPositionException ex) {
jtulach@14
   459
            // OK
jtulach@14
   460
        }
jtulach@14
   461
jtulach@13
   462
    }
jtulach@30
   463
jtulach@31
   464
    public void testDetectInvalidFence() {
jtulach@31
   465
        try {
jtulach@31
   466
            Move m = Move.fence('D', 9, Orientation.HORIZONTAL);
jtulach@31
   467
            fail("Move shall not be allowed: " + m);
jtulach@31
   468
        } catch (IllegalPositionException ex) {
jtulach@31
   469
            // OK
jtulach@31
   470
        }
jtulach@31
   471
    }
jtulach@31
   472
jtulach@30
   473
    public void testEqualityOfMoves() throws Exception {
jtulach@30
   474
jtulach@30
   475
        for (Direction m1 : Direction.values()) {
jtulach@30
   476
            for (Direction m2 : Direction.values()) {
jtulach@30
   477
                Move both1 = Move.jump(m1, m2);
jtulach@30
   478
                Move both2 = Move.jump(m1, m2);
jtulach@30
   479
                Move opposite = Move.jump(m2, m1);
jtulach@30
   480
jtulach@30
   481
                assertTrue("Both boths are equal", both1.equals(both2));
jtulach@30
   482
                assertFalse("Not north", Move.NORTH.equals(both1));
jtulach@30
   483
                assertFalse("Not east", Move.EAST.equals(both1));
jtulach@30
   484
                assertFalse("Not west", Move.WEST.equals(both1));
jtulach@30
   485
                assertFalse("Not south", Move.SOUTH.equals(both1));
jtulach@30
   486
                if (m1 == m2) {
jtulach@30
   487
                    continue;
jtulach@30
   488
                }
jtulach@30
   489
                assertFalse("Not equal to opposite", both1.equals(opposite));
jtulach@30
   490
            }
jtulach@30
   491
        }
jtulach@30
   492
jtulach@30
   493
        Move f1 = Move.fence('D', 6, Orientation.HORIZONTAL);
jtulach@30
   494
        Move f2 = Move.fence('D', 6, Orientation.HORIZONTAL);
jtulach@30
   495
        Move f3 = Move.fence('D', 6, Orientation.VERTICAL);
jtulach@30
   496
        Move f4 = Move.fence('E', 6, Orientation.VERTICAL);
jtulach@30
   497
        Move f5 = Move.fence('E', 5, Orientation.VERTICAL);
jtulach@30
   498
jtulach@30
   499
        assertEquals(f1, f2);
jtulach@30
   500
        assertFalse(f1.equals(f3));
jtulach@30
   501
        assertFalse(f4.equals(f5));
jtulach@30
   502
        assertFalse(f3.equals(f5));
jtulach@30
   503
    }
jaroslav@219
   504
jaroslav@219
   505
    public void testBrokenWriteOfAGameDanVsJarda() throws Exception {
jaroslav@219
   506
        Board b = board.apply(Move.NORTH).apply(Move.SOUTH).
jaroslav@219
   507
            apply(Move.NORTH).apply(Move.SOUTH).
jaroslav@219
   508
            apply(Move.NORTH).apply(Move.SOUTH).
jaroslav@219
   509
            apply(Move.WEST).apply(Move.fence('C', 6, Orientation.HORIZONTAL)).
jaroslav@219
   510
            apply(Move.EAST).apply(Move.SOUTH).
jaroslav@219
   511
            apply(Move.fence('E', 2, Orientation.HORIZONTAL)).apply(Move.fence('E', 5, Orientation.HORIZONTAL)).
jaroslav@219
   512
            apply(Move.fence('F', 3, Orientation.VERTICAL)).apply(Move.fence('D', 6, Orientation.VERTICAL)).
jaroslav@219
   513
            apply(Move.jump(Direction.NORTH, Direction.EAST)).apply(Move.fence('F', 5, Orientation.VERTICAL)).
jaroslav@219
   514
            apply(Move.fence('E', 1, Orientation.VERTICAL)).apply(Move.fence('E', 4, Orientation.VERTICAL)).
jaroslav@219
   515
            apply(Move.fence('D', 8, Orientation.HORIZONTAL)).apply(Move.fence('D', 7, Orientation.HORIZONTAL)).
jaroslav@219
   516
            apply(Move.fence('D', 4, Orientation.HORIZONTAL)).apply(Move.fence('E', 8, Orientation.VERTICAL)).
jaroslav@219
   517
            apply(Move.fence('C', 3, Orientation.HORIZONTAL)).apply(Move.WEST).
jaroslav@219
   518
            apply(Move.fence('D', 2, Orientation.VERTICAL)).apply(Move.fence('A', 4, Orientation.HORIZONTAL)).
jaroslav@219
   519
            apply(Move.fence('B', 2, Orientation.HORIZONTAL)).apply(Move.WEST).
jaroslav@219
   520
            apply(Move.SOUTH).apply(Move.SOUTH).
jaroslav@219
   521
            apply(Move.SOUTH).apply(Move.WEST);
jaroslav@219
   522
        Board m = move(b, 0, Direction.WEST);
jaroslav@219
   523
        Board f = fence(m, 1, 'A', 1, Orientation.VERTICAL);
jaroslav@219
   524
        Board l = fence(f, 0, 'A', 3, Orientation.VERTICAL);
jaroslav@219
   525
    }
jaroslav@219
   526
jaroslav@179
   527
    static Board picture2board(String text) throws IOException, IllegalPositionException {
jaroslav@179
   528
        StringReader sr = new StringReader(text);
jaroslav@179
   529
        return Board.read(sr);
jaroslav@179
   530
    }
jtulach@0
   531
}