Difference between revisions of "If a rabbit is contained within a warren, create a den class for foxes"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Location Changes)
(Simulation Changes)
Line 137: Line 137:
 
==Simulation Changes==
 
==Simulation Changes==
 
In the simulation class we need to replace the underlined Fox entries in the Simulation constructor:  
 
In the simulation class we need to replace the underlined Fox entries in the Simulation constructor:  
 
  
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>

Revision as of 12:31, 13 June 2017

Den Class

This is unlikely to be a question you need to program, the detail and number of changes make it impractical in the time you have. You can easily create a new class for a Den by copying the warren class and edit it to become a den, ie with the new copy selected change all references from Warren to Den, and change all references from Rabbit to Fox.

Warren to den.gif

Rabbit to fox.gif

You will need to change:

private const int MaxFoxsInDen = 10;

Change the constructor so that only a single fox will start in a den:

        public Den(int Variability)
        {
            this.Variability = Variability;
            Foxs = new Fox[MaxFoxsInDen];
            Foxs[0] = new Fox(Variability);
        }

You can also remove some of the methods because they are no longer needed. So find EatFoxs, ContainsMales.

Changes to AdvanceGeneration

This is the current version of AdvanceGeneration, because we copied it from Warren and a rabbit can be male or female, we need to make some changes.

public void AdvanceGeneration(bool ShowDetail)
        {
            PeriodsRun++;
            if (FoxCount > 0)
            {
                KillByOtherFactors(ShowDetail);
            }
            if (FoxCount > 0)
            {
                AgeFoxs(ShowDetail);
            }
            if ((FoxCount > 0) && (FoxCount <= MaxFoxsInDen))
            {
                if (ContainsMales())
                {
                    MateFoxs(ShowDetail);
                }
            }
            if ((FoxCount == 0) && (ShowDetail))
            {
                Console.WriteLine("  All Foxs in Den are dead");
            }
        }

Change this If statement:

if ((FoxCount > 0) && (FoxCount <= MaxFoxsInDen))
            {
                MateFoxs(ShowDetail);
            }

Changes to MateFoxs

A fox, unlike a rabbit doesn't have Gender, so they reproduce differently. This is the current MateFoxs method:

private void MateFoxs(bool ShowDetail)
        {
            int Mate = 0;
            int Babies = 0;
            double CombinedReproductionRate;
            for (int r = 0; r < FoxCount; r++)
            {
                if ((Foxs[r].IsFemale()) && (FoxCount + Babies < MaxFoxsInDen))
                {
                    do
                    {
                        Mate = Rnd.Next(0, FoxCount);
                    } while ((Mate == r) || (Foxs[Mate].IsFemale()));
                    CombinedReproductionRate = (Foxs[r].GetReproductionRate() + Foxs[Mate].GetReproductionRate()) / 2;
                    if (CombinedReproductionRate >= 1)
                    {
                        Foxs[FoxCount + Babies] = new Fox(Variability, CombinedReproductionRate);
                        Babies++;
                    }
                }
            }
            FoxCount = FoxCount + Babies;
            if (ShowDetail)
            {
                Console.WriteLine("  " + Babies + " baby Foxs born.");
            }
        }

You can simplify the code to this:

        private void MateFoxs(bool ShowDetail)
        {
            
            int Babies = 0;
            
            for (int r = 0; r < FoxCount; r++)
            {
                if (Foxs[r].ReproduceThisPeriod())
                {
                    Babies++;
                }
            }
            FoxCount = FoxCount + Babies;
            if (ShowDetail)
            {
                Console.WriteLine("  " + Babies + " baby Foxs born.");
            }
        }

Location Changes

However, a location can only contain a fox and or a warren. You will therefore need to remove the Fox declaration and instead declare a Den. Therefore a location can only contain a Den or Warren. You need to make the following change:

class Location
    {
        public Den Den;
        public Warren Warren;

        public Location()
        {
            Den = null;
            Warren = null;
        }
    }

Simulation Changes

In the simulation class we need to replace the underlined Fox entries in the Simulation constructor:

public Simulation(int LandscapeSize, int InitialWarrenCount, int InitialFoxCount, int Variability, bool FixedInitialLocations)
        {
            int menuOption;
            int x;
            int y;
            string viewRabbits;
            this.LandscapeSize = LandscapeSize;
            this.Variability = Variability;
            Landscape = new Location[LandscapeSize, LandscapeSize];
            CreateLandscapeAndAnimals(InitialWarrenCount, InitialFoxCount, FixedInitialLocations);
            DrawLandscape();
            do
            {
                Console.WriteLine();
                Console.WriteLine("1. Advance to next time period showing detail");
                Console.WriteLine("2. Advance to next time period hiding detail");
                Console.WriteLine("3. Inspect den");
                Console.WriteLine("4. Inspect warren");
                Console.WriteLine("5. Exit");
                Console.WriteLine();
                Console.Write("Select option: ");
                menuOption = Convert.ToInt32(Console.ReadLine());
                if (menuOption == 1)
                {
                    TimePeriod++;
                    ShowDetail = true;
                    AdvanceTimePeriod();
                }
                if (menuOption == 2)
                {
                    TimePeriod++;
                    ShowDetail = false;
                    AdvanceTimePeriod();
                }
                if (menuOption == 3)
                {
                    x = InputCoordinate('x');
                    y = InputCoordinate('y');
                    if (Landscape[x, y].Den != null)
                    {
                        Landscape[x, y].Den.Inspect();
                    }
                }
                if (menuOption == 4)
                {
                    x = InputCoordinate('x');
                    y = InputCoordinate('y');
                    if (Landscape[x, y].Warren != null)
                    {
                        Landscape[x, y].Warren.Inspect();
                        Console.Write("View individual rabbits (y/n)?");
                        viewRabbits = Console.ReadLine();
                        if (viewRabbits == "y")
                        {
                            Landscape[x, y].Warren.ListRabbits();
                        }
                    }
                }
            } while (((WarrenCount > 0) || (FoxCount > 0)) && (menuOption != 5));
            Console.ReadKey();
        }

Also in CreateLandscapeAndAnimals we need to change all references to Fox to Den:

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].Den = new Den(Variability);
                Landscape[6, 1].Den = new Den(Variability);
                Landscape[8, 6].Den = new Den(Variability);
                Landscape[11, 13].Den = new Den(Variability);
                Landscape[12, 4].Den = new Den(Variability);
                FoxCount = 5;
            }
            else
            {
                for (int w = 0; w < InitialWarrenCount; w++)
                {
                    CreateNewWarren();
                }
                for (int f = 0; f < InitialFoxCount; f++)
                {
                    CreateNewDen();
                }
            }
        }

, AdvanceTimePeriod, FoxesEatRabbitsInWarren at the moment works for if fox at location is not null, however this will need to be changed so it works if Den at location is not null, and then for every fox in the den at that location.

Also how the foxes are created will need to be changed so that they are created by the Den and not by the simulation. Also when a fox reproduces it increments a variable called NewFoxCount. This is then used to randomly position the new foxes, this would need to change so that the den