示例#1
0
        public override bool Propagate()
        {
            Cell          cell    = Enviroment.GetCellByCoords(PositionY, PositionX);
            List <Animal> animals = cell.Animals;
            int           progeny = 0;
            Random        rand    = new Random();
            int           averageWeight;
            bool          isFemale;

            if (CheckCellForEat(cell) && cell.Animals.Count < 6)
            {
                foreach (Animal animal in animals)
                {
                    if (CheckAnimalForPropagate(animal))
                    {
                        if (animal.IsFemale)
                        {
                            progeny = animal.Progeny;
                        }
                        if (this.isFemale)
                        {
                            progeny = Progeny;
                        }
                        for (int i = 0; i < progeny; i++)
                        {
                            if (animal.AverageWeight >= AverageWeight)
                            {
                                averageWeight = rand.Next(AverageWeight, animal.AverageWeight);
                            }
                            else
                            {
                                averageWeight = rand.Next(animal.AverageWeight, AverageWeight);
                            }
                            isFemale = (rand.Next(0, 2) == 0 ? true : false);
                            Mammal child = new Mammal(PositionX, PositionY, IsPredator, IsSwimming, averageWeight, Habitat, isFemale);
                            cell.AddAnimal(child);
                            Enviroment.AddAnimal(child);
                        }
                        return(true);
                    }
                }
            }
            return(false);
        }
示例#2
0
 protected override bool CheckAnimalForPropagate(Animal animal)
 {
     if (animal is Mammal)
     {
         Mammal mammal = (Mammal)animal;
         int    k      = 0;
         for (int i = 0; i < mammal.Habitat.Length; i++)
         {
             for (int j = 0; j < Habitat.Length; j++)
             {
                 if (mammal.Habitat[i] == Habitat[j])
                 {
                     k++;
                 }
             }
         }
         if (k > 0 && mammal.IsFemale != IsFemale && mammal.IsPredator == IsPredator && mammal.IsSwimming == IsSwimming && Math.Abs(mammal.AverageWeight - AverageWeight) < 50 && mammal != this)
         {
             return(true);
         }
     }
     return(false);
 }
