A fox moves in a random direction if they haven't eaten enough food that turn

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

The Fox Class

Declare a new Boolean called MoveFox, this should be declared as private (you could make it simpler by declaring it public).

Then create a method to set this value.

Then create a method to get this value.

    private bool MoveFox = false;

    public void SetMoveFox(bool value) 
    {
        MoveFox = value;
    }

    public bool GetMoveFox()
    {
        return MoveFox;
    }

Find the line in the Fox class that displays the following message:

Console.WriteLine("  Fox ages further due to lack of food.");

Add a line below this to set MoveFox to true:

                SetMoveFox(true);

or:

                MoveFox = true;

The Simulation Class

The AdvanceTimePeriod method has the following code to cycle through each fox:

for (int x = 0; x < LandscapeSize; x++)
      {
        for (int y = 0; y < LandscapeSize; y++)
        {
          if (Landscape[x, y].Fox != null)
          {
            if (ShowDetail)
            {
              Console.WriteLine("Fox at (" + x + "," + y + "): ");
            }
            Landscape[x, y].Fox.AdvanceGeneration(ShowDetail);
            if (Landscape[x, y].Fox.CheckIfDead())
            {
              Landscape[x, y].Fox = null;
              FoxCount--;
            }
            else
            {
              if (Landscape[x, y].Fox.ReproduceThisPeriod())
              {
                if (ShowDetail) {
                  Console.WriteLine("  Fox has reproduced. ");
                }
                NewFoxCount++;
              }
              if (ShowDetail) {
                Landscape[x, y].Fox.Inspect();
              }
              Landscape[x, y].Fox.ResetFoodConsumed();
            }
          }
        }
      }

Copy this nested for loop and paste it directly below, and remove the unnecessary bits :

 for (int x = 0; x < LandscapeSize; x++)
      {
          for (int y = 0; y < LandscapeSize; y++)
          {
              if (Landscape[x, y].Fox != null)
              {
                  if (ShowDetail)
                  {
                      Console.WriteLine("Fox at (" + x + "," + y + "): ");
                  }                     
              }
          }
      }

Firstly change the if statement to check if the fox is not null AND the fox can be moved:

 if (Landscape[x, y].Fox != null && Landscape[x, y].Fox.GetMoveFox())

Now for the tricky part. We can define 2 new integers for OffsetX and OffsetY, these are randomly generated to be a value between -1 and 1. However the while condition of the do while loop must check if this position is on the landscape:

                          int OffsetX;
                          do
                          {
                              OffsetX = Rnd.Next(-1, 1);
                          }
                          while (x + OffsetX < 0 || x + OffsetX >= LandscapeSize);

                          int OffsetY;
                          do
                          {
                              OffsetY = Rnd.Next(-1, 1);
                          }
                          while (y + OffsetY < 0 || y + OffsetY >= LandscapeSize);

You now need to check if the new location is empty. If so then set the fox at the new location to equal the fox at the old location. Then write a message to say the new position, then set the old location to null.

                          if (Landscape[x + OffsetX, y + OffsetY].Fox == null)
                          {
                              Landscape[x + OffsetX, y + OffsetY].Fox = Landscape[x, y].Fox;
                              Console.WriteLine("Fox Moved to (" + (x+OffsetX) + "," + (y+OffsetY) + "): ");
                              Landscape[x, y].Fox = null;
                          }

You should now have

 for (int x = 0; x < LandscapeSize; x++)
      {
          for (int y = 0; y < LandscapeSize; y++)
          {
              if (Landscape[x, y].Fox != null && Landscape[x, y].Fox.GetMoveFox())
              {
                  if (ShowDetail)
                  {
                          Console.WriteLine("Fox at (" + x + "," + y + "): ");
                          bool again = true;

                          int OffsetX;
                          do
                          {
                              OffsetX = Rnd.Next(-1, 1);
                          }
                          while (x + OffsetX < 0 || x + OffsetX >= LandscapeSize);

                          int OffsetY;
                          do
                          {
                              OffsetY = Rnd.Next(-1, 1);
                          }
                          while (y + OffsetY < 0 || y + OffsetY >= LandscapeSize);

                          if (Landscape[x + OffsetX, y + OffsetY].Fox == null)
                          {
                              Landscape[x + OffsetX, y + OffsetY].Fox = Landscape[x, y].Fox;
                              Console.WriteLine("Fox Moved to (" + (x+OffsetX) + "," + (y+OffsetY) + "): ");
                              Landscape[x, y].Fox = null;
                          }
                      }
                  }
              }
          }

this will work, however if the new location is taken nothing happens. If you want to choose a new square you could create a new Boolean set to true, and create a do while loop controlled by this new Boolean. After you Console.WriteLine the Fox Moved message you should set the new Boolean to false to stop the loop:

 for (int x = 0; x < LandscapeSize; x++)
      {
          for (int y = 0; y < LandscapeSize; y++)
          {
              if (Landscape[x, y].Fox != null && Landscape[x, y].Fox.GetMoveFox())
              {
                  if (ShowDetail)
                  {
                      Console.WriteLine("Fox at (" + x + "," + y + "): ");
                      bool again = true;
                      do
                      {
                          int OffsetX;
                          do
                          {
                              OffsetX = Rnd.Next(-1, 1);
                          }
                          while (x + OffsetX < 0 || x + OffsetX >= LandscapeSize);

                          int OffsetY;
                          do
                          {
                              OffsetY = Rnd.Next(-1, 1);
                          }
                          while (y + OffsetY < 0 || y + OffsetY >= LandscapeSize);

                          if (Landscape[x + OffsetX, y + OffsetY].Fox == null)
                          {
                              Landscape[x + OffsetX, y + OffsetY].Fox = Landscape[x, y].Fox;
                              Console.WriteLine("Fox Moved to (" + (x+OffsetX) + "," + (y+OffsetY) + "): ");
                              Landscape[x, y].Fox = null;
                              again = false;
                          }
                      }
                      while (again);
                  }
              }
          }
      }