コード例 #1
0
ファイル: AI.java プロジェクト: slauson/Tactics
  /**
   * Returns change in battle strength if unit at region switches places with neighbor.
   *
   * @param region
   * @param neighbor
   * @param includeNeighborStrengthChange true if change in neighbor strength should be included
   * @return
   */
  private static float getBattleStrengthChange(
      Region region, Neighbor neighbor, boolean includeNeighborStrengthChange) {
    // get current strengths of region, neighbor
    float regionStrength = RegionUtils.getRegionBattleStrength(region);
    float neighborStrength = RegionUtils.getRegionBattleStrength(neighbor.region);

    // swap units, see change in strengths
    Unit tempUnit = region.unit;
    region.unit = neighbor.region.unit;
    neighbor.region.unit = tempUnit;

    Player tempPlayer = region.player;
    region.player = neighbor.region.player;
    neighbor.region.player = tempPlayer;

    float regionMoveStrength = RegionUtils.getRegionBattleStrength(region);
    float neighborMoveStrength = RegionUtils.getRegionBattleStrength(neighbor.region);

    // put units back
    tempUnit = region.unit;
    region.unit = neighbor.region.unit;
    neighbor.region.unit = tempUnit;

    tempPlayer = region.player;
    region.player = neighbor.region.player;
    neighbor.region.player = tempPlayer;

    if (includeNeighborStrengthChange) {
      return (regionMoveStrength + neighborMoveStrength - regionStrength - neighborStrength + 2)
          / 4;
    } else {
      return (regionMoveStrength - regionStrength + 1) / 2;
    }
  }
コード例 #2
0
ファイル: AI.java プロジェクト: slauson/Tactics
  /**
   * Returns changes in region strengths for each possible move.
   *
   * @param region
   * @return
   */
  protected static Map<Neighbor, Float> checkMoves(Region region) {
    Map<Neighbor, Float> result = new HashMap<Neighbor, Float>();

    for (Neighbor neighbor : region.neighbors) {
      // check if neighbor can be moved to
      if (RegionUtils.canMove(region, neighbor)) {
        // get current strengths of region, neighbor
        float regionStrength = RegionUtils.getRegionBattleStrength(region);
        float neighborStrength = RegionUtils.getRegionBattleStrength(neighbor.region);

        // swap units, see change in strengths
        Unit tempUnit = region.unit;
        region.unit = neighbor.region.unit;
        neighbor.region.unit = tempUnit;

        Player tempPlayer = region.player;
        region.player = neighbor.region.player;
        neighbor.region.player = tempPlayer;

        float regionMoveStrength = RegionUtils.getRegionBattleStrength(region);
        float neighborMoveStrength = RegionUtils.getRegionBattleStrength(neighbor.region);

        // put units back
        tempUnit = region.unit;
        region.unit = neighbor.region.unit;
        neighbor.region.unit = tempUnit;

        tempPlayer = region.player;
        region.player = neighbor.region.player;
        neighbor.region.player = tempPlayer;

        result.put(
            neighbor,
            regionMoveStrength + neighborMoveStrength - regionStrength - neighborStrength);
      }
    }

    return result;
  }
コード例 #3
0
ファイル: AI.java プロジェクト: slauson/Tactics
  /**
   * Returns best reinforcement for given region.
   *
   * @param region
   * @return
   */
  protected static ReinforcementType getBestReinforcement(Region region) {

    ReinforcementType result = new ReinforcementType();

    // existing unit
    if (region.unit != null) {
      float originalRegionHealth = region.unit.health;
      float originalBattleStrength = RegionUtils.getRegionBattleStrength(region);

      // simulate max health
      region.unit.health = Unit.MAX_HEALTH;
      result.strengthChange = RegionUtils.getRegionBattleStrength(region) - originalBattleStrength;
      region.unit.health = originalRegionHealth;
    }
    // new unit
    else {

      // get optimal unit type
      // check each possible unit type
      for (Unit.Type unitType : Unit.Type.values()) {
        region.unit = new Unit(unitType, Unit.MAX_HEALTH);

        float regionBattleStrength = RegionUtils.getRegionBattleStrength(region);

        if (result.unitType == null || regionBattleStrength > result.strengthChange) {
          result.strengthChange = regionBattleStrength;
          result.unitType = unitType;
        }
      }

      // reset region to have no unit
      region.unit = null;
    }

    return result;
  }