Пример #1
0
        /**
         * Randomly populate the field with foxes and rabbits.
         */
        private void Populate()
        {
            Random rand = Randomizer.GetRandomGenerator();

            Field.Clear();
            for (int row = 0; row < Field.Depth; row++)
            {
                for (int col = 0; col < Field.Width; col++)
                {
                    if (rand.NextDouble() <= FOX_CREATION_PROBABILITY)
                    {
                        Location location = new Location(row, col);
                        Fox      fox      = new Fox(true, Field, location);
                        foxes.Add(fox);
                    }
                    else if (rand.NextDouble() <= RABBIT_CREATION_PROBABILITY)
                    {
                        Location location = new Location(row, col);
                        Rabbit   rabbit   = new Rabbit(true, Field, location);
                        rabbits.Add(rabbit);
                    }
                    // else leave the location empty.
                }
            }
        }
Пример #2
0
        /**
         * Check whether or not this fox is to give birth at this step.
         * New births will be made into free adjacent locations.
         * @param newFoxes A list to return newly born foxes.
         */
        public void GiveBirth(List <Fox> newFoxes)
        {
            List <Location> free = field.GetFreeAdjacentLocations(location);

            int births = Breed();

            for (int b = 0; b < births && free.Count > 0; b++)
            {
                Location loc = free[0];
                free.RemoveAt(0);

                Fox young = new Fox(false, field, loc);
                newFoxes.Add(young);
            }
        }
Пример #3
0
        /**
         * Run the simulation from its current state for a single step. Iterate
         * over the whole field updating the state of each fox and rabbit.
         */
        public void SimulateOneStep()
        {
            step++;

            // Provide space for newborn rabbits.
            List <Rabbit> newRabbits = new List <Rabbit>();

            // Let all rabbits act.
            for (int r = 0; r < rabbits.Count; r++)
            {
                Rabbit rabbit = rabbits[r];
                rabbit.Run();
                if (!rabbit.IsAlive())
                {
                    rabbits.Remove(rabbit);
                }
                else
                {
                    rabbit.GiveBirth(newRabbits);
                }
            }

            // Provide space for newborn foxes.
            List <Fox> newFoxes = new List <Fox>();

            // Let all foxes act.
            for (int f = 0; f < foxes.Count; f++)
            {
                Fox fox = foxes[f];
                fox.Hunt();
                if (!fox.IsAlive())
                {
                    foxes.Remove(fox);
                }
                else
                {
                    fox.GiveBirth(newFoxes);
                }
            }

            // Add the newly born foxes and rabbits to the main lists.
            rabbits.AddRange(newRabbits);
            foxes.AddRange(newFoxes);

            //view.ShowStatus(step, field);
        }