Example #1
0
 public void VerifySubGroups()
 {
     foreach (Fleet f in Fleets)
     {
         if (f.Orbiting != this)
         {
             Fleets.Remove(f);
         }
     }
 }
        //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
        private void FightBattle(Planet p)
        {
            Map <int, int> participants = new TreeMap <int, int>();

            participants.put(p.Owner, p.NumShips);
            bool doBattle = false;

            //PORT: Deleting items from iterators is not allowed.
            //      converted to deletable for loop
            for (int fleetIndex = 0; fleetIndex < Fleets.Count;)
            {
                Fleet fl = Fleets[fleetIndex];
                if (fl.TurnsRemaining <= 0 && fl.DestinationPlanet == p.PlanetID)
                {
                    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);
                }
                else
                {
                    fleetIndex++;
                }
            }

            if (doBattle)
            {
                Fleet winner = new Fleet(0, 0);
                Fleet second = new Fleet(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);
                        }
                        else
                        {
                            second = new Fleet(f.Key, f.Value);
                        }
                    }
                }

                if (winner.NumShips > second.NumShips)
                {
                    p.SetNumShips(winner.NumShips - second.NumShips);
                    p.SetOwner(winner.Owner);
                }
                else
                {
                    p.SetNumShips(0);
                }
            }
        }