public override bool Eat() { Cell cell = Enviroment.GetCellByCoords(PositionY, PositionX); List <Animal> animals = cell.Animals; int food = cell.Food; if (CheckCellForEat(cell)) { if (IsPredator) { foreach (Animal animal in animals) { if (animal != this) { if (animal is Bird) { Bird bird = (Bird)animal; if (bird.IsFlying) { break; } } if (averageWeight > animal.AverageWeight) { cell.RemoveAnimal(animal); Enviroment.RemoveAnimal(animal); currentPortionOfFood += AverageWeight; } if (currentPortionOfFood >= RequiredPortionOfFood) { currentPortionOfFood = 0; return(true); } } } } else { if (food >= RequiredPortionOfFood) { food -= RequiredPortionOfFood; Enviroment.GetCellByCoords(PositionY, PositionX).Food = food; return(true); } } } currentPortionOfFood = 0; if (currentStepsWithoutFood >= PossibleStepsWithoutFood) { cell.RemoveAnimal(this); Enviroment.RemoveAnimal(this); return(false); } return(false); }
public Cell FindCellForPropagate() { Random rand = new Random(); Cell[] rangeCells = Enviroment.GetRangeCells(RadiusOfSight, PositionY, PositionX); Cell cell = Enviroment.GetCellByCoords(PositionY, PositionX), nextCell; int minX = PositionX, minY = PositionY; for (int i = 0; i < rangeCells.Length; i++) { if (rangeCells[i] != null && CheckCellForEat(rangeCells[i])) { List <Animal> animals = rangeCells[i].Animals; for (int j = 0; j < animals.Count; j++) { Animal animal = animals.ElementAt(j); if (CheckAnimalForPropagate(animal)) { if (i < 9) { cell = rangeCells[i]; return(cell); } else { int min = RadiusOfSight * 2; for (int k = 0; k < 9; k++) { if (rangeCells[k] != null && CheckCellForEat(rangeCells[i])) { nextCell = rangeCells[k]; if (((Math.Abs(rangeCells[i].PositionX - nextCell.PositionX) + Math.Abs(rangeCells[i].PositionY - nextCell.PositionY)) < min) && nextCell is WaterCell) { min = Math.Abs(rangeCells[i].PositionX - nextCell.PositionX) + Math.Abs(rangeCells[i].PositionY - nextCell.PositionY); minX = nextCell.PositionX; minY = nextCell.PositionY; } } } } cell = Enviroment.GetCellByCoords(minY, minX); } if (minX == PositionX && minY == PositionY) { int next = rand.Next(9); if (rangeCells[next] != null && CheckCellForEat(rangeCells[next])) { cell = rangeCells[next]; } } } } } return(cell); }
public override bool Eat() { Cell cell = Enviroment.GetCellByCoords(PositionY, PositionX); if (cell != null) { List <Animal> animals = cell.Animals; int food = cell.Food; if (IsPredator) { foreach (Animal animal in animals) { if (animal != this && (animal is Fish)) { if (averageWeight > animal.AverageWeight) { cell.RemoveAnimal(animal); Enviroment.RemoveAnimal(animal); currentPortionOfFood += AverageWeight; } if (currentPortionOfFood >= RequiredPortionOfFood) { currentPortionOfFood = 0; return(true); } } } } else { if (food >= RequiredPortionOfFood) { food -= RequiredPortionOfFood; Enviroment.GetCellByCoords(PositionY, PositionX).Food = food; return(true); } } currentPortionOfFood = 0; if (currentStepsWithoutFood >= PossibleStepsWithoutFood) { cell.RemoveAnimal(this); Enviroment.RemoveAnimal(this); return(false); } } return(false); }
public override bool Move(Cell cellForMove) { Random rand = new Random(); Cell cell = Enviroment.GetCellByCoords(PositionY, PositionX); if (CheckCell(cellForMove)) { currentStepsWithoutFood++; PositionX = cellForMove.PositionX; PositionY = cellForMove.PositionY; cell.RemoveAnimal(this); cellForMove.AddAnimal(this); return(true); } return(false); }
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); Fish child = new Fish(PositionX, PositionY, IsPredator, averageWeight, isFemale); cell.AddAnimal(child); Enviroment.AddAnimal(child); } return(true); } } } return(false); }
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); } }