// Collect ships to fleet
        private Fleet GetFleet(string file, Random rand)
        {
            if (!File.Exists(file)) throw new Exception(file + " file not found");
            StreamReader fin = new StreamReader(file);
            if (fin == null) throw new Exception("Unable to open " + file);

            Fleet fleet = null;

            try
            {
                string fleetName = "", version = "", line = "";
                int numberOfShip = 0;

                // Read the version
                version = fin.ReadLine();

                // Read the assignment 2 format
                if (!String.IsNullOrEmpty(version) && version.Equals("#2"))
                {
                    // Read the fleet name
                    fleetName = fin.ReadLine();
                    if (String.IsNullOrEmpty(fleetName))
                        throw new Exception("Missing fleet name");

                    Ships ships = new Ships();

                    while (!fin.EndOfStream)
                    {
                        string shipName = fin.ReadLine();

                        // Read the number of ships
                        line = fin.ReadLine();
                        if (!Int32.TryParse(line, out numberOfShip) || numberOfShip < 1)
                            throw new Exception("Invalid number of ships");

                        ships.Add(shipName, numberOfShip);
                    }

                    fleet = new Fleet(fleetName, ships, rand);
                }
                // Read the assignment 1 format
                else
                {
                    // Reread again from the first line
                    fin = new StreamReader(file);

                    // Read the fleet name
                    fleetName = fin.ReadLine();
                    if (String.IsNullOrEmpty(fleetName))
                        throw new Exception("Missing fleet name");

                    // Read the number of ships
                    line = fin.ReadLine();
                    if (!Int32.TryParse(line, out numberOfShip) || numberOfShip < 1)
                        throw new Exception("Invalid number of ships");

                    Ships ships = GetShips(numberOfShip, fin);
                    fleet = new Fleet(fleetName, ships, rand);
                }
                return fleet;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + " in " + file);
            }
            finally
            {
                fin.Close();
            }
        }
Beispiel #2
0
 // Update the ships by copy the live ships to current ships
 public void Update()
 {
     if (NumberOfLiveShips() < GetLength())
     {
         Ships liveShips = new Ships(NumberOfLiveShips());
         foreach (Ship ship in ships)
         {
             if (!ship.IsDestroyed())
                 liveShips.Add(ship);
         }
         // Keep track previous number of ship
         this.prevNumberOfShip = this.numberOfShip;
         // Update current ship
         this.ships = liveShips.GetShips();
         // Update current number of ship
         this.numberOfShip = liveShips.GetLength();
     }
     else
     {
         // Keep track previous number of ship
         this.prevNumberOfShip = this.numberOfShip;
     }
 }
        // Collect individual ship to list of ships
        private Ships GetShips(int numberOfShip, StreamReader fin)
        {
            Ships ships = new Ships(numberOfShip);
            for (int i = 0; i < numberOfShip; i++)
            {
                ships.Add(GetShip(fin));
            }

            if (!EndOfFleetFile(fin))
                throw new Exception ("More ships than stated");

            return ships;
        }
Beispiel #4
0
 // Get lost ships
 public Ships LostShips()
 {
     if (NumberOfLostShips() > 0)
     {
         Ships lostShips = new Ships(NumberOfLostShips());
         foreach (Ship ship in ships)
         {
             if (ship.IsDestroyed())
                 lostShips.Add(ship);
         }
         return lostShips;
     }
     else
     {
         return null;
     }
 }