Difference between revisions of "Implementing weather changes that affect animals"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Use the Season Variable)
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Will lead into Storms and fires
+
==Setup Simulation Class==
Weather chance to be certain things (storm, rain, sun, neutral,)
+
Within the Simulation class, declare a new int to store the current season:
class weather
 
==private==
 
variability float
 
stormradius float
 
  
==public==
+
<syntaxhighlight lang="csharp">
storm
+
private int Season = 0;
{
+
</syntaxhighlight>
}
 
  
rain
+
==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:
}
+
<syntaxhighlight lang="csharp">
 +
                if (menuOption == 1)
 +
                {
 +
                    TimePeriod++;
 +
                    Season = TimePeriod % 4;
 +
                    ShowDetail = true;
 +
                    AdvanceTimePeriod();
 +
                }
 +
                if (menuOption == 2)
 +
                {
 +
                    TimePeriod++;
 +
                    Season = TimePeriod % 4;
 +
                    ShowDetail = false;
 +
                    AdvanceTimePeriod();
 +
                }
 +
</syntaxhighlight>
  
sun
+
==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:
  
neutral
+
<syntaxhighlight lang="csharp">
{
+
        private double DistanceBetween(int x1, int y1, int x2, int y2)
  keep same
+
        {
}
+
            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));
 +
        }
 +
</syntaxhighlight>
 +
 
 +
Alternatively you could change the FoxesEatRabbitsInWarren method within the Simulation class. The code below is the original skeleton program:
 +
 
 +
<syntaxhighlight lang="csharp">
 +
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 + ").");
 +
            }
 +
          }
 +
</syntaxhighlight>
 +
 
 +
After the <code>Dist = DistanceBetween(FoxX, FoxY, WarrenX, WarrenY);</code> line you could add the following logic:]
 +
 
 +
<syntaxhighlight lang="csharp">
 +
            if (Season == 0)
 +
                Dist = Dist + 3;
 +
</syntaxhighlight>
 +
 
 +
===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:
 +
 
 +
<syntaxhighlight lang="csharp">
 +
            if (Season == 0)
 +
                Dist = Dist + 3;
 +
            else if (Season == 3)
 +
                Dist = Dist - 3;
 +
</syntaxhighlight>
 +
 
 +
==Your Final Version==
 +
You should now have this:
 +
 
 +
<syntaxhighlight lang="csharp">
 +
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 + ").");
 +
            }
 +
          }
 +
        }
 +
      }
 +
    }
 +
</syntaxhighlight>

Latest revision as of 11:55, 25 May 2017

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 + ").");
            }
          }
        }
      }
    }