Ejemplo n.º 1
0
        public void AssessAsteroid(Asteroid other)
        {
            var angle = CalculateAngle(other);

            if (VisibleAngles.ContainsKey(angle))
            {
                VisibleAngles[angle].Add(other);
            }
            else
            {
                VisibleAngles[angle] = new List <Asteroid>()
                {
                    other
                };
            }
        }
Ejemplo n.º 2
0
        public (double, Asteroid) FireLaser()
        {
            // laser starts at 0 degrees with no hits
            // laser must increase its angle, then fire at the closest asteroid on the next available angle
            // if there are no more asteroids to shoot, then return an appropriate error message
            var asteroidCount = VisibleAngles.Values.SelectMany(a => a).ToList().Count;

            if (asteroidCount == 0)
            {
                return(-1, null);
            }
            // Console.WriteLine($"{asteroidCount} asteroids remaining to choose from");
            var nextAngle = VisibleAngles
                            .Select(angle => angle)
                            .Where(angle => angle.Key > LaserAngle && angle.Value.Count > 0)
                            .OrderBy(angle => angle.Key)
                            .FirstOrDefault();

            if (nextAngle.Value == null || nextAngle.Value.Count == 0)
            {
                // in this case we have no more angles with asteroids in to destroy, start another loop around
                Console.WriteLine("Starting another loop around");
                LaserAngle = -0.1d;
                //recurse
                return(FireLaser());
            }

            var asteroidHit = nextAngle.Value
                              .Select(asteroid => asteroid)
                              .OrderBy(asteroid => asteroid.DistanceFrom(this))
                              .First();

            VisibleAngles[nextAngle.Key].Remove(asteroidHit);
            LaserAngle = nextAngle.Key;
            return(nextAngle.Key, asteroidHit);
        }