Exemplo n.º 1
0
        /// <summary>
        /// Simulate one year on the hill
        /// </summary>
        public void RunAYear()
        {
            HashSet <Bunny> deadBunniesList = new HashSet <Bunny>();

            for (int i = 0; i < _bunniesList.Count; i++)
            {
                Bunny curBunny = _bunniesList.ElementAt(i);
                //Make each bunny a year older
                curBunny.GrowAYear();

                //Check if a bunny is too old
                if (curBunny.Age > LIFESPAN)
                {
                    //If the bunny's life is over, the bunny is added to the dead bunnies list :(
                    deadBunniesList.Add(curBunny);
                }
            }
            //Remove all the dead bunnies
            foreach (Bunny bunny in deadBunniesList)
            {
                this._bunniesList.Remove(bunny);
            }

            /*Mate the bunnies to create new bunnies*/
            IEnumerable <Bunny> maleAdultBunnies   = this.GetMaleAdultBunnies();
            IEnumerable <Bunny> femaleAdultBunnies = this.GetFemaleAdultBunnies();
            ICollection <Bunny> newBunniesList     = new HashSet <Bunny>();

            foreach (Bunny fatherBunny in maleAdultBunnies)
            {
                foreach (Bunny motherBunny in femaleAdultBunnies)
                {
                    Color motherColor = motherBunny.BunnyColor;
                    Bunny babyBunny   = BunnyCreator.CreateBunny(motherColor);
                    this._bunniesList.Add(babyBunny);
                    newBunniesList.Add(babyBunny);
                }
            }

            //Print the current state
            this.UpdateBunniesCount();
            Console.WriteLine("There are " + this._maleBunniesCount + " male bunnies, " + this._femaleBunniesCount + " female bunnies (total of " + this._bunniesList.Count + " bunnies).");
            foreach (Bunny bunny in deadBunniesList)
            {
                Console.WriteLine("Bunny " + bunny.BunnyName + " died :(");
            }
            foreach (Bunny bunny in newBunniesList)
            {
                Console.WriteLine("Bunny " + bunny.BunnyName + " was born!");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create the the initial state of the hill.
        /// </summary>
        public Hill()
        {
            this._maleBunniesCount   = 0;
            this._femaleBunniesCount = 0;

            this._bunniesList = new HashSet <Bunny>();

            //Create the initial male bunnies
            for (int i = 0; i < MALE_INITIAL_POPULATION; i++)
            {
                Bunny maleBunny = BunnyCreator.CreateBunny(Sex.Male);
                _bunniesList.Add(maleBunny);
            }

            //Create the initial female bunnies
            for (int i = 0; i < FEMALE_INITIAL_POPULATION; i++)
            {
                Bunny femaleBunny = BunnyCreator.CreateBunny(Sex.Female);
                _bunniesList.Add(femaleBunny);
            }
        }