private TerritoryIDType GetBestAttackTerritory(List <TerritoryIDType> immediateTakeTargets, List <TerritoryIDType> allTakeTargets, TerritoryIDType attackingTerritory, MultiMoves presentMoves) { List <TerritoryIDType> borderingTargets = immediateTakeTargets.Where(t => MapInformer.GetNeighborTerritories(t).Contains(attackingTerritory)).ToList(); // We prefer territories with a as little neighboring territories in the takeTargets as possible var bestAttackTerritory = borderingTargets.First(); foreach (var testAttackTerritory in borderingTargets) { var bestAttackTerritoryNeighborCount = MapInformer.GetNeighborTerritories(bestAttackTerritory).Where(t => allTakeTargets.Contains(t)).Count(); var testAttackTerritoryNeighborCount = MapInformer.GetNeighborTerritories(testAttackTerritory).Where(t => allTakeTargets.Contains(t)).Count(); if (testAttackTerritoryNeighborCount < bestAttackTerritoryNeighborCount) { bestAttackTerritory = testAttackTerritory; } else if (testAttackTerritoryNeighborCount == bestAttackTerritoryNeighborCount) { // If both have the same amount we prefer territories with as little unowned neighbors as possible var bestUnownedNeighborCount = MapInformer.GetNonOwnedNeighborTerritories(bestAttackTerritory, presentMoves.GetTerritoryStandingsAfterAllMoves()).Count(); var testUnownedNeighborCount = MapInformer.GetNonOwnedNeighborTerritories(testAttackTerritory, presentMoves.GetTerritoryStandingsAfterAllMoves()).Count(); if (testUnownedNeighborCount < bestUnownedNeighborCount) { bestAttackTerritory = testAttackTerritory; } } } return(bestAttackTerritory); }
// TODO returns no results private Dictionary <BonusIDType, BonusDetails> GetApplicableBonuses(MultiMoves presentMoves) { var territoryStandings = presentMoves.GetTerritoryStandingsAfterAllMoves(); Dictionary <BonusIDType, BonusDetails> allBonuses = GameState.Map.Bonuses; Dictionary <BonusIDType, BonusDetails> applicableBonuses = new Dictionary <BonusIDType, BonusDetails>(); Dictionary <BonusIDType, BonusDetails> ownedBonuses = MapInformer.GetOwnedBonuses(territoryStandings, GameState.MyPlayerId); foreach (BonusDetails bonus in allBonuses.Values) { if (ownedBonuses.Keys.Contains(bonus.ID)) { continue; } List <TerritoryIDType> bonusTerritoriesAndNeighbors = MapInformer.GetBonusTerritoriesAndNeighbors(bonus); foreach (var territoryId in bonusTerritoriesAndNeighbors) { if (territoryStandings[territoryId].OwnerPlayerID == GameState.MyPlayerId) { applicableBonuses.Add(bonus.ID, bonus); break; } } } return(applicableBonuses); }
private TerritoryIDType GetBestNeighborToAttack(List <TerritoryIDType> possibleTargets) { var bestTarget = GameState.CurrentTurn().LatestTurnStanding.Territories[possibleTargets.First()]; foreach (TerritoryIDType territoryId in possibleTargets) { var testTarget = GameState.CurrentTurn().LatestTurnStanding.Territories[territoryId]; if (bestTarget.IsNeutral && testTarget.OwnerPlayerID == GameState.OpponentPlayerId) { testTarget = bestTarget; } else if (bestTarget.IsNeutral && testTarget.IsNeutral && testTarget.NumArmies.ArmiesOrZero < bestTarget.NumArmies.ArmiesOrZero) { testTarget = bestTarget; } else if (bestTarget.OwnerPlayerID == GameState.OpponentPlayerId && testTarget.OwnerPlayerID == GameState.OpponentPlayerId) { var testTargetBonus = MapInformer.GetBonus(testTarget.ID); var bestTargetBonus = MapInformer.GetBonus(bestTarget.ID); var opponentBonuses = MapInformer.GetOwnedBonuses(GameState.CurrentTurn().LatestTurnStanding.Territories, GameState.OpponentPlayerId); if (opponentBonuses.Keys.Contains(testTargetBonus.ID) && !opponentBonuses.Keys.Contains(bestTargetBonus.ID)) { testTarget = bestTarget; } else if (opponentBonuses.Keys.Contains(testTargetBonus.ID) && opponentBonuses.Keys.Contains(bestTargetBonus.ID) && opponentBonuses[testTargetBonus.ID].Amount > opponentBonuses[bestTargetBonus.ID].Amount) { testTarget = bestTarget; } } } return(bestTarget.ID); }
private MultiMoves GetBreakBonusMoves(BonusIDType opponentBonus, MultiMoves presentMoves) { var bonusTerritories = GameState.Map.Bonuses[opponentBonus].Territories; var distances = MapInformer.GetDistancesFromTerritories(bonusTerritories); var ownedBorderTerritories = MapInformer.GetOwnedBorderTerritories(presentMoves.GetTerritoryStandingsAfterAllMoves(), GameState.MyPlayerId); int minDistance = ownedBorderTerritories.Min(o => distances[o.ID]); var bestStartTerritory = ownedBorderTerritories.Where(o => distances[o.ID] == minDistance).First(); int currentDistance = minDistance; var currentTerritoryInAttackPath = bestStartTerritory.ID; List <TerritoryIDType> territoriesToTake = new List <TerritoryIDType>(); while (currentDistance != 0) { var neighbors = MapInformer.GetNeighborTerritories(currentTerritoryInAttackPath); var possibleAttackNeighbors = neighbors.Where(n => distances[n] == currentDistance - 1).ToList(); var bestNeighborToAttack = GetBestNeighborToAttack(possibleAttackNeighbors); territoriesToTake.Add(bestNeighborToAttack); currentTerritoryInAttackPath = bestNeighborToAttack; currentDistance--; } TakeTerritoriesTask takeTerritoriesTask = new TakeTerritoriesTask(REASON); MultiMoves resultMoves = takeTerritoriesTask.CalculateTakeTerritoriesMoves(territoriesToTake, presentMoves); return(resultMoves); }
private int GetOwnedTerritoriesValue(PlayerIDType playerId) { var ownedTerritories = MapInformer.GetOwnedTerritories(FinalStandings.Values.ToList(), playerId); int amountOwned = ownedTerritories.Count(); int value = amountOwned * TERRITORIES_MULTIPLICATOR; return(value); }
private int GetNonOwnedNeighborTerritoryCount (TerritoryIDType territory, List <TerritoryIDType> territoryRange, Dictionary <TerritoryIDType, TerritoryStanding> territoryStandings) { var nonOwnedNeighborTerritories = MapInformer.GetNonOwnedNeighborTerritories(territory, territoryStandings).Keys.ToList(); nonOwnedNeighborTerritories.RemoveWhere(t => !territoryRange.Contains(t)); return(nonOwnedNeighborTerritories.Count()); }
private TerritoryIDType GetBestAttackingTerritory(List <TerritoryIDType> takeTargets, MultiMoves presentMoves) { var currentGameStandings = presentMoves.GetTerritoryStandingsAfterAllMoves(); var ownedTerritories = MapInformer.GetOwnedTerritories(currentGameStandings.Values.ToList(), GameState.MyPlayerId); var possibleStartTerritories = new List <TerritoryStanding>(); foreach (var ownedTerritory in ownedTerritories) { List <TerritoryIDType> neighbors = MapInformer.GetNeighborTerritories(ownedTerritory.ID); var matchingNeighbors = neighbors.Where(n => takeTargets.Contains(n)).ToList(); if (matchingNeighbors.Count > 0) { possibleStartTerritories.Add(ownedTerritory); } } var bestStartTerritory = possibleStartTerritories.First(); var beginTurnStandings = GameState.CurrentTurn().LatestTurnStanding.Territories; foreach (var testTerritory in possibleStartTerritories) { bool bestTerritoryOwnedByMyself = beginTurnStandings[bestStartTerritory.ID].OwnerPlayerID == GameState.MyPlayerId; bool testTerritoryOwnedByMyself = beginTurnStandings[testTerritory.ID].OwnerPlayerID == GameState.MyPlayerId; // if one of them has no neighbors to take prefer the other one bool bestTerritoryCanContinue = MapInformer.GetNeighborTerritories(bestStartTerritory.ID).Where(n => takeTargets.Contains(n)).Count() > 0; bool testTerritoryCanContinue = MapInformer.GetNeighborTerritories(testTerritory.ID).Where(n => takeTargets.Contains(n)).Count() > 0; if (testTerritoryCanContinue && !bestTerritoryCanContinue) { bestStartTerritory = testTerritory; } // if both are already owned by myself at the beginning of the round prefer the one with more armies on it else if (bestTerritoryOwnedByMyself && testTerritoryOwnedByMyself) { if (testTerritory.NumArmies.ArmiesOrZero > bestStartTerritory.NumArmies.ArmiesOrZero) { bestStartTerritory = testTerritory; } } // if only one of them is owned by myself at the beginning of the round prefer the one not owned else if (bestTerritoryOwnedByMyself && !testTerritoryOwnedByMyself) { bestStartTerritory = testTerritory; } // if both aren't owned by myself at the beginning of the round prefer the one with the longer attack path else if (!bestTerritoryOwnedByMyself && !testTerritoryOwnedByMyself) { var bestPathCount = presentMoves.GetMovePath(bestStartTerritory.ID).Count(); var testPathCount = presentMoves.GetMovePath(testTerritory.ID).Count(); if (testPathCount > bestPathCount) { bestStartTerritory = testTerritory; } } } return(bestStartTerritory.ID); }
private void AddBorderTerritoryDeployment(MultiMoves movesSoFar, int availableDeployment) { TerritoryStanding territoryToDeployTo = MapInformer.GetOwnedBorderTerritories(movesSoFar.GetTerritoryStandingsAfterAllMoves(), GameState.MyPlayerId).FirstOrDefault(); if (territoryToDeployTo == null) { territoryToDeployTo = MapInformer.GetOwnedTerritories(GameState.CurrentTurn().LatestTurnStanding.Territories.Values.ToList(), GameState.MyPlayerId).First(); } movesSoFar.AddDeployOrder(GameOrderDeploy.Create(GameState.MyPlayerId, availableDeployment, territoryToDeployTo.ID, REASON)); }
private int GetOwnedBonusesValue(PlayerIDType playerId) { Dictionary <BonusIDType, BonusDetails> ownedBonuses = MapInformer.GetOwnedBonuses(FinalStandings, playerId); int value = 0; ownedBonuses.ForEach(b => value += GetOwnedBonusValue(b.Value, playerId)); return(value); }
private int GetOwnedArmiesValue(PlayerIDType playerId) { var ownedTerritories = MapInformer.GetOwnedTerritories(FinalStandings.Values.ToList(), playerId); int amountArmiesOwned = 0; ownedTerritories.ForEach(o => amountArmiesOwned += o.NumArmies.ArmiesOrZero); int value = amountArmiesOwned * ARMIES_MULTIPLICATOR; return(value); }
/* * private int GetAverageTerritoryDistance() * { * var opponentTerritories = MapInformer.GetOwnedTerritories(FinalStandings.Values.ToList(), GameState.OpponentPlayerId); * var opponentTerritoryIds = new List<TerritoryIDType>(); * opponentTerritories.ForEach(t => opponentTerritoryIds.Add(t.ID)); * var distances = MapInformer.GetDistancesFromTerritories(opponentTerritoryIds); * int territoryCount = 0; * int allDistances = 0; * foreach (int distance in distances.Values) * { * if (distance > 0) * { * territoryCount++; * allDistances += distance; * } * } * return allDistances / territoryCount; * } */ private int GetBonusDistanceMultiplicator(BonusDetails bonus) { var opponentTerritories = MapInformer.GetOwnedTerritories(FinalStandings.Values.ToList(), GameState.OpponentPlayerId); var opponentTerritoryIds = new List <TerritoryIDType>(); opponentTerritories.ForEach(t => opponentTerritoryIds.Add(t.ID)); if (distancesCache == null) { distancesCache = MapInformer.GetDistancesFromTerritories(opponentTerritoryIds); } int territoryCount = 0; int allDistances = 0; int bonusTerritoryCount = 0; int bonusTerritoryDistances = 0; foreach (var territory in distancesCache.Keys) { var distance = distancesCache[territory]; if (distance > 0) { territoryCount++; allDistances += distance; } if (bonus.Territories.Contains(territory)) { bonusTerritoryCount++; bonusTerritoryDistances += distancesCache[territory]; } } int averageDistance; if (territoryCount != 0) { averageDistance = allDistances / territoryCount; } else { averageDistance = 0; } int bonusAverageDistance = bonusTerritoryDistances / bonusTerritoryCount; int difference = bonusAverageDistance - averageDistance; int multiplicator; if (bonusAverageDistance >= 0) { multiplicator = Math.Min(difference, MAX_BONUS_DISTANCE_ADJUSTMENT); } else { multiplicator = Math.Max(difference, -MAX_BONUS_DISTANCE_ADJUSTMENT); } return(multiplicator + 100); }
private int GetClassification(GameOrderAttackTransfer attackMove) { var initialState = GameState.CurrentTurn().LatestTurnStanding.Territories; bool opponentAttack = initialState[attackMove.To].OwnerPlayerID == GameState.OpponentPlayerId; var attackFromNeighbors = MapInformer.GetNeighborTerritories(initialState[attackMove.From].ID); var opponentTerritories = MapInformer.GetOwnedTerritories(initialState.Values.ToList(), GameState.OpponentPlayerId); var opponentNeighboringAttack = false; foreach (var opponentTerritory in opponentTerritories) { if (attackFromNeighbors.Contains(opponentTerritory.ID)) { opponentNeighboringAttack = true; } } bool clearOpponentBreak = false; int opponentIncome = GameState.CurrentTurn().Incomes[GameState.OpponentPlayerId].FreeArmies; int maxOpponentArmies = 0; if (opponentAttack) { maxOpponentArmies = initialState[attackMove.To].NumArmies.ArmiesOrZero + opponentIncome; int maxNeededArmies = AttackInformer.GetNeededBreakArmies(maxOpponentArmies); if (attackMove.NumArmies.ArmiesOrZero >= maxNeededArmies) { clearOpponentBreak = true; } } bool isTransfer = initialState[attackMove.To].OwnerPlayerID == GameState.MyPlayerId; bool isExpansion = initialState[attackMove.To].IsNeutral; if (clearOpponentBreak) { return(1); } if (isTransfer) { return(2); } if (isExpansion && !opponentNeighboringAttack) { return(3); } if (isExpansion && opponentNeighboringAttack) { return(4); } // non clear opponent attacks return(5); }
public List <MultiMoves> CalculateBreakBonusMultiTask(MultiMoves presentMoves) { List <MultiMoves> resultMoves = new List <MultiMoves>(); var opponentBonuses = MapInformer.GetOwnedBonuses(presentMoves.GetTerritoryStandingsAfterAllMoves(), GameState.OpponentPlayerId); foreach (var opponentBonus in opponentBonuses) { MultiMoves breakBonusMoves = GetBreakBonusMoves(opponentBonus.Key, presentMoves); if (breakBonusMoves != null) { resultMoves.Add(breakBonusMoves); } } return(resultMoves); }
private void ScheduleDeployMoves(MultiMoves multiMoves) { var deployMoves = multiMoves.DeployMoves; deployMoves = deployMoves.OrderBy(d => d.DeployOn.GetValue()).ToList(); var enemyTerritories = MapInformer.GetOwnedTerritories(GameState.CurrentTurn().LatestTurnStanding.Territories.Values.ToList(), GameState.OpponentPlayerId); var enemyTerritoryIds = new List <TerritoryIDType>(); enemyTerritories.ForEach(t => enemyTerritoryIds.Add(t.ID)); var distances = MapInformer.GetDistancesFromTerritories(enemyTerritoryIds); var opponentNeighboringDeployMoves = deployMoves.Where(d => distances[d.DeployOn] == 1); var nonOpponentNeighboringDeployMoves = deployMoves.Where(d => distances[d.DeployOn] > 1); multiMoves.DeployMoves.Clear(); multiMoves.DeployMoves.AddRange(opponentNeighboringDeployMoves); multiMoves.DeployMoves.AddRange(nonOpponentNeighboringDeployMoves); }
private List <TerritoryIDType> GetNonOwnedBonusTerritoriesToTake(MultiMoves multiMoves) { // Choose an arbitrary territory with a non owned neighbor TerritoryIDType nonOwnedTerritory = new TerritoryIDType(); foreach (var territory in multiMoves.GetTerritoryStandingsAfterAllMoves().Values.Where(t => t.OwnerPlayerID == GameState.MyPlayerId)) { var nonOwnedNeighbors = MapInformer.GetNonOwnedNeighborTerritories(territory.ID, multiMoves.GetTerritoryStandingsAfterAllMoves()); if (nonOwnedNeighbors.Count > 0) { nonOwnedTerritory = nonOwnedNeighbors.First().Key; break; } } var bonus = MapInformer.GetBonus(nonOwnedTerritory); AILog.Log("Debug", "Attempting to take bonus: " + bonus.Name); var nonOwnedBonusTerritories = MapInformer.GetNonOwnedBonusTerritories(bonus, multiMoves.GetTerritoryStandingsAfterAllMoves()); return(nonOwnedBonusTerritories); }
private TerritoryIDType GetBestOwnTerritoryToMakeAttack(Dictionary <TerritoryIDType, TerritoryStanding> ownedNeighbors, TerritoryIDType territoryToTake, MultiMoves currentMoves) { TerritoryIDType bestNeighbor = ownedNeighbors.First().Key; foreach (TerritoryIDType territoryId in ownedNeighbors.Keys) { var attackMoves = currentMoves.AttackMoves.Where(o => o.From == bestNeighbor && o.To == territoryId).ToList(); if (attackMoves.Count > 0) { bestNeighbor = attackMoves.First().To; } } // if we have all neighbors already choose the one with the max armies var ownedTerritories = MapInformer.GetOwnedTerritories(GameState.CurrentTurn().LatestTurnStanding.Territories.Values.ToList(), GameState.MyPlayerId); bool allNeighborsOwnedAtStart = true; foreach (TerritoryIDType ownedNeighbor in ownedNeighbors.Keys) { if (ownedTerritories.Where(o => o.ID == ownedNeighbor).Count() == 0) { allNeighborsOwnedAtStart = false; break; } } if (allNeighborsOwnedAtStart) { var territoryStandings = currentMoves.GetTerritoryStandingsAfterAllMoves(); foreach (TerritoryIDType ownedNeighbor in ownedNeighbors.Keys) { if (territoryStandings[ownedNeighbor].NumArmies.ArmiesOrZero > territoryStandings[bestNeighbor].NumArmies.ArmiesOrZero) { bestNeighbor = ownedNeighbor; } } } return(bestNeighbor); }
private List <TerritoryIDType> FilterNonBorderingTerritories(List <TerritoryIDType> territoriesToTake, MultiMoves presentMoves) { var standings = presentMoves.GetTerritoryStandingsAfterAllMoves(); return(territoriesToTake.Where(t => MapInformer.GetOwnedNeighborTerritories(t, standings).Count > 0).ToList()); }