public void RankRegionsByNeed_TwoRegions_ReturnsListSortedDesc() { var testRegion = _gameState.AddRegion(_myName, 6); var neighbor1 = _gameState.AddRegion(_enemyName, 5); testRegion.AddNeighbor(neighbor1); var neighbor2 = _gameState.AddRegion(_enemyName, 5); testRegion.AddNeighbor(neighbor2); var testRegion2 = _gameState.AddRegion(_myName, 6); var neighbor21 = _gameState.AddRegion(_enemyName, 1); testRegion2.AddNeighbor(neighbor21); var testRegion3 = _gameState.AddRegion(_myName, 6); var neighbor31 = _gameState.AddRegion(_enemyName, 6); testRegion3.AddNeighbor(neighbor31); var result = DefensiveArmiesNeeded.RankRegionsByNeed(_gameState.FullMap, _myName, 5); Assert.AreEqual(testRegion.Id, result[0].Item1.Id); Assert.AreEqual(testRegion3.Id, result[1].Item1.Id); Assert.AreEqual(testRegion2.Id, result[2].Item1.Id); }
public void ResolveConditionalAttacks(List <AttackTransferMove> committedAttackTransferMoves) { foreach (var attack in _conditionalAttacks) { var target = attack.Key; var attackerList = attack.Value; attackerList.Sort((a, b) => b.Item2.CompareTo(a.Item2)); Dictionary <int, int> remainingArmies = DefensiveArmiesNeeded.RemainingArmiesAfterFullScaleAssault(target.Armies, attackerList); if (ConditionalAttackIsSafe(target, attackerList, remainingArmies)) { CommitAttack(attack, remainingArmies.Keys.ToList(), committedAttackTransferMoves); } } }
public void For_NeedsAFewArmies_ReturnsNeededNumber() { var testRegion = _gameState.AddRegion(_myName, 6); var neighbor1 = _gameState.AddRegion(_enemyName, 5); testRegion.AddNeighbor(neighbor1); var neighbor2 = _gameState.AddRegion(_enemyName, 5); testRegion.AddNeighbor(neighbor2); var result = DefensiveArmiesNeeded.For(testRegion.Neighbors, testRegion.Armies, 5); Assert.AreEqual(4, result); }
public void For_AgainstLargeOdds_ReturnsIntMax() { var testRegion = _gameState.AddRegion(_myName, 50); var neighbor1 = _gameState.AddRegion(_enemyName, 100); testRegion.AddNeighbor(neighbor1); var neighbor2 = _gameState.AddRegion(_enemyName, 100); testRegion.AddNeighbor(neighbor2); var result = DefensiveArmiesNeeded.For(testRegion.Neighbors, testRegion.Armies, 2); Assert.AreEqual(int.MaxValue, result); }
private static int PlaceDefensiveArmies(int armiesLeft, GameState state, List <PlaceArmiesMove> placeArmiesMoves) { if (armiesLeft == 0) { return(armiesLeft); } var regionsByNeed = DefensiveArmiesNeeded.RankRegionsByNeed(state.VisibleMap, GameState.MyPlayerName, armiesLeft); for (int i = 0; armiesLeft > 0 && i < regionsByNeed.Count; i++) { if (armiesLeft >= regionsByNeed[i].Item2 && regionsByNeed[i].Item2 > 0) { placeArmiesMoves.Add(new PlaceArmiesMove(GameState.MyPlayerName, regionsByNeed[i].Item1, regionsByNeed[i].Item2)); armiesLeft -= regionsByNeed[i].Item2; } } return(armiesLeft); }
public void For_NeutralRegionIsStacked_IgnoresNeutralRegion() { var testRegion = _gameState.AddRegion(_myName, 6); var neighbor1 = _gameState.AddRegion(_enemyName, 5); testRegion.AddNeighbor(neighbor1); var neighbor2 = _gameState.AddRegion(_enemyName, 5); testRegion.AddNeighbor(neighbor2); var neighbor3 = _gameState.AddRegion(Constants.NeutralPlayerName, 100); testRegion.AddNeighbor(neighbor3); var result = DefensiveArmiesNeeded.For(testRegion.Neighbors, testRegion.Armies, 5); Assert.AreEqual(4, result); }
private bool ConditionalAttackIsSafe(Region target, List <Tuple <Region, int> > attackerList, Dictionary <int, int> remainingArmies) { bool attackIsSafe = true; var defenderArmiesRemaining = remainingArmies[-1]; foreach (var attacker in attackerList) { if (!attackIsSafe) { break; } var region = attacker.Item1; if (!remainingArmies.ContainsKey(region.Id)) { continue; } List <Region> unconqueredNeighbors; int armiesRemainingOnAttackingRegion = (attacker.Item1.Armies - attacker.Item2); if (defenderArmiesRemaining == 0) { unconqueredNeighbors = attacker.Item1.Neighbors.Where(x => x.PlayerName == GameState.GetOpponentPlayerName && x.Id != target.Id).ToList(); } else { unconqueredNeighbors = attacker.Item1.Neighbors.Where(x => x.PlayerName == GameState.GetOpponentPlayerName).ToList(); armiesRemainingOnAttackingRegion += remainingArmies[region.Id]; } var targetActualArmies = target.Armies; target.Armies = defenderArmiesRemaining; if (DefensiveArmiesNeeded.For(unconqueredNeighbors, armiesRemainingOnAttackingRegion, Constants.SafeNumberOfArmiesNeeded) > Constants.SafeNumberOfArmiesNeeded) { attackIsSafe = false; } target.Armies = targetActualArmies; } return(attackIsSafe); }
public void For_LargeArmies_ReturnsIntMax() { var testRegion = _gameState.AddRegion(_myName, 198); var neighbor1 = _gameState.AddRegion(_enemyName, 85); testRegion.AddNeighbor(neighbor1); var neighbor2 = _gameState.AddRegion(_enemyName, 110); testRegion.AddNeighbor(neighbor2); var neighbor3 = _gameState.AddRegion(_enemyName, 98); testRegion.AddNeighbor(neighbor3); var neighbor4 = _gameState.AddRegion(_enemyName, 148); testRegion.AddNeighbor(neighbor4); var result = DefensiveArmiesNeeded.For(testRegion.Neighbors, testRegion.Armies, 5); Assert.AreEqual(int.MaxValue, result); }