Adding isApplicable method
authorJaroslav Tulach <jtulach@netbeans.org>
Mon, 11 May 2009 18:51:52 +0200
changeset 17c496a8660fa6
parent 16 2a9cbf22a8f1
child 18 a99cf2e77c27
Adding isApplicable method
quoridor/src/main/java/cz/xelfi/quoridor/Board.java
quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java
     1.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Mon May 11 18:47:39 2009 +0200
     1.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Mon May 11 18:51:52 2009 +0200
     1.3 @@ -195,6 +195,22 @@
     1.4          }
     1.5      }
     1.6  
     1.7 +    /** Can the move be applied to current board position?
     1.8 +     * 
     1.9 +     * @param move the move to apply
    1.10 +     * @return true if one can call {@link #apply} method without getting 
    1.11 +     *   an exception
    1.12 +     */
    1.13 +    public boolean isApplicable(Move move) {
    1.14 +        try {
    1.15 +            // trivial implementation is enough for now
    1.16 +            apply(move);
    1.17 +            return true;
    1.18 +        } catch (IllegalPositionException ex) {
    1.19 +            return false;
    1.20 +        }
    1.21 +    }
    1.22 +    
    1.23      /** Moves the player in given direction. The direction usually
    1.24       * is one of Player.Direction constants, but in case the move
    1.25       * is a jump over another players pawn it can be followed by
    1.26 @@ -711,5 +727,5 @@
    1.27          int baseOnY0 = max * (max + 1) / 2;
    1.28          return baseOnY0 + y;
    1.29      }
    1.30 -    
    1.31 +
    1.32  }
     2.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Mon May 11 18:47:39 2009 +0200
     2.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Mon May 11 18:51:52 2009 +0200
     2.3 @@ -319,6 +319,8 @@
     2.4          assertNotNull("There is a winner", fin.getWinner());
     2.5          assertEquals("And the winner is", fin.getPlayers().get(0), fin.getWinner());
     2.6  
     2.7 +        assertFalse("No next move can be applied", fin.isApplicable(Move.fence('D', 3, Fence.Orientation.HORIZONTAL)));
     2.8 +
     2.9          try {
    2.10              fin.apply(Move.EAST);
    2.11              fail("No moves allow when we are in final position");