コード例 #1
0
ファイル: RiskAnalyzer.cs プロジェクト: resc/Risk
 private static int GetTroopTarget(GameInformation gameInformation, Country country)
 {
     var enemies = gameInformation.GetAdjacentCountriesWithDifferentOwner(country).ToList();
     if (enemies.Count == 0) return country.IsContinentGateway() ? 4 : 2;
     if (enemies.Count == 1) return enemies[0].NumberOfTroops + 3;
     return enemies.Sum(x => x.NumberOfTroops) + 1;
 }
コード例 #2
0
ファイル: Exceptionists.cs プロジェクト: resc/Risk
        public int Defend(TurnManager turnManager, List<int> attackRolls, Country countryToDefend)
        {
            if (attackRolls.Count(x => x > 3) > 1)
                return 1;

            return 2;
        }
コード例 #3
0
ファイル: Pros.cs プロジェクト: resc/Risk
        public int Defend(TurnManager turnManager, List<int> attackRolls, Country countryToDefend)
        {
            if (attackRolls.Count == 3)
            {
                var dangerlevel = 0;

                if (attackRolls[0] >= 5)
                {
                    dangerlevel++;
                }

                if (attackRolls[1] >= 5)
                {
                    dangerlevel++;
                }

                if (attackRolls[2] >= 5)
                {
                    dangerlevel++;
                }

                if (dangerlevel >= 2)
                {
                    return 1;
                }

                return 2;
            }
            else
            {
                var dangerlevel = 0;

                if (attackRolls[0] >= 5)
                {
                    dangerlevel++;
                }

                // bug here, there can be only 1 attack roll
                // Remco: added check for attack roll count
                if (attackRolls.Count>1 && attackRolls[1] >= 5)
                {
                    dangerlevel++;
                }

                if (dangerlevel >= 2)
                {
                    return 1;
                }

                return 2;
            }
        }
コード例 #4
0
ファイル: RiskAnalyzer.cs プロジェクト: resc/Risk
 /// <summary>
 ///     Adds attack info to the analysis, but only if there is a chance for success
 /// </summary>
 private void AddAttack(TurnManager turnManager, RiskAnalysis analysis, Country friendly, Country enemy)
 {
     var minDefenders = 2;
     if (friendly.NumberOfTroops > minDefenders+1 && friendly.NumberOfTroops > enemy.NumberOfTroops )
     {
         analysis.Attacks.Add(new Attack(turnManager)
             {
                 FromCountry = friendly,
                 ToCountry = enemy,
                 Troops = friendly.NumberOfTroops - minDefenders,
             });
     }
 }
コード例 #5
0
ファイル: ActionLogger.cs プロジェクト: resc/Risk
        public void AddDeployAction(Country country, int addedTroops)
        {
            var existingDeployActions =
                gameManager.Actions.Where(x => x is DeployAction).Cast<DeployAction>();
            var existingActionThisTurn =
                existingDeployActions.FirstOrDefault(x => x.CountryToDeploy == country && x.ShowOnMap);

            if (existingActionThisTurn != null)
            {
                existingActionThisTurn.NumberOfAddedTroops += addedTroops;
            }
            else
            {
                gameManager.Actions.Add(new DeployAction
                    {
                        CountryToDeploy = country,
                        NumberOfAddedTroops = addedTroops,
                        Player = gameManager.CurrentPlayer,
                        ShowOnMap = true
                    });
            }
        }
コード例 #6
0
ファイル: Player3_.cs プロジェクト: resc/Risk
 public int Defend(TurnManager turnManager, List<int> attackRolls, Country countryToDefend)
 {
     return 2;
 }
コード例 #7
0
ファイル: RiskAnalysis.cs プロジェクト: resc/Risk
 /// <summary>
 ///     Returns true if the country is important to defend.
 ///     for example :
 ///     - losing it would destroy a continent troop bonus.
 ///     - it's an attack launch site
 ///     - it's a deployment site
 ///     - it's involved in a move.
 ///     The analyzer determines which countries are important.
 /// </summary>
 public bool IsImportant(Country country)
 {
     return ImportantCountries.Contains(country);
 }
