Пример #1
0
        public void ProcessArgs(string[] args)
        {
            ProcessNumberOfArgs(args);

            Random rand = new Random(GetSeed(args[0]));
            fleet1 = GetFleet(args[1], rand);
            fleet2 = GetFleet(args[2], rand);
        }
Пример #2
0
 public void FinalReport(Fleet opponent, int round)
 {
     Console.WriteLine("\nAfter round " + round + " the " + name + " fleet won");
     Console.WriteLine("  " + opponent.GetNumberOfLostShips() + " enemy ships destroyed");
     Console.WriteLine("  " + GetNumberOfLostShips() + " ships lost");
     Console.WriteLine("  " + ships.NumberOfShip + " ships survived");
     Console.Write(ships.DamageReport());
 }
Пример #3
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();
            }
        }
Пример #4
0
 public Game(Fleet fleet1, Fleet fleet2)
 {
     this.fleet1 = fleet1;
     this.fleet2 = fleet2;
 }
Пример #5
0
 // Fire the fleet opponent
 public void Fire(Fleet opponent)
 {
     ships.Fire(opponent.Ships, rand);
 }