Difference between revisions of "Allow the capacity of a warren to vary, some warrens may be smaller and other may be larger"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "Within the warren class, a variable called MaxRabbitsInWarren is set to 99. The const declaration means it can't be changed during runtime. You must therefore remove the const...")
 
(Run simulation with option 2)
 
(5 intermediate revisions by the same user not shown)
Line 12: Line 12:
  
 
==Warren Constructors==
 
==Warren Constructors==
 +
Both of the constructors below are used to create a warren. We need to add some code to both to set the size of the warren randomly.
 +
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
ublic Warren(int Variability)
+
public Warren(int Variability)
 
     {
 
     {
 
       this.Variability = Variability;
 
       this.Variability = Variability;
Line 24: Line 26:
 
     }
 
     }
  
    public Warren(int Variability, int rabbitCount)
+
public Warren(int Variability, int rabbitCount)
 
     {
 
     {
 
       this.Variability = Variability;
 
       this.Variability = Variability;
Line 34: Line 36:
 
       }
 
       }
 
     }
 
     }
 +
</syntaxhighlight>
 +
 +
We could therefore use the CalculateRandomValue() method to also create a random value for MaxRabbitsInWarren:
 +
 +
<syntaxhighlight lang=csharp>
 +
MaxRabbitsInWarren = (int)CalculateRandomValue(MaxRabbitsInWarren, Variability);
 +
</syntaxhighlight>
 +
 +
CalculateRandomValue will return a double, but we need to have an integer. Using <code>(int)</code> will cast the double into an integer instead.
 +
 +
You should now have this:
 +
 +
<syntaxhighlight lang=csharp>
 +
public Warren(int Variability)
 +
    {
 +
      this.Variability = Variability;
 +
      MaxRabbitsInWarren = (int)CalculateRandomValue(MaxRabbitsInWarren, Variability);
 +
      Rabbits = new Rabbit[MaxRabbitsInWarren];
 +
      RabbitCount = (int)(CalculateRandomValue((int)(MaxRabbitsInWarren / 4), this.Variability));
 +
      for (int r = 0; r < RabbitCount; r++)
 +
      {
 +
        Rabbits[r] = new Rabbit(Variability);
 +
      }
 +
    }
 +
 +
public Warren(int Variability, int rabbitCount)
 +
    {
 +
      this.Variability = Variability;
 +
      MaxRabbitsInWarren = (int)CalculateRandomValue(MaxRabbitsInWarren, Variability);
 +
      this.RabbitCount = rabbitCount;
 +
      Rabbits = new Rabbit[MaxRabbitsInWarren];
 +
      for (int r = 0; r < RabbitCount; r++)
 +
      {
 +
        Rabbits[r] = new Rabbit(Variability);
 +
      }
 +
    }
 +
</syntaxhighlight>
 +
 +
==Display Warren Size==
 +
You now need to edit the Inpsect method of the Warren class, this now needs to give a message to report the maximum number of rabbits allowed in the warren. This is the original:
 +
 +
<syntaxhighlight lang=csharp>
 +
    public virtual void Inspect()
 +
    {
 +
        Console.WriteLine("Periods Run " + PeriodsRun + " Size " + RabbitCount);
 +
    }
 +
</syntaxhighlight>
 +
 +
With the extra code added:
 +
 +
<syntaxhighlight lang=csharp>
 +
    public virtual void Inspect()
 +
    {
 +
        Console.WriteLine("Max Warren Size: " + MaxRabbitsInWarren);
 +
        Console.WriteLine("Periods Run " + PeriodsRun + " Size " + RabbitCount);
 +
    }
 +
</syntaxhighlight>
 +
 +
==Run simulation with option 2==
 +
After testing this it will only work with option 2 from the initial menu, ie to run with custom settings. This is because option one has a specific number of rabbits for each warren, and to retain these values Variability is set to 0. So in the constructor which accepts 2 parameters you could give a specific value for Variability in the CalculateRandomValue method instead:
 +
 +
<syntaxhighlight lang=csharp>
 +
        public Warren(int Variability, int rabbitCount)
 +
        {
 +
            this.Variability = Variability;
 +
            MaxRabbitsInWarren = (int)CalculateRandomValue(MaxRabbitsInWarren, 20);
 +
            this.RabbitCount = rabbitCount;
 +
            Rabbits = new Rabbit[MaxRabbitsInWarren];
 +
            for (int r = 0; r < RabbitCount; r++)
 +
            {
 +
                Rabbits[r] = new Rabbit(Variability);
 +
            }
 +
        }
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 14:59, 11 June 2017

Within the warren class, a variable called MaxRabbitsInWarren is set to 99. The const declaration means it can't be changed during runtime. You must therefore remove the const:

private const int MaxRabbitsInWarren = 99;

Becomes:

private int MaxRabbitsInWarren = 99;

Warren Constructors

Both of the constructors below are used to create a warren. We need to add some code to both to set the size of the warren randomly.

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

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

We could therefore use the CalculateRandomValue() method to also create a random value for MaxRabbitsInWarren:

MaxRabbitsInWarren = (int)CalculateRandomValue(MaxRabbitsInWarren, Variability);

CalculateRandomValue will return a double, but we need to have an integer. Using (int) will cast the double into an integer instead.

You should now have this:

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

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

Display Warren Size

You now need to edit the Inpsect method of the Warren class, this now needs to give a message to report the maximum number of rabbits allowed in the warren. This is the original:

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

With the extra code added:

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

Run simulation with option 2

After testing this it will only work with option 2 from the initial menu, ie to run with custom settings. This is because option one has a specific number of rabbits for each warren, and to retain these values Variability is set to 0. So in the constructor which accepts 2 parameters you could give a specific value for Variability in the CalculateRandomValue method instead:

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