If a rabbit is contained within a warren, create a den class for foxes

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

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())
                {
                    Foxs[FoxCount + Babies] = new Fox(Variability);
                    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, viewFoxs;
            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();
                        Console.Write("View individual foxs (y/n)?");
                        viewFoxs = Console.ReadLine();
                        if (viewFoxs == "y")
                        {
                            Landscape[x, y].Den.ListFoxs();
                        }
                    }
                }
                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();
                }
            }
        }

you will also need to do the same for DrawLandscape:

 private void DrawLandscape()
        {
            Console.WriteLine();
            Console.WriteLine("TIME PERIOD: " + TimePeriod);
            Console.WriteLine();
            Console.Write("    ");
            for (int x = 0; x < LandscapeSize; x++)
            {
                if (x < 10)
                {
                    Console.Write(" ");
                }
                Console.Write(x + " |");
            }
            Console.WriteLine();
            for (int x = 0; x <= LandscapeSize * 4 + 3; x++)
            {
                Console.Write("-");
            }
            Console.WriteLine();
            for (int y = 0; y < LandscapeSize; y++)
            {
                if (y < 10)
                {
                    Console.Write(" ");
                }
                Console.Write(" " + y + "|");
                for (int x = 0; x < LandscapeSize; x++)
                {
                    if (Landscape[x, y].Warren != null)
                    {
                        if (Landscape[x, y].Warren.GetRabbitCount() < 10)
                        {
                            Console.Write(" ");
                        }
                        Console.Write(Landscape[x, y].Warren.GetRabbitCount());
                    }
                    else
                    {
                        Console.Write("  ");
                    }
                    if (Landscape[x, y].Den != null)
                    {
                        Console.Write("F");
                    }
                    else
                    {
                        Console.Write(" ");
                    }
                    Console.Write("|");
                }
                Console.WriteLine();
            }
        }
    }

You also need to do the same for CreateNewFox, change the name to CreateNewDen, and change any reference to fox to Den:

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

AdvanceTimePeriod will also need significant changes, it can be simplified because the den will need to create the fox and not the simulation:

        private void AdvanceTimePeriod()
        {
            if (ShowDetail)
            {
                Console.WriteLine();
            }
            for (int x = 0; x < LandscapeSize; x++)
            {
                for (int y = 0; y < LandscapeSize; y++)
                {
                    if (Landscape[x, y].Warren != null)
                    {
                        if (ShowDetail)
                        {
                            Console.WriteLine("Warren at (" + x + "," + y + "):");
                            Console.Write("  Period Start: ");
                            Landscape[x, y].Warren.Inspect();
                        }
                        if (DenCount > 0)
                        {
                            FoxesEatRabbitsInWarren(x, y);
                        }
                        if (Landscape[x, y].Warren.NeedToCreateNewWarren())
                        {
                            CreateNewWarren();
                        }
                        Landscape[x, y].Warren.AdvanceGeneration(ShowDetail);
                        if (ShowDetail)
                        {
                            Console.Write("  Period End: ");
                            Landscape[x, y].Warren.Inspect();
                            Console.ReadKey();
                        }
                        if (Landscape[x, y].Warren.WarrenHasDiedOut())
                        {
                            Landscape[x, y].Warren = null;
                            WarrenCount--;
                        }
                    }
                }
            }
            for (int x = 0; x < LandscapeSize; x++)
            {
                for (int y = 0; y < LandscapeSize; y++)
                {
                    if (Landscape[x, y].Den != null)
                    {
                        if (ShowDetail)
                        {
                            Console.WriteLine("Fox at (" + x + "," + y + "): ");
                        }
                        Landscape[x, y].Den.AdvanceGeneration(ShowDetail);
                   
                        if (ShowDetail)
                        {
                            Landscape[x, y].Den.Inspect();
                        }
                    }
                }
            }
            if (ShowDetail)
            {
                Console.ReadKey();
            }
            DrawLandscape();
            Console.WriteLine();
        }


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. So I have made the following changes:

private void FoxesEatRabbitsInWarren(int WarrenX, int WarrenY)
        {
            int FoodConsumed;
            int PercentToEat;
            double Dist;
            int RabbitsToEat;
            int RabbitCountAtStartOfPeriod = Landscape[WarrenX, WarrenY].Warren.GetRabbitCount();
            for (int FoxX = 0; FoxX < LandscapeSize; FoxX++)
            {
                for (int FoxY = 0; FoxY < LandscapeSize; FoxY++)
                {
                    if (Landscape[FoxX, FoxY].Den != null)
                    {
                        for (int f = 0; f < Landscape[FoxX, FoxY].Den.GetFoxCount(); f++ )
                        {
                            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].Den.Foxs[f].GiveFood(FoodConsumed);
                            if (ShowDetail)
                            {
                                Console.WriteLine("  " + FoodConsumed + " rabbits eaten by fox at (" + FoxX + "," + FoxY + ").");
                            }
                        }
                    }
                }
            }
        }