コード例 #8
0
ファイル: RiskAnalysis.cs プロジェクト: resc/Risk
        public int GetDefenceRolls(List<int> attackRolls, Country countryToDefend)
        {
            const int minNumberOfDefenders = 1;
            var maxNumberofDefenders = Math.Min(countryToDefend.NumberOfTroops, 2);

            if (IsImportant(countryToDefend))
            {
                return maxNumberofDefenders;
            }

            // more defence rolls than attack rolls increase the chances for a succesfull defence
            if (attackRolls.Count < countryToDefend.NumberOfTroops)
            {
                return maxNumberofDefenders;
            }

            // make defeat take longer by dragging out the attack.
            return minNumberOfDefenders;
        }
コード例 #9
0
ファイル: Remco.cs プロジェクト: resc/Risk
 public int Defend(TurnManager turnManager, List<int> attackRolls, Country countryToDefend)
 {
     var analysis = riskAnalyzer.Analyze(turnManager);
     return analysis.GetDefenceRolls(attackRolls, countryToDefend);
 }
コード例 #10
0
ファイル: ActionLogger.cs プロジェクト: resc/Risk
 public void AddMoveAction(Country country, Country countryToMoveTo, int numberOfTroops)
 {
     gameManager.Actions.Add(new MoveAction
         {
             Country = country,
             CountryToMoveTo = countryToMoveTo,
             NumberOfTroopsToMove = numberOfTroops,
             Player = gameManager.CurrentPlayer
         });
 }
コード例 #11
0
ファイル: Aad.cs プロジェクト: resc/Risk
 private Country GetMostImportantSurroundingCountry(TurnManager turnManager, Country country)
 {
     return
         country.AdjacentCountries.ToList()
                .Where(c => c.Owner == this)
                .OrderBy(c => GetCountryScore(turnManager, c))
                .Last();
 }
コード例 #12
0
ファイル: TurnManager.cs プロジェクト: resc/Risk
        public void MoveTroops(Country country, Country countryToMoveTo, int numberOfTroops)
        {
            ValidatePlayer();
            if (gameManager.CurrentPhase != EPhase.Move) throw new RiskException("Its not the move phase");
            if (country.Owner != player) throw new RiskException("Troops can only be moved from owned countries");
            if (countryToMoveTo.Owner != player) throw new RiskException("Troops can only be moved to owned countries");
            if (!country.AdjacentCountries.Contains(countryToMoveTo))
                throw new RiskException("Countries must be adjacent to move");
            if (country.NumberOfTroops - numberOfTroops < 1) throw new RiskException("Not enough troops on country");
            if (gameManager.TroopsToMove - numberOfTroops < 0)
                throw new RiskException(string.Format("unable to move {0} troops, only {1} moves left", numberOfTroops,
                                                      gameManager.TroopsToMove));
            if (numberOfTroops < 1) throw new RiskException("Unable to move less then 1 troop");

            gameManager.TroopsToMove -= numberOfTroops;

            country.NumberOfTroops -= numberOfTroops;
            countryToMoveTo.NumberOfTroops += numberOfTroops;

            new ActionLogger(gameManager).AddMoveAction(country, countryToMoveTo, numberOfTroops);
        }
コード例 #13
0
ファイル: Exceptionists.cs プロジェクト: resc/Risk
 private int GetNumberOfTotalEnemyTroupsForCountry(Country country)
 {
     var numberOfEnemies =
         gameInformation.GetAdjacentCountriesWithDifferentOwner(country).Sum(x => x.NumberOfTroops);
     return numberOfEnemies;
 }
コード例 #14
0
ファイル: Exceptionists.cs プロジェクト: resc/Risk
 private double GetDefensiveRateForCountry(Country country, int numberOfTotalEnemyTroopsForCountry)
 {
     return (double) country.NumberOfTroops/numberOfTotalEnemyTroopsForCountry;
 }
コード例 #15
0
ファイル: Aad.cs プロジェクト: resc/Risk
 public PossibleAttack(Country myCountry, Country victim, int troops)
 {
     MyCountry = myCountry;
     Victim = victim;
     NumberOfTroops = troops;
 }
