1.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Move.java Sat Jun 06 15:13:04 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
2.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Player.java Sat Jun 06 15:13:04 2009 +0200
2.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Player.java Sun Jul 12 13:35:58 2009 +0200
2.3 @@ -118,5 +118,14 @@
2.4 final boolean reached(Player p) {
2.5 return p.getYInternal() == finalLine;
2.6 }
2.7 +
2.8 + static Direction valueOf(char ch) throws IllegalPositionException {
2.9 + for (Direction direction : values()) {
2.10 + if (direction.name().charAt(0) == ch) {
2.11 + return direction;
2.12 + }
2.13 + }
2.14 + throw new IllegalPositionException("No direction " + ch);
2.15 + }
2.16 }
2.17 }
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/MoveTest.java Sun Jul 12 13:35:58 2009 +0200
3.3 @@ -0,0 +1,92 @@
3.4 +/*
3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3.6 + *
3.7 + * The contents of this file are subject to the terms of either the GNU
3.8 + * General Public License Version 2 only ("GPL") or the Common
3.9 + * Development and Distribution License("CDDL") (collectively, the
3.10 + * "License"). You may not use this file except in compliance with the
3.11 + * License. You can obtain a copy of the License at
3.12 + * http://www.netbeans.org/cddl-gplv2.html
3.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
3.14 + * specific language governing permissions and limitations under the
3.15 + * License. When distributing the software, include this License Header
3.16 + * Notice in each file and include the License file at
3.17 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
3.18 + * particular file as subject to the "Classpath" exception as provided
3.19 + * by Sun in the GPL Version 2 section of the License file that
3.20 + * accompanied this code. If applicable, add the following below the
3.21 + * License Header, with the fields enclosed by brackets [] replaced by
3.22 + * your own identifying information:
3.23 + * "Portions Copyrighted [year] [name of copyright owner]"
3.24 + *
3.25 + * Contributor(s):
3.26 + *
3.27 + * Portions Copyrighted 2009 Jaroslav Tulach
3.28 + */
3.29 +
3.30 +package cz.xelfi.quoridor;
3.31 +
3.32 +import cz.xelfi.quoridor.Fence.Orientation;
3.33 +import cz.xelfi.quoridor.Player.Direction;
3.34 +import org.junit.After;
3.35 +import org.junit.AfterClass;
3.36 +import org.junit.Before;
3.37 +import org.junit.BeforeClass;
3.38 +import org.junit.Test;
3.39 +import static org.junit.Assert.*;
3.40 +
3.41 +/**
3.42 + *
3.43 + * @author Jaroslav Tulach <jtulach@netbeans.org>
3.44 + */
3.45 +public class MoveTest {
3.46 +
3.47 + public MoveTest() {
3.48 + }
3.49 +
3.50 + @Test
3.51 + public void testNorth() throws Exception {
3.52 + checkValueOf("N", Move.NORTH);
3.53 + }
3.54 + @Test
3.55 + public void testSouth() throws Exception {
3.56 + checkValueOf("S", Move.SOUTH);
3.57 + }
3.58 + @Test
3.59 + public void testEast() throws Exception {
3.60 + checkValueOf("E", Move.EAST);
3.61 + }
3.62 + @Test
3.63 + public void testWest() throws Exception {
3.64 + checkValueOf("W", Move.WEST);
3.65 + }
3.66 + @Test
3.67 + public void testJumps() throws Exception {
3.68 + for (Direction d1 : Direction.values()) {
3.69 + for (Direction d2 : Direction.values()) {
3.70 + checkValueOf("" + d1.name().charAt(0) + d2.name().charAt(0), Move.jump(d1, d2));
3.71 + }
3.72 + }
3.73 + }
3.74 + @Test
3.75 + public void testFences() throws Exception {
3.76 + for (int i = 1; i <= 8; i++) {
3.77 + for (int j = 0; i < 8; i++) {
3.78 + String h = "H" + (char)('A'+ j) + i;
3.79 + String v = "V" + (char)('A'+ j) + i;
3.80 +
3.81 + Move hm = Move.fence((char) ('A' + j), i, Orientation.HORIZONTAL);
3.82 + Move vm = Move.fence((char) ('A' + j), i, Orientation.VERTICAL);
3.83 +
3.84 + checkValueOf(h, hm);
3.85 + checkValueOf(v, vm);
3.86 + }
3.87 + }
3.88 + }
3.89 +
3.90 + private static void checkValueOf(String txt, Move move) throws Exception {
3.91 + assertEquals("toString", txt, move.toString());
3.92 + assertEquals("valueOf", move, Move.valueOf(txt));
3.93 + }
3.94 +
3.95 +}
3.96 \ No newline at end of file