quoridor/src/main/java/cz/xelfi/quoridor/Board.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Jan 2010 22:51:17 +0100
branchstatistics-and-elo
changeset 179 c5fbddc4c590
parent 178 4b78d4f028b3
child 219 d836818f5554
permissions -rw-r--r--
Renaming Board.board2HashCode to getCode()
jtulach@0
     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@0
    25
 */
jtulach@0
    26
jtulach@0
    27
package cz.xelfi.quoridor;
jtulach@0
    28
jtulach@15
    29
import cz.xelfi.quoridor.Player.Direction;
jtulach@10
    30
import java.io.BufferedReader;
jtulach@10
    31
import java.io.EOFException;
jtulach@9
    32
import java.io.IOException;
jtulach@9
    33
import java.io.Reader;
jaroslav@48
    34
import java.io.StringReader;
jtulach@9
    35
import java.io.Writer;
jtulach@10
    36
import java.util.ArrayList;
jtulach@3
    37
import java.util.Arrays;
jtulach@3
    38
import java.util.BitSet;
jtulach@3
    39
import java.util.Collection;
jtulach@3
    40
import java.util.Collections;
jtulach@3
    41
import java.util.HashSet;
jtulach@3
    42
import java.util.List;
jtulach@3
    43
import java.util.Set;
jaroslav@178
    44
import java.util.TreeSet;
jtulach@10
    45
import java.util.regex.Matcher;
jtulach@10
    46
import java.util.regex.Pattern;
jtulach@3
    47
jtulach@0
    48
/**
jtulach@16
    49
 * Represents a snapshot of the game position,
jtulach@16
    50
 * including all the placed and not-yet placed fences and player positions.
jtulach@16
    51
 * It it can print itself to stream in
jtulach@16
    52
 * <a href="http://www.gamerz.net/pbmserv/quoridor.html">ascii art format<a/>
jtulach@16
    53
 * and can it read back. The class is immutable
jtulach@16
    54
 * but it contains {@link #apply(cz.xelfi.quoridor.Move)}
jtulach@16
    55
 * that produce new {@link Board} with position created after
jtulach@16
    56
 * applying some {@link Move}. Use:
jtulach@16
    57
 * <pre>
jtulach@16
    58
 * Board whiteOnTurn = Board.empty();
jtulach@16
    59
 * Board blackOnTurn = whiteOnTurn.apply(Move.NORTH);
jtulach@16
    60
 * Board whiteAgain = blackOnTurn.apply(Move.SOUTH);
jtulach@16
    61
 * Board withOneFence = whiteAgain.apply(Move.fence('D', 7));
jtulach@16
    62
 * </pre>
jtulach@0
    63
 * 
jtulach@0
    64
 * @author Jaroslav Tulach
jtulach@0
    65
 */
jtulach@0
    66
