diff --git a/SharpPropagation/Population.cs b/SharpPropagation/Population.cs index 10186e2..5e6c934 100644 --- a/SharpPropagation/Population.cs +++ b/SharpPropagation/Population.cs @@ -48,8 +48,9 @@ namespace SharpPropagation Rnd = rnd; Age = 0; Humans = new Human[dimensions.Width, dimensions.Height]; - + Console.WriteLine("Starting Generation"); Generate(); + Console.WriteLine("Generation Finished"); } public void SetDisease(Disease plague) @@ -68,27 +69,39 @@ namespace SharpPropagation //Other thing, there will always be more of the last one because someone who is already infected for example could be then put to immune or dead //One solution to this issue would be to have if else else statements but in this case for example 20% would be lower with the last because its 20% on the remaining population and not of the all //In other words I did it that way but it can be changed just its not the right method to have perfect ratios + if (StartInfectedRatio != 0) + { + if (StartInfectedRatio == 100 || Roll(Rnd,StartInfectedRatio)) + { + Humans[x, y] = new Human(new Point(x, y), Human.State.Infected); + } + } + if (StartImmuneRatio != 0) + { + if (StartImmuneRatio == 100 || Roll(Rnd,StartImmuneRatio)) + { + Humans[x, y] = new Human(new Point(x, y), Human.State.Immune); + } + } + if (StartDeadRatio != 0) + { + if (StartDeadRatio == 100 || Roll(Rnd,StartDeadRatio)) + { + Humans[x, y] = new Human(new Point(x, y), Human.State.Dead); + } + } - Humans[x, y] = new Human(new Point(x, y), Human.State.Normal); - - if (Rnd.Next(0, CORRECTED_PERCENTAGE) < StartInfectedRatio) + if (Humans[x,y] is null) { - Humans[x, y].PresentState = Human.State.Infected; - } - if (Rnd.Next(0, CORRECTED_PERCENTAGE) < StartImmuneRatio) - { - Humans[x, y].PresentState = Human.State.Immune; - } - if (Rnd.Next(0, CORRECTED_PERCENTAGE) < StartDeadRatio) - { - Humans[x, y].PresentState = Human.State.Dead; + Humans[x, y] = new Human(new Point(x, y), Human.State.Normal); } + } } } public int[] Propagate() { - List peopleToCheck = new List(); + List peopleToCheck = new List(); int[] stats = new int[] { 0, 0, 0, 0 }; // 0 Normal, 1 Infected, 2 Immune, 3 Dead @@ -104,7 +117,7 @@ namespace SharpPropagation stats[0]+=1; break; case Human.State.Infected: - peopleToCheck.Add(Humans[x, y]); + peopleToCheck.Add(new Point(x,y)); stats[1]+=1; break; case Human.State.Immune: @@ -117,52 +130,52 @@ namespace SharpPropagation }//); }//); - List peopleToInfect = new List(); - List peopleToCure = new List(); - List peopleToKill = new List(); + List peopleToInfect = new List(); + List peopleToCure = new List(); + List peopleToKill = new List(); //Parallel.ForEach(peopleToCheck, person => - foreach (Human person in peopleToCheck) + foreach (Point person in peopleToCheck) { //I am assuming that if the Human is Curing or dying on this tick, he cant infect anyone - if (wouldCure(Rnd)) + if (Roll(Rnd,Plague.CuringRate)) { peopleToCure.Add(person); } else { - if (wouldDie(Rnd)) + if (Roll(Rnd,Plague.DeathRate)) { peopleToKill.Add(person); } 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.X > 0 && person.X < Dimensions.Width - 1 && person.Y > 0 && person.Y < Dimensions.Height - 1) { /// ### /// #@# /// ### - List potentialInfections = new List(); + List potentialInfections = new List(); //potentialInfections.Add(Humans[person.Position.X,person.Position.Y]); - potentialInfections.Add(Humans[person.Position.X - 1, person.Position.Y - 1]); //Top Left - potentialInfections.Add(Humans[person.Position.X, person.Position.Y - 1]); //Top - potentialInfections.Add(Humans[person.Position.X + 1, person.Position.Y - 1]); //Top Right + potentialInfections.Add(new Point(person.X - 1, person.Y - 1)); //Top Left + potentialInfections.Add(new Point(person.X, person.Y - 1)); //Top + potentialInfections.Add(new Point(person.X + 1, person.Y - 1)); //Top Right - potentialInfections.Add(Humans[person.Position.X - 1, person.Position.Y]); //Left - potentialInfections.Add(Humans[person.Position.X + 1, person.Position.Y]); //Right + potentialInfections.Add(new Point(person.X - 1, person.Y)); //Left + potentialInfections.Add(new Point(person.X + 1, person.Y)); //Right - potentialInfections.Add(Humans[person.Position.X - 1, person.Position.Y + 1]); //Bottom Left - potentialInfections.Add(Humans[person.Position.X, person.Position.Y + 1]); //Bottom - potentialInfections.Add(Humans[person.Position.X + 1, person.Position.Y + 1]); //Bottom Right + potentialInfections.Add(new Point(person.X - 1, person.Y + 1)); //Bottom Left + potentialInfections.Add(new Point(person.X, person.Y + 1)); //Bottom + potentialInfections.Add(new Point(person.X + 1, person.Y + 1)); //Bottom Right - foreach (Human potentialInfected in potentialInfections) + foreach (Point potentialInfected in potentialInfections) { - if (potentialInfected.PresentState == Human.State.Normal) + if (Humans[potentialInfected.X,potentialInfected.Y].PresentState == Human.State.Normal) { - if (wouldBeInfected(Rnd)) + if (Roll(Rnd,Plague.InfectionRate)) { peopleToInfect.Add(potentialInfected); } @@ -216,42 +229,32 @@ namespace SharpPropagation if (peopleToInfect.Count > 0) { //Parallel.ForEach(peopleToInfect, infected => - foreach (Human infected in peopleToInfect) + foreach (Point infected in peopleToInfect) { - Humans[infected.Position.X, infected.Position.Y].PresentState = Human.State.Infected; + Humans[infected.X, infected.Y].PresentState = Human.State.Infected; }//); } if (peopleToCure.Count > 0) { //Parallel.ForEach(peopleToCure, cured => - foreach (Human cured in peopleToCure) + foreach (Point cured in peopleToCure) { - Humans[cured.Position.X, cured.Position.Y].PresentState = Human.State.Immune; + Humans[cured.X, cured.Y].PresentState = Human.State.Immune; }//); } if (peopleToKill.Count > 0) { //Parallel.ForEach(peopleToKill, dead => - foreach (Human dead in peopleToKill) + foreach (Point dead in peopleToKill) { - Humans[dead.Position.X, dead.Position.Y].PresentState = Human.State.Dead; + Humans[dead.X, dead.Y].PresentState = Human.State.Dead; }//); } return stats; } - public bool wouldBeInfected(Random rnd) + public bool Roll(Random rnd,int rate) { - return rnd.Next(0, CORRECTED_PERCENTAGE) < Plague.InfectionRate; - } - public bool wouldCure(Random rnd) - { - //You could add a modifier that increases the curing chance with the age of the disease in the Human - //Like after 2 weeks with it it would be at least 5 times more likely for him to cure - return rnd.Next(0, CORRECTED_PERCENTAGE) < Plague.CuringRate; - } - public bool wouldDie(Random rnd) - { - return rnd.Next(0, CORRECTED_PERCENTAGE) < Plague.DeathRate; + return rnd.Next(0, CORRECTED_PERCENTAGE) < rate; } public void Display() { diff --git a/SharpPropagation/Program.cs b/SharpPropagation/Program.cs index 6a5287b..b7cdda5 100644 --- a/SharpPropagation/Program.cs +++ b/SharpPropagation/Program.cs @@ -14,11 +14,11 @@ namespace SharpPropagation { Console.WriteLine("********** Sharp Propagation (Console) 2022 **********"); Random random = new Random(); - Console.WriteLine("Initialisation"); - Population peoples = new Population(new Size(1000,1000),random,10,10,5); + Size populationSize = new Size(1000,1000); + Population peoples = new Population(populationSize,random,10,10,5); peoples.SetDisease(new Disease(random,20,10,5,"Chick Chicken")); //peoples.Display(); - Console.WriteLine("Propagation :"); + Console.WriteLine($"Propagation started with {populationSize.Width} x {populationSize.Height} humans ({populationSize.Width*populationSize.Height})"); int[] stats; int steps = 0; // 0:normal 1:infected @@ -34,7 +34,7 @@ namespace SharpPropagation } //peoples.Display(); Console.WriteLine($"Propagation finished in {steps}"); - Console.ReadKey(); + //Console.ReadKey(); } } }