# HG changeset patch # User Jaroslav Tulach # Date 1247398558 -7200 # Node ID 34baf57f2d4e941db3963063a7eeeca2c803b0d7 # Parent 6a6d1dbea99ef6c92dbeee2093af7a3d04c8e9a8 Adding support for reading Move's from strings diff -r 6a6d1dbea99e -r 34baf57f2d4e quoridor/src/main/java/cz/xelfi/quoridor/Move.java --- a/quoridor/src/main/java/cz/xelfi/quoridor/Move.java Sat Jun 06 15:13:04 2009 +0200 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Move.java Sun Jul 12 13:35:58 2009 +0200 @@ -113,12 +113,50 @@ return new Move(first, second); } + /** Converts the value of the move string into valid move, if possible. + * + * @param move the string in format used by {@link #toString()} + * @return the move which's toString() returns the move parameter + * @throws IllegalPositionException raised in case the parameter does not + * represent a valid move + */ + public static Move valueOf(String move) throws IllegalPositionException { + switch (move.length()) { + case 3: + if (move.startsWith("H")) { + return Move.fence(move.charAt(1), move.charAt(2) - '0', Fence.Orientation.HORIZONTAL); + } + if (move.startsWith("V")) { + return Move.fence(move.charAt(1), move.charAt(2) - '0', Fence.Orientation.VERTICAL); + } + break; + case 2: + return Move.jump(Direction.valueOf(move.charAt(0)), Direction.valueOf(move.charAt(1))); + case 1: + switch (move.charAt(0)) { + case 'N': return Move.NORTH; + case 'S': return Move.SOUTH; + case 'E': return Move.EAST; + case 'W': return Move.WEST; + } + } + throw new IllegalPositionException(move); + } + @Override public String toString() { if (fence != null) { - return "Move[" + fence + "]"; + switch (fence.getOrientation()) { + case HORIZONTAL: return "H" + fence.getColumn() + fence.getRow(); + case VERTICAL: return "V" + fence.getColumn() + fence.getRow(); + } + throw new IllegalStateException(); } else { - return "Move" + Arrays.toString(direction); + StringBuilder sb = new StringBuilder(); + for (Direction d : direction) { + sb.append(d.name().charAt(0)); + } + return sb.toString(); } } diff -r 6a6d1dbea99e -r 34baf57f2d4e quoridor/src/main/java/cz/xelfi/quoridor/Player.java --- a/quoridor/src/main/java/cz/xelfi/quoridor/Player.java Sat Jun 06 15:13:04 2009 +0200 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Player.java Sun Jul 12 13:35:58 2009 +0200 @@ -118,5 +118,14 @@ final boolean reached(Player p) { return p.getYInternal() == finalLine; } + + static Direction valueOf(char ch) throws IllegalPositionException { + for (Direction direction : values()) { + if (direction.name().charAt(0) == ch) { + return direction; + } + } + throw new IllegalPositionException("No direction " + ch); + } } } diff -r 6a6d1dbea99e -r 34baf57f2d4e quoridor/src/test/java/cz/xelfi/quoridor/MoveTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/MoveTest.java Sun Jul 12 13:35:58 2009 +0200 @@ -0,0 +1,92 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor; + +import cz.xelfi.quoridor.Fence.Orientation; +import cz.xelfi.quoridor.Player.Direction; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author Jaroslav Tulach + */ +public class MoveTest { + + public MoveTest() { + } + + @Test + public void testNorth() throws Exception { + checkValueOf("N", Move.NORTH); + } + @Test + public void testSouth() throws Exception { + checkValueOf("S", Move.SOUTH); + } + @Test + public void testEast() throws Exception { + checkValueOf("E", Move.EAST); + } + @Test + public void testWest() throws Exception { + checkValueOf("W", Move.WEST); + } + @Test + public void testJumps() throws Exception { + for (Direction d1 : Direction.values()) { + for (Direction d2 : Direction.values()) { + checkValueOf("" + d1.name().charAt(0) + d2.name().charAt(0), Move.jump(d1, d2)); + } + } + } + @Test + public void testFences() throws Exception { + for (int i = 1; i <= 8; i++) { + for (int j = 0; i < 8; i++) { + String h = "H" + (char)('A'+ j) + i; + String v = "V" + (char)('A'+ j) + i; + + Move hm = Move.fence((char) ('A' + j), i, Orientation.HORIZONTAL); + Move vm = Move.fence((char) ('A' + j), i, Orientation.VERTICAL); + + checkValueOf(h, hm); + checkValueOf(v, vm); + } + } + } + + private static void checkValueOf(String txt, Move move) throws Exception { + assertEquals("toString", txt, move.toString()); + assertEquals("valueOf", move, Move.valueOf(txt)); + } + +} \ No newline at end of file