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
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor;
    28 
    29 import cz.xelfi.quoridor.Player.Direction;
    30 import java.util.Arrays;
    31 
    32 /** Encapsulates one possible move that can be applied to existing {@link Board}
    33  * by calling its {@link Board#apply} method.
    34  *
    35  * @author Jaroslav Tulach <jtulach@netbeans.org>
    36  */
    37 public final class Move extends Object {
    38     final Direction[] direction;
    39     final Fence fence;
    40 
    41     private Move(Direction... arr) {
    42         direction = arr;
    43         fence = null;
    44     }
    45     private Move(Fence f) {
    46         fence = f;
    47         direction = null;
    48     }
    49 
    50     /** Moves the player's figure to north */
    51     public static final Move NORTH = new Move(Direction.NORTH);
    52     /** Moves the player's figure to east */
    53     public static final Move EAST = new Move(Direction.EAST);
    54     /** Moves the player's figure to south */
    55     public static final Move SOUTH = new Move(Direction.SOUTH);
    56     /** Moves the player's figure to west */
    57     public static final Move WEST = new Move(Direction.WEST);
    58 
    59     /** Places a fence into given position with specified orientation.
    60      * The faces postions are numbered 1-8 and A-H according to following
    61      * graph: <pre>
    62             H   G   F   E   D   C   B   A
    63             |   |   |   |   |   |   |   |
    64         +-----------------------------------+
    65         |                                   |
    66      1--|   +   +   +   +   +   +   +   +   |--1
    67         |                                   |
    68      2--|   +   +   +   +   +   +   +   +   |--2
    69         |                                   |
    70      3--|   +   +   +   +   +   +   +   +   |--3
    71         |                                   |
    72      4--|   +   +   +   +   +   +   +   +   |--4
    73 [E]     |                                   |     [W]
    74      5--|   +   +   +   +   +   +   +   +   |--5
    75         |                                   |
    76      6--|   +   +   +   +   +   +   +   +   |--6
    77         |                                   |
    78      7--|   +   +   +   +   +   +   +   +   |--7
    79         |                                   |
    80      8--|   +   +   +   +   +   +   +   +   |--8
    81         |                                   |
    82         +-----------------------------------+
    83             |   |   |   |   |   |   |   |
    84             H   G   F   E   D   C   B   A
    85      * </pre>
    86      *
    87      *
    88      * @param column x-coordinate of the middle of the fence from 'A' to 'B'
    89      * @param row y-coordinate of the middle of the fence
    90      * @param orientation place the fence horizontally or vertically
    91      * @return the new board
    92      * @exception IllegalPositionException if the move is not possible
    93      */
    94     public static Move fence(char column, int row, Fence.Orientation orientation) throws IllegalPositionException {
    95         if (column < 'A' || 'H' < column) {
    96             throw new IllegalPositionException ("x coordinate has to be from A to H"); // NOI18N
    97         }
    98         if (row < 1 || row > 8) {
    99             throw new IllegalPositionException ("y coordinate has to be from 1 to 8"); // NOI18N
   100         }
   101         return new Move(new Fence ((column - 'A') * 2 + 1, row * 2 - 1, orientation));
   102     }
   103 
   104     /** Moves the player in given directions. After applying
   105      * the first direction, the pawn has to get on a position
   106      * that is occupied by other player's pawn.
   107      *
   108      * @param where one or two directions saying where
   109      * @return the new board
   110      * @exception IllegalPositionException if the move is not possible
   111      */
   112     public static Move jump(Direction first, Direction second) {
   113         return new Move(first, second);
   114     }
   115 
   116     /** Converts the value of the move string into valid move, if possible.
   117      *
   118      * @param move the string in format used by {@link #toString()}
   119      * @return the move which's toString() returns the move parameter
   120      * @throws IllegalPositionException raised in case the parameter does not
   121      *      represent a valid move
   122      */
   123     public static Move valueOf(String move) throws IllegalPositionException {
   124         switch (move.length()) {
   125             case 3:
   126             if (move.startsWith("H")) {
   127                 return Move.fence(move.charAt(1), move.charAt(2) - '0', Fence.Orientation.HORIZONTAL);
   128             }
   129             if (move.startsWith("V")) {
   130                 return Move.fence(move.charAt(1), move.charAt(2) - '0', Fence.Orientation.VERTICAL);
   131             }
   132             break;
   133             case 2:
   134                 return Move.jump(Direction.valueOf(move.charAt(0)), Direction.valueOf(move.charAt(1)));
   135             case 1:
   136                 switch (move.charAt(0)) {
   137                     case 'N': return Move.NORTH;
   138                     case 'S': return Move.SOUTH;
   139                     case 'E': return Move.EAST;
   140                     case 'W': return Move.WEST;
   141                 }
   142         }
   143         throw new IllegalPositionException(move);
   144     }
   145 
   146     @Override
   147     public String toString() {
   148         if (fence != null) {
   149             switch (fence.getOrientation()) {
   150                 case HORIZONTAL: return "H" + fence.getColumn() + fence.getRow();
   151                 case VERTICAL: return "V" + fence.getColumn() + fence.getRow();
   152             }
   153             throw new IllegalStateException();
   154         } else {
   155             StringBuilder sb = new StringBuilder();
   156             for (Direction d : direction) {
   157                 sb.append(d.name().charAt(0));
   158             }
   159             return sb.toString();
   160         }
   161     }
   162 
   163     @Override
   164     public boolean equals(Object obj) {
   165         if (obj == null) {
   166             return false;
   167         }
   168         if (getClass() != obj.getClass()) {
   169             return false;
   170         }
   171         final Move other = (Move) obj;
   172         if (!Arrays.deepEquals(this.direction, other.direction)) {
   173             return false;
   174         }
   175         if (this.fence != other.fence && (this.fence == null || !this.fence.equals(other.fence))) {
   176             return false;
   177         }
   178         return true;
   179     }
   180 
   181     @Override
   182     public int hashCode() {
   183         int hash = 3;
   184         hash = 47 * hash + Arrays.deepHashCode(this.direction);
   185         hash = 47 * hash + (this.fence != null ? this.fence.hashCode() : 0);
   186         return hash;
   187     }
   188 
   189 
   190 }