Implementing weather changes that affect animals
Contents
Setup Simulation Class
Within the Simulation class, declare a new int to store the current season:
private int Season = 0;
Give Season a value
Now within the Constructor method for Simulation you need to work out the season. This can be done using % to get the remainder. Add this Check for which season you are in to option 1 and option 2:
if (menuOption == 1)
{
TimePeriod++;
Season = TimePeriod % 4;
ShowDetail = true;
AdvanceTimePeriod();
}
if (menuOption == 2)
{
TimePeriod++;
Season = TimePeriod % 4;
ShowDetail = false;
AdvanceTimePeriod();
}
Use the Season Variable
Winter Effects
within the simulation class, a method called DistanceBetween is used to calculate the distance between 2 objects. This can be altered so that in the winter season the distance are larger than normal:
private double DistanceBetween(int x1, int y1, int x2, int y2)
{
if (Season==0)
{
return (Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2))+3);
}
else
return Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2));
}
Alternatively you could change the FoxesEatRabbitsInWarren method within the Simulation class. The code below is the original skeleton program:
if (Landscape[FoxX, FoxY].Fox != null)
{
Dist = DistanceBetween(FoxX, FoxY, WarrenX, WarrenY);
if (Dist <= 3.5)
{
PercentToEat = 20;
}
else if (Dist <= 7)
{
PercentToEat = 10;
}
else
{
PercentToEat = 0;
}
RabbitsToEat = (int)Math.Round((double)(PercentToEat * RabbitCountAtStartOfPeriod / 100.0));
FoodConsumed = Landscape[WarrenX, WarrenY].Warren.EatRabbits(RabbitsToEat);
Landscape[FoxX, FoxY].Fox.GiveFood(FoodConsumed);
if (ShowDetail)
{
Console.WriteLine(" " + FoodConsumed + " rabbits eaten by fox at (" + FoxX + "," + FoxY + ").");
}
}
After the Dist = DistanceBetween(FoxX, FoxY, WarrenX, WarrenY);
line you could add the following logic:]
if (Season == 0)
Dist = Dist + 3;
Summer Effects
Summer could reduce the distances because the fox could cover more distance in searching for food when the weather is good. So alter the code above to:
if (Season == 0)
Dist = Dist + 3;
else if (Season == 3)
Dist = Dist - 3;
Your Final Version
You should now have this:
private void FoxesEatRabbitsInWarren(int WarrenX, int WarrenY)
{
int FoodConsumed;
int PercentToEat;
double Dist;
int RabbitsToEat;
int RabbitCountAtStartOfPeriod = Landscape[WarrenX, WarrenY].Warren.GetRabbitCount();
for (int FoxX = 0; FoxX < LandscapeSize; FoxX++)
{
for (int FoxY = 0; FoxY < LandscapeSize; FoxY++)
{
if (Landscape[FoxX, FoxY].Fox != null)
{
Dist = DistanceBetween(FoxX, FoxY, WarrenX, WarrenY);
if (Season == 0)
Dist = Dist + 3;
else if (Season == 3)
Dist = Dist - 3;
if (Dist <= 3.5)
{
PercentToEat = 20;
}
else if (Dist <= 7)
{
PercentToEat = 10;
}
else
{
PercentToEat = 0;
}
RabbitsToEat = (int)Math.Round((double)(PercentToEat * RabbitCountAtStartOfPeriod / 100.0));
FoodConsumed = Landscape[WarrenX, WarrenY].Warren.EatRabbits(RabbitsToEat);
Landscape[FoxX, FoxY].Fox.GiveFood(FoodConsumed);
if (ShowDetail)
{
Console.WriteLine(" " + FoodConsumed + " rabbits eaten by fox at (" + FoxX + "," + FoxY + ").");
}
}
}
}
}