quoridor/src/main/java/cz/xelfi/quoridor/Board.java
changeset 253 ee02205edf13
parent 237 38db4aae19d9
child 264 d60370059c3c
     1.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Thu Apr 15 23:43:25 2010 +0200
     1.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Sun Sep 05 11:04:26 2010 +0200
     1.3 @@ -41,6 +41,8 @@
     1.4  import java.util.List;
     1.5  import java.util.Set;
     1.6  import java.util.TreeSet;
     1.7 +import java.util.logging.Level;
     1.8 +import java.util.logging.Logger;
     1.9  import java.util.regex.Matcher;
    1.10  import java.util.regex.Pattern;
    1.11  
    1.12 @@ -207,17 +209,53 @@
    1.13          if (getWinner() != null) {
    1.14              throw new IllegalPositionException("Game finished!"); // NOI18N
    1.15          }
    1.16 +        return apply(move, getCurrentPlayer());
    1.17 +    }
    1.18  
    1.19 +    private Board apply(Move move, Player p) throws IllegalPositionException {
    1.20          if (move.direction != null) {
    1.21 -            return move(getCurrentPlayer(), move.direction);
    1.22 +            return move(p, move.direction);
    1.23          } else {
    1.24              if (move.fence != null) {
    1.25 -                return fence(getCurrentPlayer(), move.fence);
    1.26 +                return fence(p, move.fence);
    1.27              } else {
    1.28                  return new Board(this, turn + 1);
    1.29              }
    1.30          }
    1.31      }
    1.32 +    
    1.33 +    /** Is there a move that would take the player onto given position while
    1.34 +     * respecting situation on board?
    1.35 +     * 
    1.36 +     * @param p the player to try to move
    1.37 +     * @param column desired x-coordinate. Between 0-8. See {@link Player#getColumn()}.
    1.38 +     * @param row desired y-coordinate. Between 0-8. See {@link Player#getRow()}.
    1.39 +     * @return the move that can be {@link #apply applied} to the position and
    1.40 +     *   that would move the player p into desired position or null, if such 
    1.41 +     *   move does not exist
    1.42 +     * @since 1.4
    1.43 +     */
    1.44 +    public Move findMove(Player p, int column, int row) {
    1.45 +        int idx = getPlayers().indexOf(p);
    1.46 +        if (idx < 0) {
    1.47 +            return null;
    1.48 +        }
    1.49 +        for (Move m : Move.allMovements()) {
    1.50 +            Board b;
    1.51 +            try {
    1.52 +                b = apply(m, p);
    1.53 +            } catch (IllegalPositionException ex) {
    1.54 +                continue;
    1.55 +            }
    1.56 +            if (
    1.57 +                b.getPlayers().get(idx).getColumn() == column &&
    1.58 +                b.getPlayers().get(idx).getRow() == row
    1.59 +            ) {
    1.60 +                return m;
    1.61 +            }
    1.62 +        }
    1.63 +        return null;
    1.64 +    }
    1.65  
    1.66      /** Can the move be applied to current board position?
    1.67       *