public Randomizer(IGameLayer gameLayer, ILoggerFactory loggerFactory, TurnStrategyBase strategy = null)
        {
            _gameLayer = gameLayer;
            _gameState = gameLayer.GetState();
            _logger    = loggerFactory.CreateLogger <Randomizer>();

            if (strategy == null)
            {
                _strategy = TurnStrategyBase.Build(loggerFactory)
                            .Append <BuildUtilityCloseToResidencesTurnStrategy>(c =>
                {
                    c.BuildingName         = "Mall";
                    c.MaxNumberOfBuildings = 3;
                })
                            .Append <BuildUtilityCloseToResidencesTurnStrategy>(c =>
                {
                    c.BuildingName         = "Park";
                    c.MaxNumberOfBuildings = 2;
                })
                            .Append <BuildBuildingWhenCloseToPopMaxTurnStrategy>()
                            .Append <BuyUpgradeTurnStrategy>()
                            .Append <MaintenanceWhenBuildingIsGettingDamagedTurnStrategy>()
                            .Append <BuildWhenHasBuildingsUnderConstructionTurnStrategy>()
                            .Append <AdjustBuildingTemperaturesTurnStrategy>()
                            .Append <SingletonBuildingTurnStrategy>()
                            .Compile();
            }
            else
            {
                _strategy = strategy;
            }
            _logger.LogInformation($"Strategy: {Environment.NewLine + _strategy}");
        }
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();
            }
        }
Beispiel #3
0
        public static IEnumerable <GameActions> GetPossibleActions(this IGameLayer gameLayer, bool includeWait = true)
        {
            var state = gameLayer.GetState();

            // If has money availble for a building, then can start building
            var blueprints = state.GetBuildingBlueprints().ToArray();

            if (blueprints.Any(x => x.Cost <= state.Funds))
            {
                // Only build if will have plenty of money left...

                var fraction = state.Funds / blueprints.Average(x => x.Cost);
                if (fraction >= 2)
                {
                    yield return(GameActions.StartBuild);
                }
                else
                {
                }
            }

            // If has non-completed buildings, then can build
            if (state.GetBuildingsUnderConstruction().Any(x => x.BuildProgress < 100))
            {
                yield return(GameActions.Build);
            }

            // If has completed buildings and they are low on hp, then can do maintenance
            var damagedBuildings = state.ResidenceBuildings.Where(x => x.Health < 50).ToArray();

            if (damagedBuildings.Any())
            {
                var buildingTypes       = damagedBuildings.Select(x => x.BuildingName).Distinct().ToArray();
                var affordedMaintenance = state.AvailableResidenceBuildings
                                          .Where(x => buildingTypes.Contains(x.BuildingName))
                                          .Where(x => x.MaintenanceCost < state.Funds)
                                          .ToArray();
                if (affordedMaintenance.Any())
                {
                    yield return(GameActions.Maintenance);
                }
            }

            // If has completed buildings and they are low on hp, then can do maintenance
            if (state.GetCompletedBuildings().Any())
            {
                if (state.AvailableUpgrades.Any(x => x.Cost < state.Funds))
                {
                    var fraction = state.Funds / state.AvailableUpgrades.OrderByDescending(x => x.Cost).First().Cost;
                    if (fraction >= 6)
                    {
                        // todo: enable when logic is more stable
                        //yield return GameActions.BuyUpgrade;
                    }
                    else
                    {
                    }
                }
            }

            if (includeWait)
            {
                yield return(GameActions.Wait);
            }
        }