示例#1
0
  // TODO
  public boolean requestMoveUnit(UnitConcrete unit, Territory source, Territory destination) {
    // First do some validation to ensure that the unit can physically make the move.
    if (!source.getNeighbors().contains(destination)) {
      return false;
    }
    if (unit.getMovementPoints() <= 0) {
      return false;
    }

    // LandUnit -> Land or SeaUnit -> Sea
    if (unit.getProfile() instanceof LandUnit && !destination.isSeaZone()
        || unit.getProfile() instanceof SeaUnit && destination.isSeaZone()) {
      source.removeUnit(activePower, unit);
      destination.addUnit(activePower, unit);
      unit.setTerritorySource(destination);
      unit.setMovementPoints(unit.getMovementPoints() - 1);
      if (unit.getProfile() instanceof Transport || unit.getProfile() instanceof AircraftCarrier) {
        for (UnitConcrete cargo : unit.getCargo()) {
          source.removeUnit(activePower, cargo);
          destination.addUnit(activePower, cargo);
          cargo.setTerritorySource(destination);
        }
      }
      return true;
    }
    // LandUnit -> Sea (transport)
    else if (unit.getProfile() instanceof LandUnit
        && destination.isSeaZone()
        && !source.isSeaZone()) {
      ArrayList<UnitConcrete> transports =
          destination.getUnitsStationed(activePower).getUnitsByName(UnitName.TRANSPORT);
      for (UnitConcrete t : transports) {
        if (t.tryAddCargo(unit)) {
          source.removeUnit(activePower, unit);
          destination.addUnit(activePower, unit);
          unit.setTerritorySource(destination);
          unit.setMovementPoints(1); // All transported units can load-unload in same turn.
          return true;
        }
      }
    }
    // AirUnit
    else if (unit.getProfile() instanceof AirUnit) {
      if (checkAirUnitHasReturnPath(unit, destination)) {
        source.removeUnit(activePower, unit);
        destination.addUnit(activePower, unit);
        unit.setTerritorySource(destination);
        unit.setMovementPoints(unit.getMovementPoints() - 1);
        return true;
      }
    }
    // SeaUnit -> Land (impossible)
    else if (unit.getProfile() instanceof SeaUnit && !destination.isSeaZone()) {
      return false; // Boats can never go on land.
    }
    return true;
  }
示例#2
0
 public HashSet<Territory> getTerritoriesInRange(Territory source, int movementPoints) {
   HashSet<Territory> territories = new HashSet<Territory>();
   territories.add(source);
   if (movementPoints > 0) {
     for (Territory neighbor : source.getNeighbors()) {
       territories.addAll(getTerritoriesInRange(neighbor, movementPoints - 1));
     }
   }
   return territories;
 }