Exemple #1
0
        /// <summary>
        /// Find best step for escaping
        /// </summary>
        private Position FindBestStepToEscape(Herbivore herbivore, Field field)
        {
            var maxDistance = double.MinValue;
            var bestStep    = new Position(0, 0);
            var nextStep    = new Position();

            for (nextStep.X = -herbivore.MaxSpeed; nextStep.X <= herbivore.MaxSpeed; nextStep.X++)
            {
                for (nextStep.Y = -herbivore.MaxSpeed; nextStep.Y <= herbivore.MaxSpeed; nextStep.Y++)
                {
                    var newPosition = herbivore.Position.Clone().Add(nextStep);

                    if (!_validator.PositionIsOutOfField(field, newPosition) &&
                        (!_validator.PositionIsTaken(field, newPosition) || herbivore.Position.Equals(newPosition)))
                    {
                        var distance = _calculations.Distance(newPosition, herbivore.ClosestEnemy.Position);
                        if (distance > maxDistance)
                        {
                            maxDistance = distance;
                            bestStep    = nextStep.Clone();
                        }
                    }
                }
            }

            return(bestStep);
        }
Exemple #2
0
        /// <summary>
        /// Find best step for victim catching
        /// </summary>
        private Position FindBestStepForCatching(Carnivore carnivore, Field field)
        {
            var minDistance = double.MaxValue;
            var bestStep    = new Position(0, 0);
            var nextStep    = new Position();

            for (nextStep.X = -carnivore.MaxSpeed; nextStep.X <= carnivore.MaxSpeed; nextStep.X++)
            {
                for (nextStep.Y = -carnivore.MaxSpeed; nextStep.Y <= carnivore.MaxSpeed; nextStep.Y++)
                {
                    var newPosition = carnivore.Position.Clone().Add(nextStep);

                    if (!_validator.PositionIsOutOfField(field, newPosition) &&
                        (!_validator.PositionIsTakenByCarnivore(field, newPosition) || carnivore.Position.Equals(newPosition)))
                    {
                        var distance = _calculations.Distance(newPosition, carnivore.ClosestEnemy.Position);
                        if (distance < minDistance)
                        {
                            minDistance = distance;
                            bestStep    = nextStep.Clone();
                            if (carnivore.ClosestEnemy.Position.Equals(newPosition))
                            {
                                break;
                            }
                        }
                    }
                }
            }

            return(bestStep);
        }