예제 #1
0
        /**
         * Check whether or not this rabbit is to give birth at this step.
         * New births will be made into free adjacent locations.
         * @param newRabbits A list to return newly born rabbits.
         */
        public void GiveBirth(List <Rabbit> newRabbits)
        {
            // New rabbits 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);

                Rabbit young = new Rabbit(false, field, loc);
                newRabbits.Add(young);
            }
        }
예제 #2
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);
        }
예제 #3
0
        /**
         * Look for rabbits adjacent to the current location.
         * Only the first live rabbit is eaten.
         * @return Where food was found, or null if it wasn't.
         */
        private Location FindFood()
        {
            List <Location> adjacent = field.AdjacentLocations(location);

            foreach (Location where in adjacent)
            {
                Object animal = field.GetAnimalAt(where);

                if (animal is Rabbit)
                {
                    Rabbit rabbit = (Rabbit)animal;
                    if (rabbit.IsAlive())
                    {
                        rabbit.SetDead();
                        foodLevel = RABBIT_FOOD_VALUE;
                        return(where);
                    }
                }
            }
            return(null);
        }