Example #1
0
  public int isReachable(FE fromField, FieldElement target, FieldElement.Neighbor[] neighborTypes) {
    if (fromField.equals(target)) {
      return 0;
    }
    if (neighborTypes == null || neighborTypes.length == 0) {
      return -1;
    }
    // clear pathfinding data
    for (FE e : this) {
      e.mPathfindingValue = 0;
    }
    mPathfindingNodes.clear();

    // ant algorithm, start with fromField
    mPathfindingNodes.add(fromField);
    fromField.mPathfindingValue = 1;
    do {
      FieldElement currField = mPathfindingNodes.poll();
      for (FieldElement.Neighbor n : neighborTypes) {

        if (hasNeighbor(currField, n)) {
          FE nextField = getNeighbor(currField, n);
          if (nextField.equals(target)) {
            return currField.mPathfindingValue;
          }
          if (nextField.mPathfindingValue == 0 && !nextField.isBlocked()) {
            // field not yet reached and
            // the next field is not blocked, go on
            mPathfindingNodes.add(nextField);
            nextField.mPathfindingValue = currField.mPathfindingValue + 1;
          }
        }
      }
    } while (!mPathfindingNodes.isEmpty());
    return -1;
  }