Exemple #1
0
 public bool GetsEatenByCarnivore(Wagon wagon)
 {
     foreach (Animal animal in wagon.Animals)
     {
         if (animal.Diet == Diet.Carnivore && this.Magnitude <= animal.Magnitude)
         {
             return(true);
         }
     }
     return(false);
 }
Exemple #2
0
        private void CreateListBox(Wagon wagon)
        {
            ListBox listBox = new ListBox();

            listBox.Left   = 350;
            listBox.Top    = 100;
            listBox.Height = 150;
            listBox.Width  = 100;
            flpWagons.Controls.Add(listBox);
            foreach (Animal animal in wagon.Animals)
            {
                listBox.Items.Add(animal.ToString());
            }
        }
Exemple #3
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);
        }
Exemple #4
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;
        }
Exemple #5
0
        /*private Wagon GetWagonForThisAnimal(Animal animal)
         * {
         *  Wagon wagon = GetWagonForThisAnimal(animal);
         *
         *  if (wagon.IsSafeToAddAnimal(animal) && wagon.WillAnimalFit(animal))
         *  {
         *      wagon.AddAnimal(animal);
         *      return wagon;
         *  }
         *  else
         *  {
         *      wagon = new Wagon();
         *      wagon.AddAnimal(animal);
         *      _wagons.Add(wagon);
         *      return wagon;
         *  }
         * }*/

        public void DisplayWagons()
        {
            Console.WriteLine(_animals.OfType <Carnivore>().Count() + " Carnivores");
            Console.WriteLine(_animals.OfType <Herbivore>().Count() + " Herbivores");
            Console.WriteLine(string.Format($"\nComplete Distribution of wagons:"));

            for (int i = 0; i < _wagons.Count; i++)
            {
                Wagon wagon = _wagons.ElementAt(i);
                Console.WriteLine(string.Format($"\nWagon {i} Contains: \n"));
                foreach (Carnivore carnivore in wagon.Animals.OfType <Carnivore>())
                {
                    Console.WriteLine("C-" + carnivore.size + ", ");
                }
                foreach (Herbivore herbivore in wagon.Animals.OfType <Herbivore>())
                {
                    Console.WriteLine("H-" + herbivore.size + ", ");
                }
            }
        }