Пример #1
0
        //Resolves the battle at planet p, if there is one.
        //* Removes all fleets involved in the battle
        //* Sets the number of ships and owner of the planet according the outcome
        public static bool FightBattle(Planet planet, int turnNumber)
        {
            Dictionary <int, int> participants = new Dictionary <int, int>();

            participants.Add(planet.Owner, planet.NumShips);
            bool doBattle = false;

            foreach (Fleet fl in planet.Armada)
            {
                if (fl.TurnsRemaining == turnNumber)
                {
                    int attackForce;
                    doBattle = true;
                    if (participants.TryGetValue(fl.Owner, out attackForce))
                    {
                        participants[fl.Owner] = attackForce + fl.NumShips;
                    }
                    else
                    {
                        participants.Add(fl.Owner, fl.NumShips);
                    }
                    //Fleets.Remove(fl);
                }
            }

            if (doBattle)
            {
                Fleet winner = new Fleet(0, 0, null, null, 0, 0);
                Fleet second = new Fleet(0, 0, null, null, 0, 0);
                foreach (var f in participants)
                {
                    if (f.Value > second.NumShips)
                    {
                        if (f.Value > winner.NumShips)
                        {
                            second = winner;
                            winner = new Fleet(f.Key, f.Value, null, null, 0, 0);
                        }
                        else
                        {
                            second = new Fleet(f.Key, f.Value, null, null, 0, 0);
                        }
                    }
                }

                if (winner.NumShips > second.NumShips)
                {
                    planet.DoesNotChangeOwner = planet.Owner == winner.Owner;
                    planet.ChangeOwner(winner.Owner, winner.NumShips - second.NumShips);
                }
                else
                {
                    planet.ChangeOwner(planet.Owner, 0);
                }
            }
            return(doBattle);
        }