示例#1
0
        // 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;
        }
示例#2
0
        // 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();
            }
        }
示例#3
0
 // Remove destroyed ships from fleet
 public void Update()
 {
     lostShips = ships.LostShips();
     ships.Update();
 }
示例#4
0
 public Fleet(string name, Ships ships, Random rand)
 {
     this.rand  = rand;
     this.name  = name;
     this.ships = ships;
 }
示例#5
0
        // Attack fleet opponent
        public void Fire(Ships opponent, Random rand)
        {
            // Fleet 1 attack
            foreach (Ship ship in ships)
            {
                // Generate random damage
                ship.GenerateDamage(rand);

                // Random target from the opponent
                Ship target = opponent.GetRandomShip(rand);
                target.EnableTarget();

                // Fire the target
                ship.Fire(target);
            }
        }
示例#6
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;
     }
 }
示例#7
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;
     }
 }