Browse Source

D15.2 OK!

bastien.monsarrat 6 years ago
parent
commit
9029c4ba99
1 changed files with 61 additions and 45 deletions
  1. 61 45
      D15.2/Program.cs

+ 61 - 45
D15.2/Program.cs

@@ -8,16 +8,26 @@ namespace D15._2
 {
     abstract class Unit
     {
-        public int HP = 200;
-        public int Atk = 3;
+        private int MaxHP = 200;
+        public int HP = 0;
         public (int X, int Y) Coord;
+        private (int X, int Y) InitCoord;
         public bool IsHealthy = true;
 
+        public Unit((int x, int y) coord)
+        {
+            HP = MaxHP;
+            InitCoord = coord;
+            Coord = InitCoord;
+        }
+
+        public abstract int GetAtk();
+
         public void Move((int mx, int my) mv) => Coord = (Coord.X + mv.mx, Coord.Y + mv.my);
 
         public void Attack(Unit target)
         {
-            target.ReceiveAttack(Atk);
+            target.ReceiveAttack(GetAtk());
         }
 
         private void ReceiveAttack(int atk)
@@ -25,20 +35,45 @@ namespace D15._2
             HP -= atk;
             if (HP <= 0) IsHealthy = false;
         }
+
+        public void Wololo()
+        {
+            HP = MaxHP;
+            IsHealthy = true;
+            Coord = InitCoord;
+        }
     }
 
-    class Gob : Unit { }
-    class Elf : Unit { }
+    class Gob : Unit
+    {
+        public Gob((int x, int y) coord) : base(coord) { }
+
+        public override int GetAtk()
+        {
+            return 3;
+        }
+    }
+    class Elf : Unit
+    {
+        public Elf((int x, int y) coord) : base(coord) { }
+
+        public override int GetAtk()
+        {
+            return Program.ElfAtk;
+        }
+    }
 
     class Map : HashSet<(int x, int y)> { }
     class Units : List<Unit> { }
 
     class Program
     {
+        public static int ElfAtk = 4;
+
         static readonly (int mx, int my)[] MoveSequence = new[] { (0, -1), (-1, 0), (1, 0), (0, 1) };
         static int round = 0;
 
-        static bool PRINT = false;
+        //static bool PRINT = false;
 
         static void Main(string[] args)
         {
@@ -51,52 +86,33 @@ namespace D15._2
             var units = new Units();
 
             FillMap(file, map, strMap, units);
-            if (PRINT) PrintMap(map, strMap, units, 0);
 
-            bool continueCombat = true;
-            do
+            while (true)
             {
-                continueCombat = Tick(map, units);
-                if (continueCombat) round++;
-
-                if (PRINT) PrintMap(map, strMap, units, round);
-
-            } while (continueCombat);
-
-            TheAnswerIs(units, round);
-        }
-
-        private static void PrintMap(Map map, List<string> strMap, Units units, int round)
-        {
-            if (round > 0) Console.WriteLine($"Playing round {round + 1}");
+                Console.WriteLine($"Testing atk power of : {ElfAtk}");
 
-            for (int y = 0; y < strMap.Count; ++y)
-            {
-                var line = strMap[y];
-                List<Unit> ul = new List<Unit>();
+                round = 0;
+                bool continueCombat = true;
+                foreach (var unit in units) unit.Wololo();
 
-                for (var x = 0; x < line.Length; ++x)
+                do
                 {
-                    if (!map.Contains((x, y)))
-                        Console.Write("#");
-                    else
-                    {
-                        var u = units.FirstOrDefault(un => un.Coord == (x, y));
-
-                        if (u?.IsHealthy == false) u = null;
-                        if (u != null) ul.Add(u);
+                    continueCombat = Tick(map, units);
+                    if (continueCombat) round++;
 
-                        if (u != null && u is Gob) Console.Write("G");
-                        else if (u != null && u is Elf) Console.Write("E");
-                        else Console.Write(".");
-                    }
-                }
+                } while (continueCombat);
 
-                if (ul.Count > 0)
-                    Console.Write($"\t{ string.Join(", ", ul.Select(u => $"{ (u is Gob ? 'G' : 'E') }({u.HP})")) }");
+                var deadElf = 0;
+                foreach (var unit in units)
+                    if (unit is Elf && unit.IsHealthy == false) deadElf++;
+                if (deadElf == 0) break;
 
-                Console.WriteLine();
+                ElfAtk++;
+                Console.WriteLine($"Elves suffered heavy losses...\n");
             }
+
+            Console.WriteLine($"Elves win !");
+            TheAnswerIs(units, round);
         }
 
         private static void TheAnswerIs(Units units, int round)
@@ -305,8 +321,8 @@ namespace D15._2
                 {
                     (int x, int y) coord = (x, y);
 
-                    if (line[x] == 'G') units.Add(new Gob() { Coord = coord });
-                    if (line[x] == 'E') units.Add(new Elf() { Coord = coord });
+                    if (line[x] == 'G') units.Add(new Gob(coord));
+                    if (line[x] == 'E') units.Add(new Elf(coord));
 
                     if (line[x] != '#') map.Add(coord);
                 }