protected override bool TryExecuteTurn(Randomizer randomizer, IGameLayer gameLayer, GameState state) { var upgrades = state.AvailableUpgrades; if (!upgrades.Any()) { return(false); } var completedBuildings = state.GetCompletedBuildings().OfType <BuiltResidenceBuilding>().ToArray(); if (!completedBuildings.Any()) { return(false); } var allowedUpgrades = IncludedUpgrades != null ? upgrades.Where(x => IncludedUpgrades.Contains(x.Name)).ToArray() : upgrades.ToArray(); var affordedUpgrades = allowedUpgrades.Where(x => x.Cost < state.Funds).ToArray(); var upgrade = affordedUpgrades.ElementAtOrDefault(randomizer.Random.Next(0, affordedUpgrades.Length)); if (upgrade == null) { // No allowed upgrades return(false); } var applicableBuildings = completedBuildings.Where(x => !x.Effects.Contains(upgrade.Name)).ToArray(); var building = applicableBuildings.ElementAtOrDefault(randomizer.Random.Next(0, applicableBuildings.Length)); if (building == null) { // No applicable buildings for chosen upgrade return(false); } var resultingFunds = state.Funds - upgrade.Cost; if (resultingFunds < 30000) { // Always leave a good margin on the bank account return(false); } var position = building.Position; gameLayer.BuyUpgrade(position, upgrade.Name, state.GameId); return(true); }
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(); } }