public void LocateEnemy(Field field, List <Animal> searchList) { double ultimateLocation = _math.Vector(0, field.Width, 0, field.Height); var carnivoreList = searchList.FindAll(a => a.IsHerbivore == false); var herbivoreList = searchList.FindAll(a => a.IsHerbivore == true); foreach (var carnivore in carnivoreList) { foreach (var herbivore in herbivoreList) { var location = _math.Vector(herbivore.CoordinateX, carnivore.CoordinateX, herbivore.CoordinateY, carnivore.CoordinateY); if (location < ultimateLocation) { if (location < NumParameters.VisionRange) { ultimateLocation = location; SetEnemies(carnivore, herbivore, field); } else { ResetEnemies(carnivore, herbivore, field); } } } ultimateLocation = _math.Vector(0, field.Width, 0, field.Height); } }
public List <Animal> MoveWithEnemies(Animal herbivore, List <Animal> searchList, Field field) { double closestLocation = 0; int bestStepX = herbivore.CoordinateX; int bestStepY = herbivore.CoordinateY; for (int coordX = NumParameters.MovingNegative; coordX < NumParameters.MovingPositive; coordX++) { for (int coordY = NumParameters.MovingNegative; coordY < NumParameters.MovingPositive; coordY++) { int nextStepX = herbivore.CoordinateX + coordX; int nextStepY = herbivore.CoordinateY + coordY; var validMove = _validator.ValidateMove(nextStepX, nextStepY, field) && !_validator.AnimalExists(nextStepX, nextStepY, field); if (validMove) { double betterLocation = _math.Vector(nextStepX, nextStepY, herbivore.ClosestEnemy.CoordinateX, herbivore.ClosestEnemy.CoordinateY); if (betterLocation >= closestLocation) { closestLocation = betterLocation; bestStepX = nextStepX; bestStepY = nextStepY; } } } } _genericAnimal.TakeAStep(bestStepX, bestStepY, herbivore, field); return(searchList); }
public List <Animal> MoveWithEnemies(Animal carnivore, List <Animal> searchList, Field field) { var initialLocation = _math.Vector(carnivore.CoordinateX, carnivore.CoordinateY, carnivore.ClosestEnemy.CoordinateX, carnivore.ClosestEnemy.CoordinateY); int bestStepX = carnivore.CoordinateX; int bestStepY = carnivore.CoordinateY; for (int coordX = NumParameters.MovingNegative; coordX < NumParameters.MovingPositive; coordX++) { for (int coordY = NumParameters.MovingNegative; coordY < NumParameters.MovingPositive; coordY++) { int nextStepX = carnivore.CoordinateX + coordX; int nextStepY = carnivore.CoordinateY + coordY; var validMove = _validator.ValidateMove(nextStepX, nextStepY, field) && !_validator.CarnivoreExists(nextStepX, nextStepY, field) && (coordX != 0 && coordY != 0); if (validMove) { double betterLocation = _math.Vector(nextStepX, nextStepY, carnivore.ClosestEnemy.CoordinateX, carnivore.ClosestEnemy.CoordinateY); if (betterLocation <= initialLocation) { initialLocation = betterLocation; bestStepX = carnivore.CoordinateX + coordX; bestStepY = carnivore.CoordinateY + coordY; if (_validator.HerbivoreExists(bestStepX, bestStepY, field)) { EatVictim(carnivore, field); break; } } } } } _genericAnimal.TakeAStep(bestStepX, bestStepY, carnivore, field); return(searchList); }