Example #1
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;
     }
 }
Example #2
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);
            }
        }
Example #3
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;
     }
 }
Example #4
0
        // Load file version 2
        private Fleet LoadFleetVersion2(StreamReader fin)
        {
            int numberOfShip = 0;
            // Read the fleet name
            string fleetName = fin.ReadLine();
            if (String.IsNullOrEmpty(fleetName))
                throw new Exception("Missing fleet name");

            Ships ships = new Ships();

            while (!fin.EndOfStream)
            {
                string shipName = fin.ReadLine();
                if (!IsValidShipName(shipName))
                    throw new Exception(shipName + " is not a valid ship class name");

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

                ships.Add(shipName, numberOfShip);
            }

            return new Fleet(fleetName, ships);
        }
Example #5
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;
        }
Example #6
0
 // Remove destroyed ships from fleet
 public void Update()
 {
     lostShips = ships.LostShips();
     ships.Update();
 }
Example #7
0
 public Fleet(string name, Ships ships)
 {
     this.name  = name;
     this.ships = ships;
 }