fixed all problems, now its working

This commit is contained in:
2022-05-02 13:49:55 +02:00
parent 11c84ea2d4
commit 3d9d23ba7b
2 changed files with 131 additions and 137 deletions
+117 -134
View File
@@ -124,185 +124,167 @@ namespace SharpPropagation
} }
public void Propagate() public void Propagate()
{ {
int test = 0; List<Human> peopleToCheck = new List<Human>();
int[] stats;
do //Parallel.For(0, Dimensions.Width,x =>
for (int x = 0; x < Dimensions.Width; x++)
{ {
//REMOVE THIS WHEN RELEASE //Parallel.For(0, Dimensions.Width,y =>
test++; for (int y = 0; y < Dimensions.Height; y++)
if (test > 100)
{ {
//Display(); switch (Humans[x, y].PresentState)
test = 0;
}
stats = GetStats();
List<Human> peopleToCheck = new List<Human>();
//Parallel.For(0, Dimensions.Width,x =>
for (int x = 0; x < Dimensions.Width; x++)
{
//Parallel.For(0, Dimensions.Width,y =>
for (int y = 0; y < Dimensions.Width; y++)
{ {
switch (Humans[x, y].PresentState) //for now only infected peoples are interesting
{ case Human.State.Infected:
//for now only infected peoples are interesting peopleToCheck.Add(Humans[x, y]);
case Human.State.Infected: break;
peopleToCheck.Add(Humans[x, y]); default:
break; //do nothing
default: break;
//do nothing }
break;
}
}//);
}//); }//);
}//);
List<Human> peopleToInfect = new List<Human>(); List<Human> peopleToInfect = new List<Human>();
List<Human> peopleToCure = new List<Human>(); List<Human> peopleToCure = new List<Human>();
List<Human> peopleToKill = new List<Human>(); List<Human> peopleToKill = new List<Human>();
//Parallel.ForEach(peopleToCheck, person => //Parallel.ForEach(peopleToCheck, person =>
foreach (Human person in peopleToCheck) foreach (Human person in peopleToCheck)
{
//I am assuming that if the Human is Curing or dying on this tick, he cant infect anyone
if (wouldCure(Rnd))
{ {
//I am assuming that if the Human is Curing or dying on this tick, he cant infect anyone peopleToCure.Add(person);
if (wouldCure()) }
else
{
if (wouldDie(Rnd))
{ {
peopleToCure.Add(person); peopleToKill.Add(person);
} }
else else
{ {
if (wouldDie()) //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)
{ {
peopleToKill.Add(person); /// ###
} /// #@#
else /// ###
{
//now we can start to check if people would be infected or not List<Human> potentialInfections = new List<Human>();
if (person.Position.X > 0 && person.Position.X < Dimensions.Width - 1 && person.Position.Y > 0 && person.Position.Y < Dimensions.Height - 1) //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(Humans[person.Position.X - 1, person.Position.Y]); //Left
potentialInfections.Add(Humans[person.Position.X + 1, person.Position.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
foreach (Human potentialInfected in potentialInfections)
{ {
/// ### if (wouldBeInfected(Rnd))
/// #@#
/// ###
List<Human> potentialInfections = new List<Human>();
//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(Humans[person.Position.X - 1, person.Position.Y]); //Left
potentialInfections.Add(Humans[person.Position.X + 1, person.Position.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
foreach (Human potentialInfected in potentialInfections)
{ {
if (wouldBeInfected()) if (potentialInfected.PresentState == Human.State.Normal)
{ {
if (potentialInfected.PresentState == Human.State.Normal) peopleToInfect.Add(potentialInfected);
{
peopleToInfect.Add(potentialInfected);
}
} }
} }
} }
else }
{ else
//TODO {
//Check every special cases (corners sides etc..) //TODO
//Check every special cases (corners sides etc..)
//REMOVE WHEN IMPLEMENTED //REMOVE WHEN IMPLEMENTED
//It is here to prevent an infected in the borders to keep the programm running as he thinks there are still people to simulate //It is here to prevent an infected in the borders to keep the programm running as he thinks there are still people to simulate
peopleToKill.Add(person); peopleToKill.Add(person);
/// @## /// @##
/// ### /// ###
/// ### /// ###
/// #@# /// #@#
/// ### /// ###
/// ### /// ###
/// ##@ /// ##@
/// ### /// ###
/// ### /// ###
/// ### /// ###
/// @## /// @##
/// ### /// ###
/// ### /// ###
/// ##@ /// ##@
/// ### /// ###
/// ### /// ###
/// ### /// ###
/// @## /// @##
/// ### /// ###
/// ### /// ###
/// #@# /// #@#
/// ### /// ###
/// ### /// ###
/// ##@ /// ##@
}
} }
} }
}
}//);
if (peopleToInfect.Count > 0)
{
//Parallel.ForEach(peopleToInfect, infected =>
foreach (Human infected in peopleToInfect)
{
Humans[infected.Position.X, infected.Position.Y].PresentState = Human.State.Infected;
}//); }//);
if (peopleToInfect.Count > 0) }
if (peopleToCure.Count > 0)
{
//Parallel.ForEach(peopleToCure, cured =>
foreach (Human cured in peopleToCure)
{ {
//Parallel.ForEach(peopleToInfect, infected => Humans[cured.Position.X, cured.Position.Y].PresentState = Human.State.Immune;
foreach (Human infected in peopleToInfect) }//);
{ }
Humans[infected.Position.X, infected.Position.Y].PresentState = Human.State.Infected; if (peopleToKill.Count > 0)
}//); {
} //Parallel.ForEach(peopleToKill, dead =>
if (peopleToCure.Count > 0) foreach (Human dead in peopleToKill)
{ {
//Parallel.ForEach(peopleToCure, cured => Humans[dead.Position.X, dead.Position.Y].PresentState = Human.State.Dead;
foreach (Human cured in peopleToCure) }//);
{ }
Humans[cured.Position.X, cured.Position.Y].PresentState = Human.State.Immune;
}//);
}
if (peopleToKill.Count > 0)
{
//Parallel.ForEach(peopleToKill, dead =>
foreach (Human dead in peopleToKill)
{
Humans[dead.Position.X, dead.Position.Y].PresentState = Human.State.Dead;
}//);
}
}while (stats[1] > 0); /*while (test < 1000);*/
// It will run until the last infected man either dies or cures
} }
public bool wouldBeInfected() public bool wouldBeInfected(Random rnd)
{ {
return Rnd.Next(0, CORRECTED_PERCENTAGE) > Plague.InfectionRate; return rnd.Next(0, CORRECTED_PERCENTAGE) < Plague.InfectionRate;
} }
public bool wouldCure() public bool wouldCure(Random rnd)
{ {
//You could add a modifier that increases the curing chance with the age of the disease in the Human //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 //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; return rnd.Next(0, CORRECTED_PERCENTAGE) < Plague.CuringRate;
} }
public bool wouldDie() public bool wouldDie(Random rnd)
{ {
return Rnd.Next(0, CORRECTED_PERCENTAGE) > Plague.DeathRate; return rnd.Next(0, CORRECTED_PERCENTAGE) < Plague.DeathRate;
} }
public void Display() public void Display()
{ {
int[] stats = GetStats(); int[] stats = GetStats();
char sprite = '#'; char sprite = '#';
//Console.Clear();
Console.Clear();
Console.WriteLine(String.Format("Population aged {0}", Age)); 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) if (Plague != null)
@@ -341,6 +323,7 @@ namespace SharpPropagation
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;
} }
} }
Console.Write(Environment.NewLine);
} }
} }
} }
+14 -3
View File
@@ -4,6 +4,7 @@ using System.Drawing;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Threading;
namespace SharpPropagation namespace SharpPropagation
{ {
@@ -18,9 +19,19 @@ namespace SharpPropagation
peoples.SetDisease(new Disease(random,20,10,5,"Chick Chicken")); peoples.SetDisease(new Disease(random,20,10,5,"Chick Chicken"));
Console.WriteLine("Before Propagation"); Console.WriteLine("Before Propagation");
peoples.Display(); peoples.Display();
peoples.Propagate();
Console.WriteLine("After Propagation"); int[] stats = peoples.GetStats();
peoples.Display(); // 0:normal 1:infected
int test = 1;
while (stats[1] > 5 && test < 60)
{
Console.Clear();
peoples.Propagate();
peoples.Display();
test++;
//Thread.Sleep(200);
}
Console.ReadKey(); Console.ReadKey();
} }
} }