示例#1
0
        /**
         * Randomly populate the field with foxes and rabbits.
         */
        private void Populate()
        {
            Random rand = Randomizer.GetRandom();

            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.
                }
            }
        }
        /**
         * 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.
         */
        private void GiveBirth(List <Fox> newFoxes)
        {
            // New foxes are born into adjacent locations.
            // Get a list of adjacent free locations.
            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(newRabbits);
                if (!rabbit.IsAlive())
                {
                    rabbits.Remove(rabbit);
                }
            }

            // 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(newFoxes);
                if (!fox.IsAlive())
                {
                    foxes.Remove(fox);
                }
            }

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

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