Implementing weather changes that affect animals

From TRCCompSci - AQA Computer Science
Revision as of 11:52, 25 May 2017 by Admin (talk | contribs) (Use the Season Variable)
Jump to: navigation, search

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;
            else if (Season == 3)
                Dist = Dist - 3;