Ejemplo n.º 1
0
  /**
   * Method: getMoveLocations() Usage: 3rd step in act() of Critter ---------------------------- if
   * Follower has a target, Follower moves to the location opposite to the target's direction if
   * target is null, Follower roams like a generic Critter until a target is found @Postcondition:
   * The state of all actors is unchanged.
   *
   * @return a list of possible locations for the next move
   */
  public ArrayList<Location> getMoveLocations() {
    ArrayList<Location> nextLoc = new ArrayList<Location>();

    if (target != null) {
      Location next =
          target.getLocation().getAdjacentLocation(target.getDirection() + Location.HALF_CIRCLE);
      if (getGrid().isValid(next)) nextLoc.add(next);
    } else {
      for (Location l : super.getMoveLocations()) // for every empty adjacent loc
      if (getGrid().isValid(l)) // if it's also valid
        nextLoc.add(l); // add it to list of possible locs
    }

    return nextLoc;
  }
  public boolean inDanger(Actor actor) // Method to check if piece is in danger
      {
    loc = actor.getLocation(); // Gets location of actor being checked
    neighbors = getGrid().getNeighbors(loc); // Gets all neighbors of this piece

    if (oppositeDirection
        >= 360) // Opposite direction adds 180 degrees to angle so since compass directions do not
    // exceed 359, this gets the equivalent angle if it does
    {
      oppositeDirection =
          oppositeDirection - 360; // Subtracts 360 degrees to find the coterminal angle
    }

    boolean output = false; // Default output response set to true
    for (counter = 0;
        counter < neighbors.size() - 1;
        counter++) // For loop using the external counter
    {

      if (neighbors.get(counter) instanceof WhiteKing) // If the neighboring
      {
        if (getGrid().get(oppositeDirectionLocation) == null) {
          output = true;
          return output;
        }
      }

      if (neighbors.get(counter) instanceof WhitePiece) {
        int pieceDirection = neighbors.get(counter).getDirection();
        if (pieceDirection == 0 || pieceDirection == 45 || pieceDirection == 315) {
          if (getGrid().get(oppositeDirectionLocation) == null) {
            output = true;
            return output;
          }
        }
      }
    }
    return output;
  }