quoridor/src/main/java/cz/xelfi/quoridor/Move.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 12 Jul 2009 13:35:58 +0200
changeset 34 34baf57f2d4e
parent 31 8e6a6857c7b5
child 75 6802034b7a6f
permissions -rw-r--r--
Adding support for reading Move's from strings
jtulach@3
     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@3
    25
 */
jtulach@3
    26
jtulach@3
    27
package cz.xelfi.quoridor;
jtulach@3
    28
jtulach@15
    29
import cz.xelfi.quoridor.Player.Direction;
jtulach@30
    30
import java.util.Arrays;
jtulach@3
    31
jtulach@16
    32
/** Encapsulates one possible move that can be applied to existing {@link Board}
jtulach@16
    33
 * by calling its {@link Board#apply} method.
jtulach@3
    34
 *
jtulach@3
    35
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@3
    36
 */
jtulach@3
    37
public final class Move extends Object {
jtulach@3
    38
    final Direction[] direction;
jtulach@3
    39
    final Fence fence;
jtulach@3
    40
jtulach@3
    41
    private Move(Direction... arr) {
jtulach@3
    42
        direction = arr;
jtulach@3
    43
        fence = null;
jtulach@3
    44
    }
jtulach@3
    45
    private Move(Fence f) {
jtulach@3
    46
        fence = f;
jtulach@3
    47
        direction = null;
jtulach@3
    48
    }
jtulach@3
    49
jtulach@4
    50
    /** Moves the player's figure to north */
jtulach@3
    51
    public static final Move NORTH = new Move(Direction.NORTH);
jtulach@4
    52
    /** Moves the player's figure to east */
jtulach@3
    53
    public static final Move EAST = new Move(Direction.EAST);
jtulach@4
    54
    /** Moves the player's figure to south */
jtulach@3
    55
    public static final Move SOUTH = new Move(Direction.SOUTH);
jtulach@4
    56
    /** Moves the player's figure to west */
jtulach@3
    57
    public static final Move WEST = new Move(Direction.WEST);
jtulach@3
    58
jtulach@4
    59
    /** Places a fence into given position with specified orientation.
jtulach@4
    60
     * The faces postions are numbered 1-8 and A-H according to following
jtulach@4
    61
     * graph: <pre>
jtulach@4
    62
            H   G   F   E   D   C   B   A
jtulach@4
    63
            |   |   |   |   |   |   |   |
jtulach@4
    64
        +-----------------------------------+
jtulach@4
    65
        |                                   |
jtulach@4
    66
     1--|   +   +   +   +   +   +   +   +   |--1
jtulach@4
    67
        |                                   |
jtulach@4
    68
     2--|   +   +   +   +   +   +   +   +   |--2
jtulach@4
    69
        |                                   |
jtulach@4
    70
     3--|   +   +   +   +   +   +   +   +   |--3
jtulach@4
    71
        |                                   |
jtulach@4
    72
     4--|   +   +   +   +   +   +   +   +   |--4
jtulach@4
    73
[E]     |                                   |     [W]
jtulach@4
    74
     5--|   +   +   +   +   +   +   +   +   |--5
jtulach@4
    75
        |                                   |
jtulach@4
    76
     6--|   +   +   +   +   +   +   +   +   |--6
jtulach@4
    77
        |                                   |
jtulach@4
    78
     7--|   +   +   +   +   +   +   +   +   |--7
jtulach@4
    79
        |                                   |
jtulach@4
    80
     8--|   +   +   +   +   +   +   +   +   |--8
jtulach@4
    81
        |                                   |
jtulach@4
    82
        +-----------------------------------+
jtulach@4
    83
            |   |   |   |   |   |   |   |
jtulach@4
    84
            H   G   F   E   D   C   B   A
jtulach@4
    85
     * </pre>
jtulach@4
    86
     *
jtulach@4
    87
     *
jtulach@21
    88
     * @param column x-coordinate of the middle of the fence from 'A' to 'B'
jtulach@21
    89
     * @param row y-coordinate of the middle of the fence
jtulach@4
    90
     * @param orientation place the fence horizontally or vertically
jtulach@4
    91
     * @return the new board
jtulach@4
    92
     * @exception IllegalPositionException if the move is not possible
jtulach@4
    93
     */
jtulach@21
    94
    public static Move fence(char column, int row, Fence.Orientation orientation) throws IllegalPositionException {
jtulach@21
    95
        if (column < 'A' || 'H' < column) {
jtulach@3
    96
            throw new IllegalPositionException ("x coordinate has to be from A to H"); // NOI18N
jtulach@3
    97
        }
jtulach@31
    98
        if (row < 1 || row > 8) {
jtulach@31
    99
            throw new IllegalPositionException ("y coordinate has to be from 1 to 8"); // NOI18N
jtulach@31
   100
        }
jtulach@21
   101
        return new Move(new Fence ((column - 'A') * 2 + 1, row * 2 - 1, orientation));
jtulach@3
   102
    }
jtulach@3
   103
jtulach@4
   104
    /** Moves the player in given directions. After applying
jtulach@4
   105
     * the first direction, the pawn has to get on a position
jtulach@4
   106
     * that is occupied by other player's pawn.
jtulach@4
   107
     *
jtulach@4
   108
     * @param where one or two directions saying where
jtulach@4
   109
     * @return the new board
jtulach@4
   110
     * @exception IllegalPositionException if the move is not possible
jtulach@4
   111
     */
jtulach@3
   112
    public static Move jump(Direction first, Direction second) {
jtulach@3
   113
        return new Move(first, second);
jtulach@3
   114
    }
jtulach@30
   115
jtulach@34
   116
    /** Converts the value of the move string into valid move, if possible.
jtulach@34
   117
     *
jtulach@34
   118
     * @param move the string in format used by {@link #toString()}
jtulach@34
   119
     * @return the move which's toString() returns the move parameter
jtulach@34
   120
     * @throws IllegalPositionException raised in case the parameter does not
jtulach@34
   121
     *      represent a valid move
jtulach@34
   122
     */
jtulach@34
   123
    public static Move valueOf(String move) throws IllegalPositionException {
jtulach@34
   124
        switch (move.length()) {
jtulach@34
   125
            case 3:
jtulach@34
   126
            if (move.startsWith("H")) {
jtulach@34
   127
                return Move.fence(move.charAt(1), move.charAt(2) - '0', Fence.Orientation.HORIZONTAL);
jtulach@34
   128
            }
jtulach@34
   129
            if (move.startsWith("V")) {
jtulach@34
   130
                return Move.fence(move.charAt(1), move.charAt(2) - '0', Fence.Orientation.VERTICAL);
jtulach@34
   131
            }
jtulach@34
   132
            break;
jtulach@34
   133
            case 2:
jtulach@34
   134
                return Move.jump(Direction.valueOf(move.charAt(0)), Direction.valueOf(move.charAt(1)));
jtulach@34
   135
            case 1:
jtulach@34
   136
                switch (move.charAt(0)) {
jtulach@34
   137
                    case 'N': return Move.NORTH;
jtulach@34
   138
                    case 'S': return Move.SOUTH;
jtulach@34
   139
                    case 'E': return Move.EAST;
jtulach@34
   140
                    case 'W': return Move.WEST;
jtulach@34
   141
                }
jtulach@34
   142
        }
jtulach@34
   143
        throw new IllegalPositionException(move);
jtulach@34
   144
    }
jtulach@34
   145
jtulach@30
   146
    @Override
jtulach@30
   147
    public String toString() {
jtulach@30
   148
        if (fence != null) {
jtulach@34
   149
            switch (fence.getOrientation()) {
jtulach@34
   150
                case HORIZONTAL: return "H" + fence.getColumn() + fence.getRow();
jtulach@34
   151
                case VERTICAL: return "V" + fence.getColumn() + fence.getRow();
jtulach@34
   152
            }
jtulach@34
   153
            throw new IllegalStateException();
jtulach@30
   154
        } else {
jtulach@34
   155
            StringBuilder sb = new StringBuilder();
jtulach@34
   156
            for (Direction d : direction) {
jtulach@34
   157
                sb.append(d.name().charAt(0));
jtulach@34
   158
            }
jtulach@34
   159
            return sb.toString();
jtulach@30
   160
        }
jtulach@30
   161
    }
jtulach@30
   162
jtulach@30
   163
    @Override
jtulach@30
   164
    public boolean equals(Object obj) {
jtulach@30
   165
        if (obj == null) {
jtulach@30
   166
            return false;
jtulach@30
   167
        }
jtulach@30
   168
        if (getClass() != obj.getClass()) {
jtulach@30
   169
            return false;
jtulach@30
   170
        }
jtulach@30
   171
        final Move other = (Move) obj;
jtulach@30
   172
        if (!Arrays.deepEquals(this.direction, other.direction)) {
jtulach@30
   173
            return false;
jtulach@30
   174
        }
jtulach@30
   175
        if (this.fence != other.fence && (this.fence == null || !this.fence.equals(other.fence))) {
jtulach@30
   176
            return false;
jtulach@30
   177
        }
jtulach@30
   178
        return true;
jtulach@30
   179
    }
jtulach@30
   180
jtulach@30
   181
    @Override
jtulach@30
   182
    public int hashCode() {
jtulach@30
   183
        int hash = 3;
jtulach@30
   184
        hash = 47 * hash + Arrays.deepHashCode(this.direction);
jtulach@30
   185
        hash = 47 * hash + (this.fence != null ? this.fence.hashCode() : 0);
jtulach@30
   186
        return hash;
jtulach@30
   187
    }
jtulach@30
   188
jtulach@30
   189
jtulach@3
   190
}