private static void Main(string[] args) { // Logging(); // Exceptions(); Threads(); return; var sovietUnionTankFactory = new SovietUnionTankFactory(); var unitedStatesTankFactory = new UnitedStatesTankFactory(); var army = new Army <SovietUnionTank>(sovietUnionTankFactory); army.AddRandomTank(10); var serializer = new XmlSerializer(); var data = army.Serialize(serializer); Console.WriteLine(data); var t = Army <Tank> .Deserialize(serializer, data); }
private static void First4() { var sovietUnionTankFactory = new SovietUnionTankFactory(); var unitedStatesTankFactory = new UnitedStatesTankFactory(); // Covariance IEnumerable <Tank> unitedStatesTanks = new List <UnitedStatesTank> { (UnitedStatesTank)unitedStatesTankFactory.CreateLightTank(), (UnitedStatesTank)unitedStatesTankFactory.CreateCompositeTank(), (UnitedStatesTank)unitedStatesTankFactory.CreateReactiveTank(), }; Func <Tank, uint> attackContravariance = null; foreach (var tank in GetTanks(sovietUnionTankFactory)) { // Console.WriteLine(new string('-', 20)); // Console.WriteLine($"{((SovietUnionTank)tank).Clone()}{Environment.NewLine}"); // Console.WriteLine($"{tank}{Environment.NewLine}"); // var copies = Copier<SovietUnionTank>.Copy((SovietUnionTank) tank, 10); // foreach (var sovietUnionTank in copies) // { // Console.WriteLine(sovietUnionTank); // } if (attackContravariance != null) { // Console.WriteLine("Attack: {0}", attackContravariance(tank)); // Console.WriteLine($"{tank}{Environment.NewLine}"); } attackContravariance = tank.Shoot; } foreach (var tank in unitedStatesTanks) { // Console.WriteLine($"{tank}{Environment.NewLine}"); } var army = new Army <SovietUnionTank>(sovietUnionTankFactory); army.AddRandomTank(10); foreach (var unit in army) { Console.WriteLine(unit); } Console.WriteLine(new string('-', 20)); army.Sort((l, r) => l.Armor.HealthPoints.CompareTo(r.Armor.HealthPoints)); foreach (var unit in army) { Console.WriteLine(unit); } Console.WriteLine(new string('-', 20)); Func <Army <SovietUnionTank>, List <int> > getTankWith100Hp = (collection) => { var res = new List <int>(); for (int i = 0; i < collection.Count; ++i) { if (collection[i].Armor.HealthPoints == 100) { res.Add(i); } } return(res); }; Console.WriteLine(string.Join(' ', getTankWith100Hp(army))); Console.WriteLine(new string('-', 20)); Army <SovietUnionTank> .DamageAllDelegate damage = () => 10; army.GetDamageForAll = damage; army.DamageAll(); foreach (var unit in army) { Console.WriteLine(unit); } Console.WriteLine(new string('-', 20)); Action <Army <SovietUnionTank> > pureArmor = (collection) => { foreach (var item in collection) { item.Armor.TakeDamage(item.Armor.HealthPoints * 1000); } }; pureArmor(army); foreach (var unit in army) { Console.WriteLine(unit); } Console.WriteLine(new string('-', 20)); }
private static void LoadArmies(string path) { var forces = File.ReadAllText(path); var data = JObject.Parse(forces); var existingArmies = new HashSet <string>(); var armies = new List <Army <Tank> >(); foreach (var army in data["Armies"]) { var side = (string)army["Side"]; if (existingArmies.Contains(side)) { throw new TooManyForcesException($"Each side can contains at most 1 army, but '{side}' wants to have more"); } existingArmies.Add(side); TankFactory factory = null; switch (side) { case "SovietUnion": factory = new SovietUnionTankFactory(); break; case "UnitedStates": factory = new UnitedStatesTankFactory(); break; default: throw new ArgumentException($"There is no such side like '{side}'"); } var tanks = new Army <Tank>(factory); foreach (var tank in army["Tanks"]) { var type = (string)tank["Type"]; switch (type) { case "Reactive": tanks.AddReactiveTank(); break; case "Light": tanks.AddLightTank(); break; case "Composite": tanks.AddCompositeTank(); break; default: throw new ArgumentException($"There is no such tank type like '{type}'"); } } var extraTanks = (int)(army["RandomTanks"] ?? 0); for (var i = 0; i < extraTanks; ++i) { tanks.AddRandomTank(); } armies.Add(tanks); } foreach (var army in armies) { Console.WriteLine("---"); Console.WriteLine(string.Join(' ', army)); } }