示例#1
0
        /// <summary>
        /// Distributes the list of animals in the train into the wagons where they fit in and are safe to be added to.
        /// </summary>
        public void DistributeAnimals()
        {
            Wagon wagon;

            foreach (Carnivore carnivore in _animals.OfType <Carnivore>())
            {
                wagon = new Wagon();
                wagon.AddAnimal(carnivore);
                _wagons.Add(wagon);
            }

            foreach (Herbivore herbivore in _animals.OfType <Herbivore>())
            {
                if (_wagons.Count == 0)
                {
                    _wagons.Add(new Wagon());
                }

                for (int i = 0; i < _wagons.Count(); i++)
                {
                    wagon = _wagons.ElementAt(i);

                    if (wagon.WillAnimalFit(herbivore) && wagon.IsSafeToAddAnimal(herbivore))
                    {
                        wagon.AddAnimal(herbivore);
                        break;
                    }
                    else if (i == (_wagons.Count() - 1))
                    {
                        wagon = new Wagon();
                        wagon.AddAnimal(herbivore);
                        _wagons.Add(wagon);
                        break;
                    }
                    else if (i < _wagons.Count())
                    {
                        continue;
                    }
                }
            }

            WagonCount = _wagons.Count;
            Wagons     = _wagons;
        }
示例#2
0
        private static List <Wagon> PlaceCarnivores(List <Animal> animals)
        {
            List <Wagon> wagons = new List <Wagon>();
            Wagon        wagon;

            foreach (Animal animal in animals)
            {
                if (animal.Placed == false && animal.Diet == Diet.Carnivore)
                {
                    wagon = new Wagon();
                    wagon.AddAnimal(animal);
                    wagons.Add(wagon);
                }
            }
            return(wagons);
        }