equality of moves, players report positions from 0-8 and better toString
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 06 Jun 2009 14:48:02 +0200
changeset 30f25846453907
parent 29 e5e40fd406c1
child 31 8e6a6857c7b5
equality of moves, players report positions from 0-8 and better toString
quoridor/src/main/java/cz/xelfi/quoridor/Fence.java
quoridor/src/main/java/cz/xelfi/quoridor/Move.java
quoridor/src/main/java/cz/xelfi/quoridor/Player.java
quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java
     1.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Fence.java	Sat Jun 06 11:55:18 2009 +0200
     1.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Fence.java	Sat Jun 06 14:48:02 2009 +0200
     1.3 @@ -112,7 +112,7 @@
     1.4  
     1.5      @Override
     1.6      public String toString() {
     1.7 -        return "Fence[" + x + "," + y + "," + o + "]";
     1.8 +        return "Fence[" + getColumn() + "," + getRow() + "," + o + "]";
     1.9      }
    1.10  
    1.11      @Override
     2.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Move.java	Sat Jun 06 11:55:18 2009 +0200
     2.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Move.java	Sat Jun 06 14:48:02 2009 +0200
     2.3 @@ -27,6 +27,7 @@
     2.4  package cz.xelfi.quoridor;
     2.5  
     2.6  import cz.xelfi.quoridor.Player.Direction;
     2.7 +import java.util.Arrays;
     2.8  
     2.9  /** Encapsulates one possible move that can be applied to existing {@link Board}
    2.10   * by calling its {@link Board#apply} method.
    2.11 @@ -108,4 +109,41 @@
    2.12      public static Move jump(Direction first, Direction second) {
    2.13          return new Move(first, second);
    2.14      }
    2.15 +
    2.16 +    @Override
    2.17 +    public String toString() {
    2.18 +        if (fence != null) {
    2.19 +            return "Move[" + fence + "]";
    2.20 +        } else {
    2.21 +            return "Move" + Arrays.toString(direction);
    2.22 +        }
    2.23 +    }
    2.24 +
    2.25 +    @Override
    2.26 +    public boolean equals(Object obj) {
    2.27 +        if (obj == null) {
    2.28 +            return false;
    2.29 +        }
    2.30 +        if (getClass() != obj.getClass()) {
    2.31 +            return false;
    2.32 +        }
    2.33 +        final Move other = (Move) obj;
    2.34 +        if (!Arrays.deepEquals(this.direction, other.direction)) {
    2.35 +            return false;
    2.36 +        }
    2.37 +        if (this.fence != other.fence && (this.fence == null || !this.fence.equals(other.fence))) {
    2.38 +            return false;
    2.39 +        }
    2.40 +        return true;
    2.41 +    }
    2.42 +
    2.43 +    @Override
    2.44 +    public int hashCode() {
    2.45 +        int hash = 3;
    2.46 +        hash = 47 * hash + Arrays.deepHashCode(this.direction);
    2.47 +        hash = 47 * hash + (this.fence != null ? this.fence.hashCode() : 0);
    2.48 +        return hash;
    2.49 +    }
    2.50 +
    2.51 +
    2.52  }
     3.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Player.java	Sat Jun 06 11:55:18 2009 +0200
     3.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Player.java	Sat Jun 06 14:48:02 2009 +0200
     3.3 @@ -60,6 +60,21 @@
     3.4          return y;
     3.5      }
     3.6  
     3.7 +    /** Returns position of the player from 0-8.
     3.8 +     *
     3.9 +     * @return integer from 0-8
    3.10 +     */
    3.11 +    public int getColumn() {
    3.12 +        return x / 2;
    3.13 +    }
    3.14 +
    3.15 +    /** The y-position of the player.
    3.16 +     * @return integer from 0-8
    3.17 +     */
    3.18 +    public int getRow() {
    3.19 +        return y / 2;
    3.20 +    }
    3.21 +
    3.22      /** How much fences is still available for the player.
    3.23       * 
    3.24       * @return number of fences this player still has
     4.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Sat Jun 06 11:55:18 2009 +0200
     4.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Sat Jun 06 14:48:02 2009 +0200
     4.3 @@ -26,6 +26,7 @@
     4.4  package cz.xelfi.quoridor;
     4.5  
     4.6  import cz.xelfi.quoridor.Fence.Orientation;
     4.7 +import cz.xelfi.quoridor.Player.Direction;
     4.8  import java.util.List;
     4.9  import junit.framework.TestCase;
    4.10  
    4.11 @@ -365,4 +366,36 @@
    4.12          }
    4.13  
    4.14      }
    4.15 +
    4.16 +    public void testEqualityOfMoves() throws Exception {
    4.17 +
    4.18 +        for (Direction m1 : Direction.values()) {
    4.19 +            for (Direction m2 : Direction.values()) {
    4.20 +                Move both1 = Move.jump(m1, m2);
    4.21 +                Move both2 = Move.jump(m1, m2);
    4.22 +                Move opposite = Move.jump(m2, m1);
    4.23 +
    4.24 +                assertTrue("Both boths are equal", both1.equals(both2));
    4.25 +                assertFalse("Not north", Move.NORTH.equals(both1));
    4.26 +                assertFalse("Not east", Move.EAST.equals(both1));
    4.27 +                assertFalse("Not west", Move.WEST.equals(both1));
    4.28 +                assertFalse("Not south", Move.SOUTH.equals(both1));
    4.29 +                if (m1 == m2) {
    4.30 +                    continue;
    4.31 +                }
    4.32 +                assertFalse("Not equal to opposite", both1.equals(opposite));
    4.33 +            }
    4.34 +        }
    4.35 +
    4.36 +        Move f1 = Move.fence('D', 6, Orientation.HORIZONTAL);
    4.37 +        Move f2 = Move.fence('D', 6, Orientation.HORIZONTAL);
    4.38 +        Move f3 = Move.fence('D', 6, Orientation.VERTICAL);
    4.39 +        Move f4 = Move.fence('E', 6, Orientation.VERTICAL);
    4.40 +        Move f5 = Move.fence('E', 5, Orientation.VERTICAL);
    4.41 +
    4.42 +        assertEquals(f1, f2);
    4.43 +        assertFalse(f1.equals(f3));
    4.44 +        assertFalse(f4.equals(f5));
    4.45 +        assertFalse(f3.equals(f5));
    4.46 +    }
    4.47  }