Beispiel #1
0
        public override void Visit(Grass grass)
        {
            if (grass == null)
            {
                throw new ArgumentNullException("grass");
            }

            if (grass.IsAlive == false)
            {
                return;
            }

            GainWeight(1.00);
            grass.Deactivate();
            base.Visit(grass);
        }
Beispiel #2
0
        public override void Visit(Grass grass)
        {
            if (grass == null)
            {
                throw new ArgumentNullException("grass");
            }

            if (grass.IsAlive == false)
            {
                return;
            }

            GainWeight(1.00);
            grass.Deactivate();
            base.Visit(grass);
        }
Beispiel #3
0
        private IEnumerable <Action> Act(Animal animal)
        {
            if (animal.IsAlive == false)
            {
                yield break;
            }

            animal.GetOlder();
            animal.LoseWeight(animal.Weight * 0.25);

            // Dies from overpopulation or starvation?
            int    max = this.savannah.NumTiles;
            int    animals = this.savannah.Animals.Count(a => a.GetType() == animal.GetType());
            double k = Random.NextDouble();
            bool   dies = k <((1.0 * animals) / (1.0 * max)) || animal.Age> Random.Next(5, 20);

            if (dies)
            {
                yield return(animal.Deactivate);

                yield break;
            }

            for (int dy = -1; dy <= 1; dy++)
            {
                for (int dx = -1; dx <= 1; dx++)
                {
                    int x = animal.X + dx;
                    int y = animal.Y + dy;

                    Grass  neighbourGrass  = this.savannah.Grasses.SingleOrDefault(g => g.X == x && g.Y == y);
                    Animal neighbourAnimal = this.savannah.Animals.SingleOrDefault(a => a.IsAlive && a.X == x && a.Y == y);

                    if (neighbourGrass != null)
                    {
                        yield return(() => neighbourGrass.Accept(animal));
                    }

                    if (neighbourAnimal != null)
                    {
                        yield return(() => animal.Accept(neighbourAnimal));
                    }
                }
            }
        }