public final class Board {
jtulach@75
    67
    /** winner, if any */
jtulach@75
    68
    private final Player winner;
jtulach@0
    69
    /** players */
jtulach@3
    70
    private final List<Player> players;
jtulach@0
    71
    /** fences placed on board */
jtulach@3
    72
    private final Set<Fence> fences;
jtulach@0
    73
    /** occurpied bits (coordinates encoded by toIndex methods) 
jtulach@0
    74
                         [N]
jtulach@0
    75
               
jtulach@0
    76
        +-----------------------------------+
jtulach@0
    77
     6  |                                   |          
jtulach@0
    78
     5  |   +   +   +   +   +   +   +   +   |  
jtulach@0
    79
     4  |                                   |          
jtulach@0
    80
     3  |   +   +   +   +   +   +   +   +   |  
jtulach@0
    81
     2  |                                   |          
jtulach@0
    82
     1  |   +   +   +   +   +   +   +   +   |  
jtulach@0
    83
     0  |                                   |
jtulach@0
    84
     9  |   +   +   +   +   +   +   +   +   |
jtulach@0
    85
[W]  8  |                                   |     [E]
jtulach@0
    86
     7  |   +   +   +   +   +   +   +   +   |
jtulach@0
    87
     6  |                                   |
jtulach@0
    88
     5  |   +   +   +   +   +   +   +   +   |
jtulach@0
    89
     4  |                                   |
jtulach@0
    90
     3  |   +   +   +   +   +   +   +   +   |
jtulach@0
    91
     2  |                                   |
jtulach@0
    92
     1  |   +   +   +   +   +   +   +   +   |
jtulach@0
    93
     0  |                                   |
jtulach@0
    94
        +-----------------------------------+
jtulach@0
    95
          0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
jtulach@0
    96
                         [S]
jtulach@0
    97
     
jtulach@0
    98
     * even indexes == position of the pawns
jtulach@0
    99
     * odd indexes  == centers of fences
jtulach@0
   100
     * even x odd   == side of a fence
jtulach@0
   101
     * odd x even   == side of a fence
jtulach@0
   102
     */
jtulach@3
   103
    private final BitSet occupied;
jtulach@3
   104
    /** which player's turn is next? */
jtulach@3
   105
    private final int turn;
jtulach@0
   106
    
jtulach@0
   107
    /**
jtulach@0
   108
     * Creates a new instance of Board 
jtulach@0
   109
     */
jtulach@0
   110
    private Board (int x1, int y1, int f1, int x2, int y2, int f2) {
jtulach@3
   111
        this.players = Collections.unmodifiableList (Arrays.asList (new Player[] {
jtulach@0
   112
            new Player (x1, y1, f1, Player.Direction.NORTH),
jtulach@0
   113
            new Player (x2, y2, f2, Player.Direction.SOUTH),
jtulach@0
   114
        }));
jtulach@3
   115
        this.fences = Collections.emptySet ();
jtulach@0
   116
        try {
jtulach@0
   117
            this.occupied = computeOccupied (players, fences);
jtulach@0
   118
        } catch (IllegalPositionException ex) {
jtulach@0
   119
            throw new IllegalStateException (ex.getMessage ());
jtulach@0
   120
        }
jtulach@3
   121
        this.turn = 0;
jtulach@75
   122
        this.winner = null;
jtulach@0
   123
    }
jtulach@0
   124
    
jtulach@0
   125
    /** Copy constructor that provides players and fences.
jtulach@0
   126
     */
jtulach@3
   127
    private Board (int turn, List<Player> players, Set<Fence> fences)
jtulach@0
   128
    throws IllegalPositionException {
jtulach@3
   129
        this.players = Collections.unmodifiableList (players);
jtulach@3
   130
        this.fences = Collections.unmodifiableSet (fences);
jtulach@0
   131
        this.occupied = computeOccupied (players, fences);
jaroslav@92
   132
jtulach@0
   133
        for (Player p : players) {
jaroslav@92
   134
            BitSet bs = new BitSet(17 * 17);
jaroslav@92
   135
            if (!accessibleFinalLine (p, p.endDirection, occupied, bs)) {
jtulach@0
   136
                throw new IllegalPositionException ("Player " + p + " cannot reach " + p.endDirection + " side"); // NOI18N
jtulach@0
   137
            }
jtulach@0
   138
        }
jtulach@3
   139
        this.turn = turn % players.size();
jtulach@75
   140
        this.winner = null;
jtulach@75
   141
    }
jtulach@75
   142
jtulach@75
   143
    /** Copy constructor for resigning the game */
jtulach@75
   144
    private Board(Board previous, int winner) {
jtulach@75
   145
        this.players = previous.players;
jtulach@75
   146
        this.turn = winner % players.size();
jtulach@75
   147
        this.fences = previous.fences;
jtulach@75
   148
        this.occupied = previous.occupied;
jtulach@75
   149
        this.winner = players.get(this.turn);
jtulach@0
   150
    }
jtulach@0
   151
    
jtulach@16
   152
    /** Returns empty board with default starting position.
jtulach@16
   153
     * @return board with two pawns.
jtulach@0
   154
     */
jtulach@16
   155
    public static Board empty () {
jtulach@0
   156
        return new Board (8, 0, 10, 8, 16, 10);
jtulach@0
   157
    }
jtulach@0
   158
    
jtulach@0
   159
    /** Returns players in the game.
jtulach@0
   160
     * @return player object
jtulach@0
   161
     */
jtulach@3
   162
    public List<Player> getPlayers () {
jtulach@0
   163
        return players;
jtulach@0
   164
    }
jtulach@0
   165
    
jtulach@0
   166
    /** The fences currently on board. 
jtulach@0
   167
     * 
jtulach@0
   168
     * @return immutable set of Fences
jtulach@0
   169
     */
jtulach@3
   170
    public Set<Fence> getFences () {
jtulach@0
   171
        return fences;
jtulach@0
   172
    }
jtulach@3
   173
jtulach@3
   174
    /** The player that is supposed to play now.
jtulach@75
   175
     * @return the player to do next move, null if the game is over
jtulach@3
   176
     */
jtulach@3
   177
    public Player getCurrentPlayer() {
jtulach@75
   178
        if (getWinner() != null) {
jtulach@75
   179
            return null;
jtulach@75
   180
        }
jtulach@3
   181
        return players.get(turn);
jtulach@3
   182
    }
jtulach@14
   183
jtulach@14
   184
    /** The play who wins on current board.
jtulach@14
   185
     *
jtulach@14
   186
     * @return the winning player or <code>null</code>
jtulach@14
   187
     */
jtulach@14
   188
    public Player getWinner() {
jtulach@75
   189
        if (winner != null) {
jtulach@75
   190
            return winner;
jtulach@75
   191
        }
jtulach@14
   192
        for (Player p : players) {
jtulach@14
   193
            if (p.endDirection.reached(p)) {
jtulach@14
   194
                return p;
jtulach@14
   195
            }
jtulach@14
   196
        }
jtulach@14
   197
        return null;
jtulach@14
   198
    }
jtulach@16
   199
jtulach@16
   200
    /** Applies given move to current board.
jtulach@16
   201
     *
jtulach@16
   202
     * @param move move creates by one of the factory methods or fields of the {@link Move} class
jtulach@16
   203
     * @return new board derived from this one
jtulach@16
   204
     *
jtulach@16
   205
     * @throws cz.xelfi.quoridor.IllegalPositionException throws exception if the move is illegal
jtulach@16
   206
     */
jtulach@3
   207
    public Board apply(Move move) throws IllegalPositionException {
jtulach@14
   208
        if (getWinner() != null) {
jtulach@14
   209
            throw new IllegalPositionException("Game finished!"); // NOI18N
jtulach@14
   210
        }
jtulach@14
   211
jtulach@3
   212
        if (move.direction != null) {
jtulach@3
   213
            return move(getCurrentPlayer(), move.direction);
jtulach@3
   214
        } else {
jtulach@75
   215
            if (move.fence != null) {
jtulach@75
   216
                return fence(getCurrentPlayer(), move.fence);
jtulach@75
   217
            } else {
jtulach@75
   218
                return new Board(this, turn + 1);
jtulach@75
   219
            }
jtulach@3
   220
        }
jtulach@3
   221
    }
jtulach@0
   222
jtulach@17
   223
    /** Can the move be applied to current board position?
jtulach@17
   224
     * 
jtulach@17
   225
     * @param move the move to apply
jtulach@17
   226
     * @return true if one can call {@link #apply} method without getting 
jtulach@17
   227
     *   an exception
jtulach@17
   228
     */
jtulach@17
   229
    public boolean isApplicable(Move move) {
jtulach@17
   230
        try {
jtulach@17
   231
            // trivial implementation is enough for now
jtulach@17
   232
            apply(move);
jtulach@17
   233
            return true;
jtulach@17
   234
        } catch (IllegalPositionException ex) {
jtulach@17
   235
            return false;
jtulach@17
   236
        }
jtulach@17
   237
    }
jtulach@17
   238
    
jtulach@0
   239
    /** Moves the player in given direction. The direction usually
jtulach@0
   240
     * is one of Player.Direction constants, but in case the move
jtulach@0
   241
     * is a jump over another players pawn it can be followed by
jtulach@0
   242
     * another (usually, if there is no fence the same) direction.
jtulach@0
   243
     *
jtulach@0
   244
     * @param player the player to move
jtulach@0
   245
     * @param where one or two directions saying where
jtulach@0
   246
     * @return the new board
jtulach@0
   247
     * @exception IllegalPositionException if the move is not possible
jtulach@0
   248
     */
jtulach@3
   249
    final Board move (Player player, Player.Direction... where) throws IllegalPositionException {
jtulach@0
   250
        if (where.length != 1 && where.length != 2) {
jtulach@0
   251
            throw new IllegalPositionException ("Move over one or two Directions"); // NOI18N
jtulach@0
   252
        }
jtulach@0
   253
        
jtulach@0
   254
        int index = players.indexOf (player);
jtulach@0
   255
        Player[] arr = players.toArray (new Player[0]);
jtulach@0
   256
jtulach@0
   257
        Player oneStep = newPosition (player, where[0]);
jtulach@0
   258
        
jtulach@0
   259
        if (where.length == 1) {
jtulach@0
   260
            arr[index] = oneStep;
jtulach@3
   261
            return new Board(turn + 1, Arrays.asList (arr), fences);
jtulach@0
   262
        }
jtulach@0
   263
jtulach@0
   264
        // straight jump over
jtulach@0
   265
        for (Player p : players) {
jtulach@26
   266
            if (p.getXInternal () == oneStep.getXInternal () && p.getYInternal() == oneStep.getYInternal ()) {
jtulach@0
   267
                // ok, we are jumping over this one
jtulach@0
   268
                GO_ON: if (where[0] != where[1]) {
jtulach@0
   269
                    // first of all ensure that we cannot go straight
jtulach@0
   270
                    try {
jtulach@0
   271
                        newPosition (oneStep, where[0]);
jtulach@0
   272
                    } catch (IllegalPositionException ex) {
jtulach@0
   273
                        // ok
jtulach@0
   274
                        break GO_ON;
jtulach@0
   275
                    }
jtulach@0
   276
                    throw new IllegalPositionException ("You have to jump straight if there is no wall"); // NOI18N
jtulach@0
   277
                }
jtulach@0
   278
                arr[index] = newPosition (oneStep, where[1]);
jtulach@3
   279
                return new Board (turn + 1, Arrays.asList (arr), fences);
jtulach@0
   280
            }
jtulach@0
   281
        }
jtulach@0
   282
        throw new IllegalPositionException ("Cannot use multi direction when there is not oponent pawn"); // NOI18N
jtulach@0
   283
    }
jtulach@0
   284
    
jtulach@3
   285
    final Board fence (Player player, char x, int y, Fence.Orientation orientation) throws IllegalPositionException {
jtulach@3
   286
        return fence(player, new Fence ((x - 'A') * 2 + 1, y * 2 - 1, orientation));
jtulach@3
   287
    }
jtulach@3
   288
jtulach@40
   289
    private void columnLine(int width, int spaceX, Writer w) throws IOException {
jtulach@40
   290
        char ch = 'A';
jtulach@40
   291
        for (int x = 0; x < width - 1; x++) {
jtulach@40
   292
            if (x % (spaceX + 1) == 0 && x > 0) {
jtulach@40
   293
                w.write(ch);
jtulach@40
   294
                ch = (char) (ch + 1);
jtulach@40
   295
            } else {
jtulach@40
   296
                w.write(' ');
jtulach@40
   297
            }
jtulach@40
   298
        }
jtulach@40
   299
    }
jtulach@40
   300
jtulach@3
   301
    private Board fence(Player player, Fence fence) throws IllegalPositionException {
jtulach@0
   302
        if (player.getFences () == 0) {
jtulach@0
   303
            throw new IllegalPositionException ("Not enough fences: " + player); // NOI18N
jtulach@0
   304
        }
jtulach@107
   305
jtulach@0
   306
        int index = players.indexOf (player);
jtulach@0
   307
        Player[] arr = players.toArray (new Player[0]);
jtulach@26
   308
        arr[index] = new Player (arr[index].getXInternal(), arr[index].getYInternal(), arr[index].getFences() - 1, arr[index].endDirection);
jtulach@0
   309
        
jtulach@3
   310
        HashSet<Fence> fen = new HashSet<Fence> (this.fences);
jtulach@107
   311
        if (!fen.add (fence)) {
jtulach@107
   312
            throw new IllegalPositionException ("Fence already prsent: " + fence); // NOI18N
jtulach@107
   313
        }
jtulach@0
   314
        
jtulach@3
   315
        return new Board (turn + 1, Arrays.asList (arr), fen);
jtulach@0
   316
    }
jtulach@9
   317
jtulach@9
   318
    //
jtulach@9
   319
    // Serialization
jtulach@9
   320
    //
jtulach@9
   321
jtulach@13
   322
    private static final Pattern northSouthPattern = Pattern.compile("(\\+(\\|*)(\\*)?-+\\+)");
jaroslav@48
   323
jaroslav@48
   324
    /** Reads the board from a reader. Opposite operation to {@link #write(java.io.Writer)}.
jaroslav@48
   325
     *
jaroslav@48
   326
     * @param r the reader
jaroslav@48
   327
     * @return the read board
jaroslav@48
   328
     * @throws IOException if I/O error occurs
jaroslav@48
   329
     * @throws IllegalPositionException if the reader does not contain description
jaroslav@48
   330
     *   of the board
jaroslav@48
   331
     */
jtulach@10
   332
    public static Board read(Reader r) throws IOException, IllegalPositionException {
jtulach@10
   333
        BufferedReader b = new BufferedReader(r);
jtulach@10
   334
        for (;;) {
jtulach@10
   335
            String s = b.readLine();
jtulach@10
   336
            if (s == null) {
jtulach@10
   337
                throw new IOException("No board found!");
jtulach@10
   338
            }
jtulach@11
   339
            Matcher m = northSouthPattern.matcher(s);
jtulach@10
   340
            if (m.find()) {
jtulach@13
   341
                return readFromBetween(b, m);
jtulach@10
   342
            }
jtulach@10
   343
        }
jtulach@10
   344
    }
jtulach@10
   345
jaroslav@48
   346
    /** Translates the string into board, if possible. String created by
jaroslav@48
   347
     * use of {@link #toString()} is accepted, more information about the
jaroslav@48
   348
     * format is avaliable in the description of {@link #write(java.io.Writer)}
jaroslav@48
   349
     * method.
jaroslav@48
   350
     *
jaroslav@48
   351
     * @param board string to analyze
jaroslav@48
   352
     * @return board object, if the string can be read
jaroslav@48
   353
     * @throws IllegalPositionException if the string does not represent the board
jaroslav@48
   354
     */
jaroslav@178
   355
    public static Board valueOf(String board) {
jaroslav@178
   356
        return new Board(board);
jaroslav@178
   357
    }
jaroslav@178
   358
jtulach@12
   359
    private static int assertChar(String s, int pos, char... ch) throws IOException {
jtulach@12
   360
        if (s.length() >= pos) {
jtulach@12
   361
            for (int i = 0; i < ch.length; i++) {
jtulach@12
   362
                if (ch[i] == s.charAt(pos)) {
jtulach@12
   363
                    return i;
jtulach@12
   364
                }
jtulach@12
   365
            }
jtulach@10
   366
        }
jtulach@12
   367
        throw new IOException("Not found " + ch[0] + " at " + pos + " in" + s);
jtulach@10
   368
    }
jtulach@10
   369
jtulach@13
   370
    private static Player findPlayer(
jtulach@13
   371
        Player previous, String line, int y, int spaceX, Player.Direction dir, int fences
jtulach@10
   372
    ) {
jtulach@13
   373
        int index = line.indexOf(dir.player);
jtulach@10
   374
        if (index == -1) {
jtulach@13
   375
            return previous;
jtulach@10
   376
        }
jtulach@10
   377
        int x = (index - 1) / (spaceX + 1) * 2;
jtulach@13
   378
        return new Player(x, y - 1, fences, dir);
jtulach@10
   379
    }
jtulach@10
   380
jtulach@13
   381
    private static Board readFromBetween(BufferedReader b, Matcher firstMatcher)
jtulach@10
   382
    throws IOException, IllegalPositionException {
jtulach@13
   383
        final int from = firstMatcher.start(1);
jtulach@13
   384
        final int to = firstMatcher.end(1);
jtulach@13
   385
        final int northFences = firstMatcher.end(2) - firstMatcher.start(2);
jtulach@10
   386
        final int spaceX = (to - from - 1) / 9 - 1;
jtulach@10
   387
        final int spaceY = 1;
jtulach@10
   388
jtulach@13
   389
        Player p = null;
jtulach@13
   390
        Player q = null;
jtulach@12
   391
        Set<Fence> fences = new HashSet<Fence>();
jtulach@10
   392
jtulach@9
   393
        StringBuffer sb = new StringBuffer();
jtulach@40
   394
        int row = 7;
jtulach@40
   395
        for (int y = (spaceY + 1) * 9 - 1; y > 0; y--) {
jtulach@10
   396
            String s = b.readLine();
jtulach@10
   397
            if (s == null) {
jtulach@10
   398
                throw new EOFException();
jtulach@9
   399
            }
jtulach@10
   400
            sb.append(s);
jtulach@10
   401
            sb.append('\n');
jtulach@10
   402
            if (s.length() < to) {
jtulach@10
   403
                throw new IOException("Too short line: " + s); // NOI18N
jtulach@10
   404
            }
jtulach@10
   405
            assertChar(s, from, '|');
jtulach@10
   406
            assertChar(s, to - 1, '|');
jtulach@10
   407
jtulach@10
   408
            if (y % (spaceY + 1) == 0) {
jtulach@10
   409
                for (int x = 1; x < 9; x++) {
jtulach@12
   410
                    switch (assertChar(s, from + (spaceX + 1) * x, '+', '-', '|')) {
jtulach@12
   411
                        case 1:
jtulach@12
   412
                            fences.add(new Fence(x * 2 - 1, row * 2 + 1, Fence.Orientation.HORIZONTAL));
jtulach@12
   413
                            break;
jtulach@12
   414
                        case 2:
jtulach@12
   415
                            fences.add(new Fence(x * 2 - 1, row * 2 + 1, Fence.Orientation.VERTICAL));
jtulach@12
   416
                            break;
jtulach@12
   417
                        case 0:
jtulach@12
   418
                            break;
jtulach@12
   419
                        default:
jtulach@12
   420
                            assert false;
jtulach@12
   421
                    }
jtulach@10
   422
                }
jtulach@40
   423
                row--;
jtulach@10
   424
            } else {
jtulach@10
   425
                String line = s.substring(from, to);
jtulach@13
   426
                p = findPlayer(p, line, y, spaceX, Player.Direction.NORTH, -1);
jtulach@13
   427
                q = findPlayer(q, line, y, spaceX, Player.Direction.SOUTH, northFences);
jtulach@10
   428
            }
jtulach@9
   429
        }
jtulach@10
   430
jtulach@11
   431
        String last = b.readLine();
jtulach@11
   432
        if (last == null) {
jtulach@11
   433
            throw new EOFException();
jtulach@11
   434
        }
jtulach@13
   435
        Matcher lastMatcher = northSouthPattern.matcher(last);
jtulach@13
   436
        if (!lastMatcher.find()) {
jtulach@11
   437
            throw new IOException("Unrecognized last line: " + last);
jtulach@11
   438
        }
jtulach@11
   439
jtulach@13
   440
        List<Player> arr = new ArrayList<Player>(2);
jtulach@13
   441
        assert p != null;
jtulach@13
   442
        int southFences = lastMatcher.end(2) - lastMatcher.start(2);
jtulach@26
   443
        arr.add(new Player(p.getXInternal(), p.getYInternal(), southFences, p.endDirection));
jtulach@13
   444
        arr.add(q);
jtulach@13
   445
        int turn = "*".equals(lastMatcher.group(3)) ? 0 : 1; // NOI18N
jtulach@13
   446
        return new Board(turn, arr, fences);
jtulach@9
   447
    }
jtulach@9
   448
jtulach@9
   449
jtulach@0
   450
    
jtulach@40
   451
    /** Writes the board to the provided writer. <b>P</b> denotes position
jtulach@40
   452
     * of the first player and <b>Q</b> of the second. Number of remaining
jtulach@40
   453
     * fences of each player are shown in left bottom and left top corner
jtulach@40
   454
     * as appropriate number of <b>|||</b> - one <b>|</b> per fence. The
jtulach@40
   455
     * player that is supposed to move in this turn has <b>*</b> right after
jtulach@40
   456
     * the set of its fences.
jtulach@0
   457
     *
jtulach@9
   458
     * <pre>
jtulach@40
   459
                         [N]
jtulach@40
   460
jtulach@40
   461
            A   B   C   D   E   F   G   H
jtulach@0
   462
            |   |   |   |   |   |   |   |
jtulach@11
   463
        +|||||------------------------------+
jtulach@40
   464
        |                 Q                 |
jtulach@40
   465
     8--|   +   +   +   +   +   +   +   +   |--8
jtulach@11
   466
        |       |                           |           
jtulach@40
   467
     7--|   +   |   +   +-------+-------+   |--7
jtulach@11
   468
        |       |       |                   |           
jtulach@40
   469
     6--|-------+-------|-------+-------+   |--6
jtulach@0
   470
        |               |                   |          
jtulach@40
   471
     5--|   +   +   +   +   +   +   +   +   |--5
jtulach@40
   472
[W]     |               |                   |     [E]
jtulach@40
   473
     4--|   +   +   +   |   +   +   +-------|--4
jtulach@40
   474
        |               |                   |
jtulach@40
   475
     3--|   +   +   +   +-------+   +   +   |--3
jtulach@40
   476
        |                                   |
jtulach@40
   477
     2--|   +   +   +   +   +   +   +   +   |--2
jtulach@40
   478
        |                       |           |
jtulach@40
   479
     1--|   +   +   +   +   +   |   +   +   |--1
jtulach@40
   480
        |                 P     |           |
jtulach@40
   481
        +|||*-------------------------------+
jtulach@0
   482
            |   |   |   |   |   |   |   |
jtulach@40
   483
            A   B   C   D   E   F   G   H
jtulach@40
   484
jtulach@40
   485
                         [S]
jtulach@9
   486
     </pre>
jtulach@0
   487
     * @param w writer to write the board to
jtulach@0
   488
     * @exception IOException if communiction with writer fails
jtulach@0
   489
     */
jtulach@9
   490
    public void write (Writer w) throws IOException {
jtulach@9
   491
        write(w, 3, 1);
jtulach@0
   492
    }
jtulach@40
   493
jtulach@40
   494
    private void northSouthSign(int width, char sign, Writer w) throws IOException {
jtulach@40
   495
        int middle = width / 2;
jtulach@40
   496
        for (int x = 0; x < width; x++) {
jtulach@40
   497
            char ch = ' ';
jtulach@40
   498
            if (x == middle - 1) {
jtulach@40
   499
                ch = '[';
jtulach@40
   500
            }
jtulach@40
   501
            if (x == middle) {
jtulach@40
   502
                ch = sign;
jtulach@40
   503
            }
jtulach@40
   504
            if (x == middle + 1) {
jtulach@40
   505
                ch = ']';
jtulach@40
   506
            }
jtulach@40
   507
            w.write(ch);
jtulach@40
   508
        }
jtulach@40
   509
        w.write(System.getProperty("line.separator")); // NOI18N
jtulach@40
   510
    }
jtulach@40
   511
jtulach@40
   512
    private void subColumnLine(int width, int spaceX, Writer w) throws IOException {
jtulach@40
   513
        for (int x = 0; x < width - 1; x++) {
jtulach@40
   514
            if (x % (spaceX + 1) == 0 && x > 0) {
jtulach@40
   515
                w.write('|');
jtulach@40
   516
            } else {
jtulach@40
   517
                w.write(' ');
jtulach@40
   518
            }
jtulach@40
   519
        }
jtulach@40
   520
    }
jtulach@0
   521
    
jtulach@0
   522
    /** This will print the board with provided spacing.
jtulach@0
   523
     * This is example of 3:1 spacing: 
jtulach@0
   524
     * <pre>
jtulach@0
   525
     *  +---+
jtulach@0
   526
     *  |sss|
jtulach@0
   527
     *  +---+
jtulach@0
   528
     * </pre>
jtulach@0
   529
     * and this 4:2 spacing:
jtulach@0
   530
     * <pre>
jtulach@0
   531
     *  +----+
jtulach@0
   532
     *  |ssss|
jtulach@0
   533
     *  |ssss|
jtulach@0
   534
     *  +----+
jtulach@0
   535
     * </pre>
jtulach@0
   536
     */
jtulach@9
   537
    private void write (Writer w, int spaceX, int spaceY) throws IOException {
jtulach@9
   538
        int width = spaceX * 9 + 10;
jtulach@9
   539
        int height = spaceY * 9 + 10;
jtulach@9
   540
        char[][] desk = new char[height][];
jtulach@9
   541
        for (int i = 0; i < height; i++) {
jtulach@9
   542
            desk[i] = new char[width];
jtulach@9
   543
            for (int j = 0; j < width; j++) {
jtulach@9
   544
                if (i % (spaceY + 1) == 0 && j % (spaceX + 1) == 0) {
jtulach@9
   545
                    desk[i][j] = '+';
jtulach@9
   546
                } else {
jtulach@9
   547
                    desk[i][j] = ' ';
jtulach@9
   548
                }
jtulach@0
   549
            }
jtulach@9
   550
            desk[i][0] = '|';
jtulach@9
   551
            desk[i][width - 1] = '|';
jtulach@0
   552
        }
jtulach@9
   553
        for (int i = 1; i < width - 1; i++) {
jtulach@9
   554
            desk[0][i] = '-';
jtulach@9
   555
            desk[height -1][i] = '-';
jtulach@9
   556
        }
jtulach@9
   557
        desk[0][0] = '+';
jtulach@9
   558
        desk[0][width - 1] = '+';
jtulach@9
   559
        desk[height - 1][0] = '+';
jtulach@9
   560
        desk[height - 1][width - 1] = '+';
jtulach@9
   561
jtulach@9
   562
        {
jtulach@9
   563
            for (Player p : players) {
jtulach@26
   564
                int px = p.getXInternal() / 2;
jtulach@26
   565
                int py = p.getYInternal() / 2;
jtulach@9
   566
                desk
jtulach@9
   567
                    [1 + py * (spaceY + 1) + spaceY / 2]
jtulach@13
   568
                    [1 + px * (spaceX + 1) + spaceX / 2] = p.endDirection.player;
jtulach@13
   569
                paintLeftFences(desk, p.endDirection, p.getFences(), p.equals(getCurrentPlayer()));
jtulach@9
   570
            }
jtulach@0
   571
        }
jtulach@0
   572
        
jtulach@0
   573
        for (Fence f : fences) {
jtulach@12
   574
            int fx = (f.getX() / 2 + 1) * (spaceX + 1);
jtulach@12
   575
            int fy = (f.getY() / 2 + 1) * (spaceY + 1);
jtulach@0
   576
            switch (f.getOrientation()) {
jtulach@0
   577
                case HORIZONTAL:
jtulach@12
   578
                    for (int i = -spaceX; i <= spaceX; i++) {
jtulach@12
   579
                        desk[fy][fx + i] = '-';
jtulach@0
   580
                    }
jtulach@0
   581
                    break;
jtulach@0
   582
                case VERTICAL:
jtulach@12
   583
                    for (int i = -spaceY; i <= spaceY; i++) {
jtulach@12
   584
                        desk[fy + i][fx] = '|';
jtulach@0
   585
                    }
jtulach@0
   586
                    break;
jtulach@0
   587
                default: 
jtulach@11
   588
                    throw new IllegalStateException ("Unknown orientation: " + f.getOrientation ()); // NOI18N
jtulach@0
   589
            }
jtulach@0
   590
        }
jtulach@40
   591
        w.write("        ");
jtulach@40
   592
        northSouthSign(width, 'N', w);
jtulach@40
   593
        w.write(System.getProperty("line.separator")); // NOI18N
jtulach@40
   594
        w.write("        ");
jtulach@40
   595
        columnLine(width, spaceX, w);
jtulach@40
   596
        w.write(System.getProperty("line.separator")); // NOI18N
jtulach@40
   597
        w.write("        ");
jtulach@40
   598
        subColumnLine(width, spaceX, w);
jtulach@40
   599
        w.write(System.getProperty("line.separator")); // NOI18N
jtulach@40
   600
        int cnt = 9;
jtulach@40
   601
        for (int y = height - 1; y >= 0; y--) {
jtulach@40
   602
            if (y == height / 2) {
jtulach@40
   603
                w.write("[W]  ");
jtulach@40
   604
            } else {
jtulach@40
   605
                w.write("     ");
jtulach@40
   606
            }
jtulach@40
   607
            boolean number = y % (spaceY + 1) == 0 && y > 0 && y < height - 1;
jtulach@40
   608
            if (number) {
jtulach@40
   609
                cnt--;
jtulach@40
   610
                w.write(Integer.toString(cnt));
jtulach@40
   611
                w.write("--");
jtulach@40
   612
            } else {
jtulach@40
   613
                w.write("   ");
jtulach@40
   614
            }
jtulach@9
   615
            for (int x = 0; x < width; x++) {
jtulach@9
   616
                w.write(desk[y][x]);
jtulach@9
   617
            }
jtulach@40
   618
            if (number) {
jtulach@40
   619
                w.write("--");
jtulach@40
   620
                w.write(Integer.toString(cnt));
jtulach@40
   621
            } else {
jtulach@40
   622
                w.write("   ");
jtulach@40
   623
            }
jtulach@40
   624
            if (y == height / 2) {
jtulach@40
   625
                w.write("  [E]");
jtulach@40
   626
            }
jtulach@9
   627
            w.write(System.getProperty("line.separator")); // NOI18N
jtulach@9
   628
        }
jtulach@40
   629
        w.write("        ");
jtulach@40
   630
        subColumnLine(width, spaceX, w);
jtulach@40
   631
        w.write(System.getProperty("line.separator")); // NOI18N
jtulach@40
   632
        w.write("        ");
jtulach@40
   633
        columnLine(width, spaceX, w);
jtulach@40
   634
        w.write(System.getProperty("line.separator")); // NOI18N
jtulach@40
   635
        w.write(System.getProperty("line.separator")); // NOI18N
jtulach@40
   636
        w.write("        ");
jtulach@40
   637
        northSouthSign(width, 'S', w);
jtulach@0
   638
    }
jtulach@11
   639
jtulach@11
   640
jtulach@13
   641
    private void paintLeftFences(char[][] desk, Direction endDirection, int fences, boolean currentTurn) {
jtulach@13
   642
        assert fences >= 0 && fences <= 10 : "Players: " + players;
jtulach@11
   643
        switch (endDirection) {
jtulach@40
   644
            case SOUTH: {
jtulach@11
   645
                for (int i = 0; i < fences; i++) {
jtulach@11
   646
                    desk[desk.length - 1][i + 1] = '|';
jtulach@11
   647
                }
jtulach@13
   648
                if (currentTurn) {
jtulach@13
   649
                    desk[desk.length - 1][fences + 1] = '*';
jtulach@13
   650
                }
jtulach@11
   651
                break;
jtulach@11
   652
            }
jtulach@40
   653
            case NORTH: {
jtulach@11
   654
                for (int i = 0; i < fences; i++) {
jtulach@11
   655
                    desk[0][i + 1] = '|';
jtulach@11
   656
                }
jtulach@13
   657
                if (currentTurn) {
jtulach@13
   658
                    desk[0][fences + 1] = '*';
jtulach@13
   659
                }
jtulach@11
   660
                break;
jtulach@11
   661
            }
jtulach@11
   662
            default:
jtulach@11
   663
                assert false;
jtulach@11
   664
        }
jtulach@11
   665
    }
jtulach@11
   666
jtulach@11
   667
jtulach@0
   668
    //
jtulach@0
   669
    // Standard Methods
jtulach@0
   670
    // 
jtulach@0
   671
jtulach@0
   672
    
jtulach@2
   673
    @Override
jtulach@0
   674
    public int hashCode () {
jtulach@0
   675
        return occupied.hashCode ();
jtulach@0
   676
    }
jtulach@0
   677
    
jtulach@2
   678
    @Override
jtulach@0
   679
    public boolean equals (Object o) {
jtulach@0
   680
        if (o instanceof Board) {
jtulach@0
   681
            Board b = (Board)o;
jtulach@0
   682
            return occupied.equals (b.occupied) && players.equals (b.players);
jtulach@0
   683
        }
jtulach@0
   684
        return false;
jtulach@0
   685
    }
jtulach@12
   686
jaroslav@48
   687
    /** Converts the board into string representation. For more information
jaroslav@48
   688
     * about the format see {@link #write(java.io.Writer)}. To read the
jaroslav@48
   689
     * string back use {@link #valueOf(java.lang.String)}.
jaroslav@48
   690
     *
jaroslav@48
   691
     * @return string representing the board
jaroslav@48
   692
     */
jtulach@12
   693
    @Override
jtulach@12
   694
    public String toString() {
jaroslav@179
   695
        return getCode();
jaroslav@178
   696
    }
jaroslav@178
   697
jtulach@0
   698
    //
jtulach@0
   699
    // Validation methods
jtulach@0
   700
    //
jtulach@0
   701
jtulach@0
   702
    /** Computes new position of a player and checks whether there is no
jtulach@0
   703
     * fence between the old and new.
jtulach@0
   704
     */
jtulach@0
   705
    private Player newPosition (Player old, Player.Direction direction) throws IllegalPositionException {
jtulach@0
   706
        return newPosition (old, direction, occupied);
jtulach@0
   707
    }
jtulach@0
   708
    
jtulach@0
   709
    
jtulach@3
   710
    private static Player newPosition (Player old, Player.Direction direction, BitSet occupied) throws IllegalPositionException {
jtulach@0
   711
        int nx = old.x;
jtulach@0
   712
        int ny = old.y;
jtulach@0
   713
        int fx = old.x;
jtulach@0
   714
        int fy = old.y;
jtulach@0
   715
        
jtulach@0
   716
        switch (direction) {
jtulach@0
   717
            case NORTH: 
jtulach@0
   718
                ny = old.y + 2;
jtulach@0
   719
                fy = old.y + 1;
jtulach@0
   720
                break;
jtulach@0
   721
            case SOUTH: 
jtulach@0
   722
                ny = old.y - 2; 
jtulach@0
   723
                fy = old.y - 1;
jtulach@0
   724
                break;
jtulach@0
   725
            case EAST: 
jtulach@0
   726
                nx = old.x + 2; 
jtulach@0
   727
                fx = old.x + 1;
jtulach@0
   728
                break;
jtulach@0
   729
            case WEST: 
jtulach@0
   730
                nx = old.x - 2; 
jtulach@0
   731
                fx = old.x - 1;
jtulach@0
   732
                break;
jtulach@0
   733
            default: throw new IllegalStateException ("Unknown direction: " + direction); // NOI18N
jtulach@0
   734
        }
jtulach@0
   735
        
jtulach@0
   736
        if (nx < 0 || nx > 16) throw new IllegalPositionException ("Wrong player position: " + nx + ":" + ny); // NOI18N
jtulach@0
   737
        if (ny < 0 || ny > 16) throw new IllegalPositionException ("Wrong player position: " + nx + ":" + ny); // NOI18N
jtulach@0
   738
        
jtulach@0
   739
        int fenceIndex = toIndex (fx, fy);
jtulach@0
   740
        if (occupied.get (fenceIndex)) {
jtulach@0
   741
            throw new IllegalPositionException ("You cannot go over fences"); // NOI18N
jtulach@0
   742
        }
jtulach@0
   743
        
jtulach@0
   744
        return new Player (nx, ny, old.getFences (), old.endDirection);
jtulach@0
   745
    }
jtulach@0
   746
    
jtulach@0
   747
    /** @param position the current position of the player
jtulach@0
   748
     * @param endDir the side the player wants to reach
jtulach@0
   749
     * @param fences bits set to 1 when fences are placed
jtulach@0
   750
     * @param reached bits on squares that were already reached (modified during run)
jtulach@0
   751
     * @return true if the end line can be reached
jtulach@0
   752
     */
jtulach@3
   753
    private static boolean accessibleFinalLine (Player position, Player.Direction endDir, BitSet fences, BitSet reached) {
jtulach@0
   754
        int index = toIndex (position);
jtulach@0
   755
        if (reached.get (index)) {
jtulach@0
   756
            // already visited without success
jtulach@0
   757
            return false;
jtulach@0
   758
        }
jaroslav@92
   759
jaroslav@92
   760
        if (endDir.reached(position)) {
jaroslav@92
   761
            return true;
jtulach@0
   762
        }
jtulach@0
   763
        
jtulach@0
   764
        reached.set (index);
jtulach@0
   765
        
jtulach@0
   766
        for (Player.Direction oneDirection : Player.Direction.values ()) {
jtulach@0
   767
            try {
jtulach@0
   768
                if (accessibleFinalLine (newPosition (position, oneDirection, fences), endDir, fences, reached)) {
jtulach@0
   769
                    return true;
jtulach@0
   770
                }
jtulach@0
   771
            } catch (IllegalPositionException ex) {
jtulach@0
   772
                // ok, try once more
jtulach@0
   773
            }
jtulach@0
   774
        }
jtulach@0
   775
        
jtulach@0
   776
        return false;
jtulach@0
   777
    }
jtulach@0
   778
    
jtulach@0
   779
    /** Computes mask of the occupried bits.
jtulach@0
   780
     */
jtulach@3
   781
    private static BitSet computeOccupied (
jtulach@3
   782
        Collection<Player> players, Collection<Fence> fences
jtulach@0
   783
    ) throws IllegalPositionException {
jaroslav@92
   784
        BitSet occupied = new BitSet (17 * 17);
jtulach@0
   785
        
jtulach@0
   786
        for (Player p : players) {
jtulach@26
   787
            if (p.getXInternal() % 2 == 1) throw new IllegalPositionException ("Wrong player position: " + p); // NOI18N
jtulach@26
   788
            if (p.getYInternal() % 2 == 1) throw new IllegalPositionException ("Wrong player position: " + p); // NOI18N
jtulach@0
   789
            
jtulach@0
   790
            int index = toIndex (p);
jtulach@0
   791
            if (occupied.get (index)) {
jtulach@0
   792
                throw new IllegalPositionException ("There cannot be two players at " + p); // NOI18N
jtulach@0
   793
            }
jtulach@0
   794
            occupied.set (index);
jtulach@0
   795
        }
jtulach@0
   796
        
jtulach@0
   797
        for (Fence f : fences) {
jtulach@0
   798
            
jtulach@0
   799
            for (int i = -1; i <= 1; i++) {
jtulach@0
   800
                int index = toIndex (f, i);
jtulach@0
   801
                if (index < 0 || index > occupied.size ()) {
jtulach@0
   802
                    throw new IllegalPositionException ("Wrong fence position: " + f); // NOI18N
jtulach@0
   803
                }
jtulach@0
   804
                if (occupied.get (index)) {
jtulach@0
   805
                    throw new IllegalPositionException ("Fence collition: " + f); // NOI18N
jtulach@0
   806
                }
jtulach@0
   807
                
jtulach@0
   808
                occupied.set (index);
jtulach@0
   809
            }
jtulach@0
   810
            
jtulach@0
   811
        }
jtulach@0
   812
        
jtulach@0
   813
        return occupied;
jtulach@0
   814
    }
jtulach@0
   815
jtulach@0
   816
    /** Converts twodimensional coordinates of player to one dimensional index.
jtulach@0
   817
     */
jtulach@0
   818
    private static int toIndex (Player p) {
jtulach@26
   819
        return toIndex (p.getXInternal(), p.getYInternal());
jtulach@0
   820
    }
jtulach@0
   821
    
jtulach@0
   822
    /** Converts twodimensional coordinates of fence to one dimensional index.
jtulach@0
   823
     * @param f the fence
jtulach@0
   824
     * @param whichPart (-1, 0, 1) 
jtulach@0
   825
     */
jtulach@0
   826
    private static int toIndex (Fence f, int whichPart) {
jtulach@0
   827
        int x = f.getX ();
jtulach@0
   828
        int y = f.getY ();
jtulach@0
   829
        
jtulach@0
   830
        switch (f.getOrientation ()) {
jtulach@0
   831
            case HORIZONTAL: x += whichPart; break;
jtulach@0
   832
            case VERTICAL: y += whichPart; break;
jtulach@0
   833
            default: throw new IllegalStateException ("Wrong orientation: " + f.getOrientation ()); // NOI18N
jtulach@0
   834
        }
jtulach@0
   835
        
jtulach@0
   836
        return toIndex (x, y);
jtulach@0
   837
    }
jtulach@0
   838
    
jtulach@0
   839
    /** Diagonal conversion of two positive integers to one integer.
jtulach@0
   840
     * <pre>
jtulach@0
   841
     * y3 9
jtulach@0
   842
     * y2 5  8
jtulach@0
   843
     * y1 2  4  7
jtulach@0
   844
     * y0 0  1  3  6
jtulach@0
   845
     *   x0 x1 x2 x3
jtulach@0
   846
     * </pre>
jtulach@0
   847
     */
jtulach@0
   848
    private static int toIndex (int x, int y) {
jaroslav@92
   849
        assert x >= 0 && x < 17;
jaroslav@92
   850
        assert y >= 0 && y < 17;
jaroslav@92
   851
        return 17 * y + x;
jtulach@0
   852
    }
jtulach@17
   853
jaroslav@179
   854
    Board(String hashCode) throws IllegalStateException{
jaroslav@178
   855
        this.fences = new HashSet<Fence>();
jaroslav@178
   856
        if((hashCode != null) && (hashCode.length() > 6)){
jaroslav@178
   857
            char[]c = hashCode.toCharArray();
jaroslav@178
   858
            this.players = Collections.unmodifiableList (Arrays.asList (new Player[] {
jaroslav@178
   859
                new Player ((c[0]-'A')*2, (c[1]-'0')*2, c[2]-'a', Player.Direction.NORTH),
jaroslav@178
   860
                new Player ((c[3]-'A')*2, (c[4]-'0')*2, c[5]-'a', Player.Direction.SOUTH),
jaroslav@178
   861
            }));
jaroslav@178
   862
            if(c[6]=='w'){
jaroslav@178
   863
                this.turn = 0;
jaroslav@178
   864
                this.winner = null;
jaroslav@178
   865
            }else if(c[6]=='b'){
jaroslav@178
   866
                this.turn = 1;
jaroslav@178
   867
                this.winner = null;
jaroslav@178
   868
            }else if(c[6]=='W'){
jaroslav@178
   869
                this.turn = 0;
jaroslav@178
   870
                this.winner = this.players.get(0);
jaroslav@178
   871
            }else if(c[6]=='B'){
jaroslav@178
   872
                this.turn = 1;
jaroslav@178
   873
                this.winner = this.players.get(1);
jaroslav@178
   874
            }else{
jaroslav@178
   875
                this.turn = 0;
jaroslav@178
   876
                this.winner = null;
jaroslav@178
   877
            }
jaroslav@178
   878
            for(int i=7; i<c.length;i+=2){
jaroslav@178
   879
                int f = Integer.parseInt(hashCode.substring(i, i+2),16);
jaroslav@178
   880
                Fence.Orientation o = Fence.Orientation.HORIZONTAL;
jaroslav@178
   881
                if(f > 64){
jaroslav@178
   882
                    o = Fence.Orientation.VERTICAL;
jaroslav@178
   883
                    f -= 64;
jaroslav@178
   884
                }
jaroslav@178
   885
                fences.add(new Fence((f/8)*2+1, (f%8)*2+1,o));
jaroslav@178
   886
            }
jaroslav@178
   887
        }else{
jaroslav@178
   888
            this.players = Collections.unmodifiableList (Arrays.asList (new Player[] {
jaroslav@178
   889
                new Player (8,0,10,Player.Direction.NORTH),
jaroslav@178
   890
                new Player (8,16,10,Player.Direction.SOUTH),
jaroslav@178
   891
            }));
jaroslav@178
   892
            this.winner = null;
jaroslav@178
   893
            this.turn = 0;
jaroslav@178
   894
        }
jaroslav@178
   895
        try {
jaroslav@178
   896
            this.occupied = computeOccupied (players, fences);
jaroslav@178
   897
        } catch (IllegalPositionException ex) {
jaroslav@178
   898
            throw new IllegalStateException (ex.getMessage ());
jaroslav@178
   899
        }
jaroslav@178
   900
    }
jaroslav@178
   901
jaroslav@179
   902
    public final String getCode() {
jaroslav@179
   903
        Board b = this;
jaroslav@178
   904
        StringBuilder sb = new StringBuilder();
jaroslav@178
   905
        for(Player p: b.getPlayers()){
jaroslav@178
   906
            sb.append((char)(p.getColumn() + 'A'));
jaroslav@178
   907
            sb.append((char)(p.getRow() + '0'));
jaroslav@178
   908
            sb.append((char)(p.getFences() + 'a'));
jaroslav@178
   909
        }
jaroslav@178
   910
        Player winner = b.getWinner();
jaroslav@178
   911
        if(winner == null){
jaroslav@178
   912
            if(b.players.indexOf(b.getCurrentPlayer())==0)
jaroslav@178
   913
                sb.append('w');
jaroslav@178
   914
            else if(b.players.indexOf(b.getCurrentPlayer())==1)
jaroslav@178
   915
                sb.append('b');
jaroslav@178
   916
            else
jaroslav@178
   917
                sb.append('n');
jaroslav@178
   918
        }else{
jaroslav@178
   919
            if(b.players.indexOf(winner)==0)
jaroslav@178
   920
                sb.append('W');
jaroslav@178
   921
            else if(b.players.indexOf(winner)==1)
jaroslav@178
   922
                sb.append('B');
jaroslav@178
   923
            else
jaroslav@178
   924
                sb.append('N');
jaroslav@178
   925
        }
jaroslav@178
   926
jaroslav@178
   927
        TreeSet<Integer> fences = new TreeSet<Integer>();
jaroslav@178
   928
        for(Fence f: b.getFences()){
jaroslav@178
   929
            int a = (f.getColumn() - 'A')*8 + (f.getRow()-1);
jaroslav@178
   930
            if(f.getOrientation().equals(Fence.Orientation.VERTICAL))
jaroslav@178
   931
                a+=64;
jaroslav@178
   932
            fences.add(a);
jaroslav@178
   933
        }
jaroslav@178
   934
        for(int f: fences){
jaroslav@178
   935
            if(f<16)
jaroslav@178
   936
                sb.append('0');
jaroslav@178
   937
            sb.append(Integer.toHexString(f));
jaroslav@178
   938
        }
jaroslav@178
   939
        return sb.toString();
jaroslav@178
   940
    }
jaroslav@178
   941
jtulach@0
   942
}