protected override bool TryExecuteTurn(Randomizer randomizer, IGameLayer gameLayer, GameState state)
        {
            var damagedBuildings = state.ResidenceBuildings.Where(x => x.Health < ThresholdHealth)
                                   .OrderBy(x => x.Health)
                                   .ToArray();

            if (!damagedBuildings.Any())
            {
                return(false);
            }

            var affordedBuildings = damagedBuildings.Where(x =>
            {
                var blueprint = state.AvailableResidenceBuildings.Single(p => p.BuildingName == x.BuildingName);
                var afforded  = blueprint.MaintenanceCost < state.Funds;
                return(afforded);
            }).ToArray();

            BuiltResidenceBuilding building;

            if (PrioritizeWeakest)
            {
                building = damagedBuildings.First();
                if (!affordedBuildings.Contains(building))
                {
                    // Can't afford it, then skip hole strategy to save money for next time
                    return(false);
                }
            }
            else
            {
                // Can afford maintenance on the weakest building
                building = affordedBuildings.FirstOrDefault();
                if (building == null)
                {
                    // Can't afford it, must wait until can afford
                    return(false);
                }
            }


            var position = building.Position;

            gameLayer.Maintenance(position, state.GameId);
            return(true);
        }
Beispiel #2
0
        public static void ExecuteAction(this IGameLayer gameLayer, GameActions action, Position position, object argument = null)
        {
            var state = gameLayer.GetState();

            switch (action)
            {
            case GameActions.StartBuild:
                var buildingName = (string)argument ?? throw new ArgumentNullException(nameof(argument));
                gameLayer.StartBuild(position, buildingName, state.GameId);
                break;

            case GameActions.Build:
                gameLayer.Build(position, state.GameId);
                break;

            case GameActions.Maintenance:
                gameLayer.Maintenance(position, state.GameId);
                break;

            case GameActions.BuyUpgrade:
                var upgradeName = (string)argument ?? throw new ArgumentNullException(nameof(argument));
                gameLayer.BuyUpgrade(position, upgradeName, state.GameId);
                break;



            case GameActions.Wait:
                gameLayer.Wait(state.GameId);
                break;

            case GameActions.None:
                throw new NotSupportedException();

            default:
                throw new NotImplementedException();
            }
        }