コード例 #16
0
ファイル: Aad.cs プロジェクト: resc/Risk
 private void MoveTroopsToSurroundingCountries(TurnManager turnManager, Country country)
 {
     turnManager.MoveTroops(country, GetMostImportantSurroundingCountry(turnManager, country), 1);
 }
コード例 #17
0
ファイル: Aad.cs プロジェクト: resc/Risk
        private bool IsLastEnemyInContinent(TurnManager turnManager, Country country)
        {
            var victimContinent = country.Continent;

            var info = turnManager.GetGameInfo();

            var myCountriesInContinent =
                info.GetAllCountriesOwnedByPlayer(this).Count(c => c.Continent == country.Continent);
            var allCountriesInContinent = info.GetAllCountries().Count(c => c.Continent == country.Continent);

            return (allCountriesInContinent - myCountriesInContinent) == 1;
        }
コード例 #18
0
ファイル: Player3_.cs プロジェクト: resc/Risk
 private Country GetRandomAdjacentCountryToAttack(TurnManager turnManager, Country country)
 {
     var adjacentCountries =
         turnManager.GetGameInfo().GetAdjacentCountriesWithDifferentOwner(country);
     return adjacentCountries[r.Next(0, adjacentCountries.Count - 1)];
 }
コード例 #19
0
ファイル: TurnManager.cs プロジェクト: resc/Risk
        public void DeployTroops(Country country, int numberOfTroops)
        {
            ValidatePlayer();
            if (gameManager.CurrentPhase != EPhase.Deploy) throw new RiskException("Its not the deploy phase");
            if (country.Owner != player) throw new RiskException("Troops can only be deployed on owned countries");
            if (gameManager.TroopsToDeploy - numberOfTroops < 0)
                throw new RiskException("Unable to deploy more troops than given");

            gameManager.TroopsToDeploy -= numberOfTroops;
            country.NumberOfTroops += numberOfTroops;

            new ActionLogger(gameManager).AddDeployAction(country, numberOfTroops);
        }
コード例 #20
0
ファイル: Aad.cs プロジェクト: resc/Risk
        private double GetCountryScore(TurnManager turnManager, Country country)
        {
            var NumberOfPotentialTroopsFromContinent = 0;
            var StaticValue = 0;
            var SurroundingFriendlyCountries = 0;
            var SurroundingEnemyCountries = 0;

            var SurroundingFriendlyTroops = 0;
            var SurroundingEnemyTroops = 0;

            var average = AverageTroopsPerCountry(turnManager);

            if (country.NumberOfTroops > (1.5 * average))
            {
                return 0;
            }

            SurroundingFriendlyCountries = country.AdjacentCountries.ToList().Where(c => c.Owner == this).Count();
            SurroundingEnemyCountries = country.AdjacentCountries.Count - SurroundingFriendlyCountries;

            if (SurroundingFriendlyCountries == country.AdjacentCountries.Count)
            {
                return 0;
            }

            switch (country.Continent)
            {
                case EContinent.NorthAmerica:
                    NumberOfPotentialTroopsFromContinent = 4;
                    break;
                case EContinent.SouthAmerica:
                    NumberOfPotentialTroopsFromContinent = 4;
                    break;
                case EContinent.Africa:
                    NumberOfPotentialTroopsFromContinent = 4;
                    break;
                case EContinent.Europe:
                    NumberOfPotentialTroopsFromContinent = 4;
                    break;
                case EContinent.Asia:
                    NumberOfPotentialTroopsFromContinent = 4;
                    break;
                case EContinent.Australia:
                    NumberOfPotentialTroopsFromContinent = 3;
                    break;
                default:
                    break;
            }

            var belowAverage = average - country.NumberOfTroops;

            var rate = NumberOfPotentialTroopsFromContinent * 1 +
                       StaticValue * 1 +
                       SurroundingEnemyTroops * 1 -
                       SurroundingFriendlyTroops * 1 +
                       SurroundingEnemyCountries * 1 -
                       SurroundingFriendlyCountries * 1 +
                       belowAverage * 2;

            return rate;
        }
