diff --git a/SharpPropagation/Population.cs b/SharpPropagation/Population.cs index 17765ce..10186e2 100644 --- a/SharpPropagation/Population.cs +++ b/SharpPropagation/Population.cs @@ -86,45 +86,11 @@ namespace SharpPropagation } } } - public int[] GetStats() - { - int[] result; - if (Humans != null) - { - int normalCount = 0; - int infectedCount = 0; - int immuneCount = 0; - int deadCount = 0; - - foreach (Human michel in Humans) - { - switch (michel.PresentState) - { - case Human.State.Normal: - normalCount++; - break; - case Human.State.Infected: - infectedCount++; - break; - case Human.State.Immune: - immuneCount++; - break; - case Human.State.Dead: - deadCount++; - break; - } - } - result = new int[] { normalCount, infectedCount, immuneCount, deadCount }; - } - else - { - result = new int[] { 0, 0, 0, 0 }; - } - return result; - } - public void Propagate() + public int[] Propagate() { List peopleToCheck = new List(); + int[] stats = new int[] { 0, 0, 0, 0 }; + // 0 Normal, 1 Infected, 2 Immune, 3 Dead //Parallel.For(0, Dimensions.Width,x => for (int x = 0; x < Dimensions.Width; x++) @@ -134,12 +100,18 @@ namespace SharpPropagation { switch (Humans[x, y].PresentState) { - //for now only infected peoples are interesting + case Human.State.Normal: + stats[0]+=1; + break; case Human.State.Infected: peopleToCheck.Add(Humans[x, y]); + stats[1]+=1; break; - default: - //do nothing + case Human.State.Immune: + stats[2] += 1; + break; + case Human.State.Dead: + stats[3] += 1; break; } }//); @@ -166,7 +138,7 @@ namespace SharpPropagation else { //now we can start to check if people would be infected or not - if (person.Position.X > 0 && person.Position.X < Dimensions.Width -1 && person.Position.Y > 0 && person.Position.Y < Dimensions.Height -1) + if (person.Position.X > 0 && person.Position.X < Dimensions.Width - 1 && person.Position.Y > 0 && person.Position.Y < Dimensions.Height - 1) { /// ### /// #@# @@ -188,9 +160,9 @@ namespace SharpPropagation foreach (Human potentialInfected in potentialInfections) { - if (wouldBeInfected(Rnd)) + if (potentialInfected.PresentState == Human.State.Normal) { - if (potentialInfected.PresentState == Human.State.Normal) + if (wouldBeInfected(Rnd)) { peopleToInfect.Add(potentialInfected); } @@ -265,6 +237,7 @@ namespace SharpPropagation Humans[dead.Position.X, dead.Position.Y].PresentState = Human.State.Dead; }//); } + return stats; } public bool wouldBeInfected(Random rnd) { @@ -282,11 +255,11 @@ namespace SharpPropagation } public void Display() { - int[] stats = GetStats(); + //int[] stats = GetStats(); char sprite = '#'; //Console.Clear(); Console.WriteLine(String.Format("Population aged {0}", Age)); - Console.WriteLine(String.Format("Population : {0} Normals : {1} Infecteds : {2} Immunes : {3} Deads : {4}", Humans.Length, stats[0], stats[1], stats[2], stats[3])); + //Console.WriteLine(String.Format("Population : {0} Normals : {1} Infecteds : {2} Immunes : {3} Deads : {4}", Humans.Length, stats[0], stats[1], stats[2], stats[3])); if (Plague != null) Console.WriteLine(Plague); Console.Write("\n"); diff --git a/SharpPropagation/Program.cs b/SharpPropagation/Program.cs index 6246847..6a5287b 100644 --- a/SharpPropagation/Program.cs +++ b/SharpPropagation/Program.cs @@ -14,24 +14,26 @@ namespace SharpPropagation { Console.WriteLine("********** Sharp Propagation (Console) 2022 **********"); Random random = new Random(); - - Population peoples = new Population(new Size(30,50),random,10,10,5); + Console.WriteLine("Initialisation"); + Population peoples = new Population(new Size(1000,1000),random,10,10,5); peoples.SetDisease(new Disease(random,20,10,5,"Chick Chicken")); - Console.WriteLine("Before Propagation"); - peoples.Display(); - - int[] stats = peoples.GetStats(); + //peoples.Display(); + Console.WriteLine("Propagation :"); + int[] stats; + int steps = 0; // 0:normal 1:infected - int test = 1; - while (stats[1] > 5 && test < 60) + while(true) { - Console.Clear(); - peoples.Propagate(); - peoples.Display(); - test++; - //Thread.Sleep(200); + steps++; + stats = peoples.Propagate(); + Console.WriteLine($"Infecteds: {stats[1]} Immunes: {stats[2]} Deads: {stats[3]}"); + if (stats[1] == 0) + { + break; + } } - + //peoples.Display(); + Console.WriteLine($"Propagation finished in {steps}"); Console.ReadKey(); } }