Beispiel #1
0
 public Army(string name, UnitFactory.IUnitFactory fabric, int armyCost)
 {
     if (fabric == null)
     {
         throw new ArgumentNullException(nameof(fabric));
     }
     if (string.IsNullOrWhiteSpace(name))
     {
         throw new ArgumentException(nameof(name));
     }
     Name = name;
     while (armyCost > 0)
     {
         var unit = fabric.GetUnit(ref armyCost);
         if (unit == null)
         {
             break;
         }
         Insert(Random.Next(Count + 1), unit);
     }
 }
Beispiel #2
0
        private GameEngine(UnitFactory.IUnitFactory unitFabric, int armyCost)
        {
            if (unitFabric == null)
            {
                throw new ArgumentNullException(nameof(unitFabric));
            }
            Army1 = new Army("Army 1", unitFabric, armyCost);
            Army2 = new Army("Army 2", unitFabric, armyCost);

            IUnitObserver observer1 = new ConsoleUnitObserver(), observer2 = new BeepUnitObserver();

            foreach (var unit in Army1.Concat(Army2))
            {
                if (unit is IObservableUnit observable)
                {
                    observable.AddObserver(observer1);
                    observable.AddObserver(observer2);
                }
            }

            ILogger logger = FileLogger.GetFileLogger("ProxyLogs.txt");

            logger.Log($"New game started: {DateTime.Now}");

            void ReplaceClonersWithProxy(Army army)
            {
                for (var i = 0; i < army.Count; i++)
                {
                    var unit = army[i];
                    if (unit is ClonerUnit clonerUnit)
                    {
                        army[i] = new ProxyClonerUnit(clonerUnit, logger);
                    }
                }
            }

            ReplaceClonersWithProxy(Army1);
            ReplaceClonersWithProxy(Army2);
        }
Beispiel #3
0
 public static GameEngine StartNewGame(UnitFactory.IUnitFactory unitFabric, int armyCost)
 {
     return(CurrentGame = new GameEngine(unitFabric, armyCost));
 }