コード例 #21
0
ファイル: TurnManager.cs プロジェクト: resc/Risk
        public bool Attack(Country country, Country countryToAttack, int numberOftroopsToAttackWith)
        {
            ValidatePlayer();
            if (gameManager.CurrentPhase != EPhase.Attack) throw new RiskException("Its not the attack phase");
            if (country.Owner != player) throw new RiskException("Attacks can only start from a owned country");
            if (countryToAttack.Owner == player) throw new RiskException("Can't attack a owned country");
            if (!country.AdjacentCountries.Contains(countryToAttack))
                throw new RiskException("Countries must be adjacent to attack");
            if (country.NumberOfTroops - numberOftroopsToAttackWith < 1)
                throw new RiskException("Not enough troops on country");

            country.NumberOfTroops -= numberOftroopsToAttackWith;

            var attackAction = new AttackAction
                {
                    Player = country.Owner,
                    Country = country,
                    CountryToAttack = countryToAttack,
                    DefendingPlayer = countryToAttack.Owner,
                    Attackers = numberOftroopsToAttackWith,
                    Defenders = countryToAttack.NumberOfTroops
                };

            var totalDeadDefenders = 0;
            var totalDeadAttackers = 0;

            do
            {
                var numberOfAttackDiceRolls = Math.Min(3, numberOftroopsToAttackWith);
                var attackRolls = gameManager.GetDiceRolls(numberOfAttackDiceRolls);

                int numberOfDiceToDefendWith;

                try
                {
                    var defenceTurnManager = gameManager.GetTurnManager(countryToAttack.Owner);
                    numberOfDiceToDefendWith = countryToAttack.Owner.Defend(defenceTurnManager, attackRolls.GetRange(0, attackRolls.Count), countryToAttack);

                    if (numberOfDiceToDefendWith > 2 || numberOfDiceToDefendWith < 1 ||
                        countryToAttack.NumberOfTroops == 1)
                    {
                        numberOfDiceToDefendWith = 1;
                    }
                }
                catch (Exception e)
                {
                    new ActionLogger(gameManager).AddException(e, countryToAttack.Owner);
                    numberOfDiceToDefendWith = 1;
                }

                var defendRolls = gameManager.GetDiceRolls(numberOfDiceToDefendWith);

                var rollsToCheck = attackRolls.Count > 1 && defendRolls.Count > 1 ? 2 : 1;
                var deadAttackers = 0;
                var deadDefenders = 0;

                for (var i = 0; i < rollsToCheck; i++)
                {
                    var highestAttackRoll = attackRolls.OrderByDescending(roll => roll).First();
                    attackRolls.Remove(highestAttackRoll);

                    var highestDefendRoll = defendRolls.OrderByDescending(roll => roll).First();
                    defendRolls.Remove(highestDefendRoll);

                    if (highestAttackRoll > highestDefendRoll)
                    {
                        deadDefenders++;
                    }
                    else
                    {
                        deadAttackers++;
                    }
                }

                countryToAttack.NumberOfTroops -= deadDefenders;
                numberOftroopsToAttackWith -= deadAttackers;
                totalDeadAttackers += deadAttackers;
                totalDeadDefenders += deadDefenders;
            } while (numberOftroopsToAttackWith > 0 && countryToAttack.NumberOfTroops > 0);

            attackAction.DeadAttackers = totalDeadAttackers;
            attackAction.DeadDefenders = totalDeadDefenders;
            attackAction.AttackSucceeded = countryToAttack.NumberOfTroops == 0;

            gameManager.Actions.Add(attackAction);

            if (countryToAttack.NumberOfTroops == 0)
            {
                countryToAttack.Owner = country.Owner;
                countryToAttack.NumberOfTroops = numberOftroopsToAttackWith;

                return true;
            }

            return false;
        }
コード例 #22
0
ファイル: GameInformation.cs プロジェクト: resc/Risk
 public List<Country> GetAdjacentCountriesWithSameOwner(Country country)
 {
     return country.AdjacentCountries.Where(adjacentCountry => adjacentCountry.Owner == country.Owner).ToList();
 }