Example #1
0
 //# Generates a string representation of a fleet. This is used to send data
 //# about the fleets to the client programs.
 public static string serialize_fleet(Fleet f, int pov)
 {
     int owner = switch_pov(f.owner, pov);
     string message = "F " + owner + " " + f.num_ships + " " +
       f.source + " " + f.destination + " " +
       f.total_trip_length + " " + f.turns_remaining;
     return message.Replace(".0 ", " ");
 }
Example #2
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 void fight_battle(int pid, ref Planet p, ref List<Fleet> fleets)
        {
            List<int> participants = new List<int>();

            participants = addToArray(p.owner, p.num_ships, participants);

            for (int i = fleets.Count - 1; i > -1; i--)
            {
                Fleet f = fleets[i];
                int owner = f.owner;
                if (f.turns_remaining <= 0 && f.destination == pid)
                {
                    participants = addToArray(f.owner, f.num_ships, participants);
                    fleets[i] = null;

                }

            }
            cleanUpFleets(ref fleets);

            Fleet winner = new Fleet();
            Fleet second = new Fleet();

            for (int iOwner = 0; iOwner < participants.Count; iOwner++)
            {
                int ships = participants[iOwner];
                if (ships >= second.num_ships)
                {
                    if (ships >= winner.num_ships)
                    {
                        second.owner = winner.owner;
                        second.num_ships = winner.num_ships;

                        winner.owner = iOwner;
                        winner.num_ships = ships;
                    }
                    else
                    {
                        second.owner = iOwner;
                        second.num_ships = ships;
                    }
                }
            }
            if (winner.num_ships > second.num_ships)
            {
                p.num_ships = winner.num_ships - second.num_ships;
                p.owner = winner.owner;
            }
            else p.num_ships = 0;
        }