示例#3
0
        public void Draw()
        {
            Pen myPen = new Pen(SystemColors.Highlight, 2);

            g.DrawRectangle(myPen, new Rectangle(x0 - 5, y0 - 5, Enviroment.M * k + 10, Enviroment.N * k + 10));
            int typeOfCell;

            for (int i = 1; i <= Enviroment.N; i++)
            {
                for (int j = 1; j <= Enviroment.M; j++)
                {
                    typeOfCell = Enviroment.GetCellByCoords(i, j).Type;
                    switch (typeOfCell)
                    {
                    case 0:
                        myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(185, 150, 130));
                        //myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(135, 65, 0));
                        g.FillRectangle(myBrush, new Rectangle(x0 + (j - 1) * k, y0 + (i - 1) * k, k, k));
                        break;

                    case 1:
                        myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(155, 230, 150));
                        //myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(20, 150, 0));
                        g.FillRectangle(myBrush, new Rectangle(x0 + (j - 1) * k, y0 + (i - 1) * k, k, k));
                        break;

                    case 2:
                        myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(170, 215, 240));
                        //myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(0, 10, 220));
                        g.FillRectangle(myBrush, new Rectangle(x0 + (j - 1) * k, y0 + (i - 1) * k, k, k));
                        break;
                    }
                }
            }

            List <Animal> fishes = Enviroment.GetAnimals(0);

            myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.WhiteSmoke);
            g.FillRectangle(myBrush, new Rectangle(10, 400, 150, 20));
            String s = "Рыбы: " + Enviroment.NumberOfFishes;

            g.DrawString(s, aFont, Brushes.Black, 10, 400);
            myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(0, 50, 50));
            g.FillEllipse(myBrush, 10, 420, 15, 15);
            myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(0, 0, 150));
            g.FillEllipse(myBrush, 10, 440, 15, 15);
            myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.WhiteSmoke);
            g.FillRectangle(myBrush, new Rectangle(40, 420, 150, 40));
            s = "Хищники:";
            g.DrawString(s, aFont, Brushes.Black, 40, 420);
            s = "Травоядные:";
            g.DrawString(s, aFont, Brushes.Black, 40, 440);

            s = "" + fishes.FindAll(x => x.IsPredator).Count;
            g.DrawString(s, aFont, Brushes.Black, 150, 420);
            s = "" + fishes.FindAll(x => !x.IsPredator).Count;
            g.DrawString(s, aFont, Brushes.Black, 150, 440);

            for (int i = 0; i < Enviroment.NumberOfFishes; i++)
            {
                Fish fish = (Fish)fishes.ElementAt(i);
                int  x = fish.PositionX, y = fish.PositionY;
                if (fish.IsPredator)
                {
                    myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(0, 50, 50));
                }
                else
                {
                    myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(0, 0, 150));
                }
                g.FillEllipse(myBrush, x0 + (x - 1) * k + 1, y0 + (y - 1) * k + 1, k / 3, k / 3);
            }

            List <Animal> birds = Enviroment.GetAnimals(1);

            /*
             * String s2 = "Птицы: " + Enviroment.NumberOfBirds + " из них хищников: " + birds.FindAll(x => x.IsPredator).Count;
             * myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.WhiteSmoke);
             * g.FillRectangle(myBrush, new Rectangle(10, 420, 270, 450));
             * g.DrawString(s2, aFont, Brushes.Black, 10, 420);*/
            myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.WhiteSmoke);
            g.FillRectangle(myBrush, new Rectangle(10, 470, 150, 20));
            s = "Птицы: " + Enviroment.NumberOfBirds;

            g.DrawString(s, aFont, Brushes.Black, 10, 470);
            myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(175, 75, 0));
            g.FillEllipse(myBrush, 10, 490, 15, 15);
            myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(255, 105, 0));
            g.FillEllipse(myBrush, 10, 510, 15, 15);
            myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.WhiteSmoke);
            g.FillRectangle(myBrush, new Rectangle(40, 490, 150, 40));
            s = "Хищники:";
            g.DrawString(s, aFont, Brushes.Black, 40, 490);
            s = "Травоядные:";
            g.DrawString(s, aFont, Brushes.Black, 40, 510);

            s = "" + birds.FindAll(x => x.IsPredator).Count;
            g.DrawString(s, aFont, Brushes.Black, 150, 490);
            s = "" + birds.FindAll(x => !x.IsPredator).Count;
            g.DrawString(s, aFont, Brushes.Black, 150, 510);
            for (int i = 0; i < Enviroment.NumberOfBirds; i++)
            {
                Bird bird = (Bird)birds.ElementAt(i);
                int  x = bird.PositionX, y = bird.PositionY;
                if (bird.IsPredator)
                {
                    myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(175, 75, 0));
                }
                else
                {
                    myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(255, 105, 0));
                }

                g.FillEllipse(myBrush, x0 + (x - 1) * k + k / 3 + 1, y0 + (y - 1) * k + k / 3 + 1, k / 3, k / 3);
            }

            List <Animal> mammals = Enviroment.GetAnimals(2);

            /*
             * String s3 = "Млекопитающие: " + Enviroment.NumberOfMammals + " из них хищников: " + mammals.FindAll(x => x.IsPredator).Count;
             * myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.WhiteSmoke);
             * g.FillRectangle(myBrush, new Rectangle(10, 440, 270, 460));
             * g.DrawString(s3, aFont, Brushes.Black, 10, 440);*/
            myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.WhiteSmoke);
            g.FillRectangle(myBrush, new Rectangle(10, 540, 150, 20));
            s = "Млекопитающие: " + Enviroment.NumberOfMammals;

            g.DrawString(s, aFont, Brushes.Black, 10, 540);
            myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(150, 0, 0));
            g.FillEllipse(myBrush, 10, 560, 15, 15);
            myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(255, 0, 0));
            g.FillEllipse(myBrush, 10, 580, 15, 15);
            myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.WhiteSmoke);
            g.FillRectangle(myBrush, new Rectangle(40, 560, 150, 40));
            s = "Хищники:";
            g.DrawString(s, aFont, Brushes.Black, 40, 560);
            s = "Травоядные:";
            g.DrawString(s, aFont, Brushes.Black, 40, 580);

            s = "" + mammals.FindAll(x => x.IsPredator).Count;
            g.DrawString(s, aFont, Brushes.Black, 150, 560);
            s = "" + mammals.FindAll(x => !x.IsPredator).Count;
            g.DrawString(s, aFont, Brushes.Black, 150, 580);
            for (int i = 0; i < Enviroment.NumberOfMammals; i++)
            {
                Mammal mammal = (Mammal)mammals.ElementAt(i);
                int    x = mammal.PositionX, y = mammal.PositionY;
                if (mammal.IsPredator)
                {
                    myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(150, 0, 0));
                }
                else
                {
                    myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(255, 0, 0));
                }

                g.FillEllipse(myBrush, x0 + (x - 1) * k + k / 3 + 1, y0 + (y - 1) * k + 1, k / 3, k / 3);
            }
        }