コード例 #1
0
        private Route BestRoute(Vector2 origin, out Vector2?destination)
        {
            List <Route> posibleRoutes = new List <Route>();

            foreach (Lighthouse lighthouse in Lighthouses.Where(x => x.Position != origin && x.IdOwner != this.Id))
            {
                Route suggestedRoute = TraceRoute(origin, lighthouse.Position);
                if (suggestedRoute == null)
                {
                    continue;
                }

                posibleRoutes.Add(suggestedRoute);
            }

            if (!posibleRoutes.Any())
            {
                Route suggestedRoute = TraceRoute(origin, Lighthouses.First().Position);
                if (suggestedRoute == null)
                {
                    //Caso excepcional. No hay ningún faro accesible, dirigirse a casilla aleatoria accesible
                    //Por ahora podría generar excepciones
                    destination = null;
                    return(null);
                }

                posibleRoutes.Add(suggestedRoute);
            }

            Route bestRoute = posibleRoutes.OrderByDescending(x => x.Way.Sum(w => w.Energy)).First();

            destination = bestRoute.Destination.Position;

            return(bestRoute);
        }
コード例 #2
0
        private bool AttackLighthouse(IDecision decision)
        {
            IEnumerable <ILighthouse> actualPositionLighthouses = Lighthouses.Where(x => x.Position == Position);

            if (actualPositionLighthouses.Any())
            {
                ILighthouse actualPositionLighthouse = actualPositionLighthouses.Single();
                if (actualPositionLighthouse.IdOwner != this.Id)
                {
                    double maxAllowedEnergy = (this.Energy * MAX_ENERGY_CONSUMPTION_COEFFICIENT);
                    if (actualPositionLighthouse.Energy > maxAllowedEnergy)
                    {
                        return(false);
                    }

                    int loadedEnergy;
                    if (actualPositionLighthouse.Energy == 0)
                    {
                        double averageEnergy = Lighthouses.Average(x => x.Energy);
                        if (averageEnergy == 0)
                        {
                            loadedEnergy = (int)Math.Truncate(maxAllowedEnergy);
                        }
                        else
                        {
                            loadedEnergy = (int)Math.Truncate(((averageEnergy * ENERGY_INCREMENT) + averageEnergy));
                        }
                    }
                    else
                    {
                        loadedEnergy = (actualPositionLighthouse.Energy * 2) + (int)Math.Truncate((actualPositionLighthouse.Energy * ENERGY_INCREMENT));
                    }

                    if (loadedEnergy > maxAllowedEnergy)
                    {
                        return(false);
                    }

                    decision.Action = PlayerActions.Attack;
                    decision.Energy = loadedEnergy;

                    return(true);
                }
            }

            return(false);
        }