Create a giant warren which allows 200 rabbits

From TRCCompSci - AQA Computer Science
Revision as of 09:13, 24 May 2017 by Admin (talk | contribs) (Using the SuperWarren Class)
Jump to: navigation, search

Before you can use the Warren class as the parent of the SuperWarren class you may need to create another constructor class. You will need to create one which doesn't accept any values, the other 2 constructor must have parameters passed:

    public Warren()
    {
        this.Variability = 45;
        Rabbits = new Rabbit[MaxRabbitsInWarren];
        RabbitCount = (int)(CalculateRandomValue((int)(MaxRabbitsInWarren / 4), this.Variability));
        for (int r = 0; r < RabbitCount; r++)
        {
            Rabbits[r] = new Rabbit(Variability);
        }
    }

Because we want to override the inspect method in the new class, you need to change the Inspect method to virtual:

    public virtual void Inspect()
    {
      Console.WriteLine("Periods Run " + PeriodsRun + " Size " + RabbitCount);
    }

The SuperWarren Class

Now we are ready to declare the SuperWarren class, find the end of the Warren class and declare:

class SuperWarren : Warren
{

}

A SuperWarren is going to allow upto 200 rabbits, so you need to declare a value for MaxRabbitsInWarren. This should be a constant integer just like in the Warren class:

private const int MaxRabbitsInWarren = 200;

This should allow SuperWarren's to be created, but in-order to tell which are SuperWarren's and which are just normal ones we need to create an Inspect method:

      public override void Inspect()
      {
          base.Inspect();
          Console.WriteLine("Super Warren");
      }

This will run the inherited method first (ie base.Inspect) and then add a message to show this is a SuperWarren.

Using the SuperWarren Class

The Warren's are created in the Simulation class, find the method called CreateLandscapeAndAnimals:

private void CreateLandscapeAndAnimals(int InitialWarrenCount, int InitialFoxCount, bool FixedInitialLocations)
    {
      for (int x = 0; x < LandscapeSize; x++)
      {
        for (int y = 0; y < LandscapeSize; y++)
        {
          Landscape[x, y] = new Location();
        }
      }
      if (FixedInitialLocations)
      { 
        Landscape[1, 1].Warren = new Warren(Variability, 38);
        Landscape[2, 8].Warren = new Warren(Variability, 80);
        Landscape[9, 7].Warren = new Warren(Variability, 20);
        Landscape[10, 3].Warren = new Warren(Variability, 52);
        Landscape[13, 4].Warren = new Warren(Variability, 67);
        WarrenCount = 5;
        Landscape[2, 10].Fox = new Fox(Variability);
        Landscape[6, 1].Fox = new Fox(Variability);
        Landscape[8, 6].Fox = new Fox(Variability);
        Landscape[11, 13].Fox = new Fox(Variability);
        Landscape[12, 4].Fox = new Fox(Variability);
        FoxCount = 5;
      }
      else
      {
        for (int w = 0; w < InitialWarrenCount; w++)
        {
          CreateNewWarren();
        }
        for (int f = 0; f < InitialFoxCount; f++)
        {
          CreateNewFox();
        }
      }
    }

If you are using the fixed locations you can change some of the new Warren() sections to say new SuperWarren().

Alternatively if you are using custom settings a method called CreateNewWarren is run for each warren specified. Find this method:

private void CreateNewWarren()
    {
      int x, y;
      do
      {
        x = Rnd.Next(0, LandscapeSize);
        y = Rnd.Next(0, LandscapeSize);
      } while (Landscape[x, y].Warren != null);
      if (ShowDetail)
      {
        Console.WriteLine("New Warren at (" + x + "," + y + ")");
      }
      Landscape[x, y].Warren = new Warren(Variability);
      WarrenCount++;
    }

We can add some addition code around the line Landscape[x, y].Warren = new Warren(Variability); so that it has a 50% chance of creating a SuperWarren instead:

private void CreateNewWarren()
    {
      int x, y;
      do
      {
        x = Rnd.Next(0, LandscapeSize);
        y = Rnd.Next(0, LandscapeSize);
      } while (Landscape[x, y].Warren != null);
      if (ShowDetail)
      {
        Console.WriteLine("New Warren at (" + x + "," + y + ")");
      }
      if (x % 2 == 0)
      {
          Landscape[x, y].Warren = new Warren();
      }
      else
      {
          Landscape[x, y].Warren = new SuperWarren();
      }
      WarrenCount++;
    }