quoridor/src/main/java/cz/xelfi/quoridor/Move.java
changeset 34 34baf57f2d4e
parent 31 8e6a6857c7b5
child 75 6802034b7a6f
     1.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Move.java	Sat Jun 06 14:51:46 2009 +0200
     1.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Move.java	Sun Jul 12 13:35:58 2009 +0200
     1.3 @@ -113,12 +113,50 @@
     1.4          return new Move(first, second);
     1.5      }
     1.6  
     1.7 +    /** Converts the value of the move string into valid move, if possible.
     1.8 +     *
     1.9 +     * @param move the string in format used by {@link #toString()}
    1.10 +     * @return the move which's toString() returns the move parameter
    1.11 +     * @throws IllegalPositionException raised in case the parameter does not
    1.12 +     *      represent a valid move
    1.13 +     */
    1.14 +    public static Move valueOf(String move) throws IllegalPositionException {
    1.15 +        switch (move.length()) {
    1.16 +            case 3:
    1.17 +            if (move.startsWith("H")) {
    1.18 +                return Move.fence(move.charAt(1), move.charAt(2) - '0', Fence.Orientation.HORIZONTAL);
    1.19 +            }
    1.20 +            if (move.startsWith("V")) {
    1.21 +                return Move.fence(move.charAt(1), move.charAt(2) - '0', Fence.Orientation.VERTICAL);
    1.22 +            }
    1.23 +            break;
    1.24 +            case 2:
    1.25 +                return Move.jump(Direction.valueOf(move.charAt(0)), Direction.valueOf(move.charAt(1)));
    1.26 +            case 1:
    1.27 +                switch (move.charAt(0)) {
    1.28 +                    case 'N': return Move.NORTH;
    1.29 +                    case 'S': return Move.SOUTH;
    1.30 +                    case 'E': return Move.EAST;
    1.31 +                    case 'W': return Move.WEST;
    1.32 +                }
    1.33 +        }
    1.34 +        throw new IllegalPositionException(move);
    1.35 +    }
    1.36 +
    1.37      @Override
    1.38      public String toString() {
    1.39          if (fence != null) {
    1.40 -            return "Move[" + fence + "]";
    1.41 +            switch (fence.getOrientation()) {
    1.42 +                case HORIZONTAL: return "H" + fence.getColumn() + fence.getRow();
    1.43 +                case VERTICAL: return "V" + fence.getColumn() + fence.getRow();
    1.44 +            }
    1.45 +            throw new IllegalStateException();
    1.46          } else {
    1.47 -            return "Move" + Arrays.toString(direction);
    1.48 +            StringBuilder sb = new StringBuilder();
    1.49 +            for (Direction d : direction) {
    1.50 +                sb.append(d.name().charAt(0));
    1.51 +            }
    1.52 +            return sb.toString();
    1.53          }
    1.54      }
    1.55