Ejemplo n.º 1
0
        public ScoreResponse Run(TurnStrategyBase strategy = null)
        {
            // Make actions
            GameState state = GameLayer.GetState();

            _logger.LogInformation("Map: " + Environment.NewLine + state.MapToString());

            var randomizer = new Randomizer(GameLayer, _loggerFactory, strategy);

            while (state.Turn < state.MaxTurns)
            {
                PrintDebug_NewTurn(state);

                randomizer.HandleTurn();

                foreach (var message in state.Messages)
                {
                    _logger.LogInformation(message);
                }

                foreach (var error in state.Errors)
                {
                    _logger.LogError("Error: " + error);
                }
            }

            state = GameLayer.GetState();
            var score = GameLayer.GetScore(state.GameId);

            _logger.LogInformation("");
            _logger.LogInformation("");
            _logger.LogInformation($"Done with game: {state.GameId}");
            _logger.LogInformation("");
            _logger.LogInformation($"::SUMMARY::");
            _logger.LogInformation($"Funds: {state.Funds}");
            _logger.LogInformation($"Buildings: {state.GetCompletedBuildings().Count()}");
            _logger.LogInformation($"Upgrades: {state.GetCompletedBuildings().Sum(x => x.Effects.Count)}");
            _logger.LogInformation("");
            _logger.LogInformation($"::ACTIONS::");
            foreach (GameActions action in Enum.GetValues(typeof(GameActions)))
            {
                var count = state.ActionHistory.Count(x => x.Value == action);
                if (count < 1)
                {
                    continue;
                }
                var percent = count / (double)state.ActionHistory.Count;
                _logger.LogInformation($"\t{percent:P1}\t {action} ({count}/{state.ActionHistory.Count})");
            }
            _logger.LogInformation("");
            _logger.LogInformation($"::SCORE::");
            _logger.LogInformation($"Final score: {score.FinalScore}");
            _logger.LogInformation($"Co2: {score.TotalCo2}");
            _logger.LogInformation($"Pop: {score.FinalPopulation}");
            _logger.LogInformation($"Happiness: {score.TotalHappiness}");
            return(score);
        }
Ejemplo n.º 2
0
        public void EndGame()
        {
            var state = GameLayer?.GetState();

            if (state == null)
            {
                return;
            }

            var ended = state.Turn >= state.MaxTurns;

            if (ended)
            {
                // Automatic ending
            }
            else
            {
                // Ends a game prematurely
                // This is not needed to end a game that has been completed by playing all turns.
                GameLayer.EndGame(state.GameId);
                _logger.LogInformation("Game ended prematurely");

                // Sleep again to make sure the API and Database has ended the game
                Thread.Sleep(5000);
                _logger.LogInformation("Finished sleeping after signaled Api that the game has ended");
            }
        }
Ejemplo n.º 3
0
        private static double ScoreBundle(GameLayer layer, Bundle bundle)
        {
            double score     = 0;
            var    state     = layer.GetState();
            int    avSpace   = 0;
            int    turnsleft = state.MaxTurns - state.Turn;
            double income    = Helper.GetIncome(layer);

            if (income < 600 && state.Funds < 70000 && turnsleft > 100 && bundle.TotalIncome != 0)
            {
                score += Math.Min(1000 / (0.25 * (bundle.TotalIncome / turnsleft)), 1500);
            }
            foreach (var building in state.ResidenceBuildings)
            {
                avSpace += (layer.GetResidenceBlueprint(building.BuildingName).MaxPop - building.CurrentPop);
            }
            if (bundle.Residence != null)
            {
                score += Math.Min((state.HousingQueue - avSpace) * bundle.Residence.MaxPop * 4, 4000);
            }
            if ((state.Funds - bundle.UpfrontCost < 40000 || income < 400) && bundle.Upgrade != null)
            {
                return(-100);
            }
            score += bundle.PotentialScore;
            return(score);
        }
Ejemplo n.º 4
0
        public static void Main(string[] args)
        {
            Helper.INIT();
            try
            {
                GameLayer.EndGame();
            }
            catch (Exception e) { }
            var gameId = GameLayer.NewGame(Map);

            Console.WriteLine($"Starting game: {GameLayer.GetState().GameId}");
            GameLayer.StartGame(gameId);
            var state = GameLayer.GetState();

            while (GameLayer.GetState().Turn < GameLayer.GetState().MaxTurns)
            {
                //Console.WriteLine(GameLayer.GetState().CurrentTemp);
                NewTakeTurn();
                //take_turn(gameId);
                foreach (var message in GameLayer.GetState().Messages)
                {
                    Console.WriteLine(message);
                }
                foreach (var error in GameLayer.GetState().Errors)
                {
                    Console.WriteLine("Error: " + error);
                }
            }
            Console.WriteLine($"Done with game: {GameLayer.GetState().GameId}");
            Console.WriteLine(GameLayer.GetScore(gameId).FinalScore);
        }
Ejemplo n.º 5
0
        static string RunOneGame(AI_nr2 AI)
        {
            var gameId = GameLayer.NewGame(Map);

            Log.Information($"Starting game: {GameLayer.GetState().GameId}");
            GameLayer.StartGame(gameId);
            AI.ConfigureMap();

            while (GameLayer.GetState().Turn < GameLayer.GetState().MaxTurns)
            {
                AI.Take_turn(gameId);
            }
            string endgame = string.Format("Money: {0} \nPop: {1} \nHappiness: {2} \nCo2: {3} \n\nFinal Pop Score: {4}, \nFinal Happiness Score: {5} \nFinal Co2 Score: {6} \nFinal Score: {7} \nDone with game {8}",
                                           GameLayer.GetState().Funds, GameLayer.GetScore(gameId).FinalPopulation, GameLayer.GetScore(gameId).TotalHappiness, GameLayer.GetScore(gameId).TotalCo2,
                                           GameLayer.GetScore(gameId).FinalPopulation * 15, GameLayer.GetScore(gameId).TotalHappiness / 10, GameLayer.GetScore(gameId).TotalCo2, GameLayer.GetScore(gameId).FinalScore.ToString(),
                                           GameLayer.GetState().GameId);

            return(endgame);
        }
Ejemplo n.º 6
0
        internal static Bundle GetBestBundle(List <Bundle> possibleMoves, GameLayer layer)
        {
            var state = layer.GetState();

            possibleMoves = possibleMoves.Where(t => t.UpfrontCost <= state.Funds).ToList();
            possibleMoves.Sort((a, b) =>
            {
                double sa = ScoreBundle(layer, a);
                double sb = ScoreBundle(layer, b);
                if (sa > sb)
                {
                    return(-1);
                }
                return(1);
            });
            return(possibleMoves.First());
        }
Ejemplo n.º 7
0
        private static void take_turn(string gameId)
        {
            // TODO Implement your artificial intelligence here.
            // TODO Taking one action per turn until the game ends.
            // TODO The following is a short example of how to use the StarterKit
            var x     = 0;
            var y     = 0;
            var state = GameLayer.GetState();

            if (state.ResidenceBuildings.Count < 1)
            {
                for (var i = 0; i < 10; i++)
                {
                    for (var j = 0; j < 10; j++)
                    {
                        if (state.Map[i][j] == 0)
                        {
                            x = i;
                            y = j;
                            break;
                        }
                    }
                }

                GameLayer.StartBuild(new Position(x, y), state.AvailableResidenceBuildings[0].BuildingName, gameId);
            }
            else
            {
                var building = state.ResidenceBuildings[0];
                if (building.BuildProgress < 100)
                {
                    GameLayer.Build(building.Position, gameId);
                }
                else if (!building.Effects.Contains(state.AvailableUpgrades[0].Name))
                {
                    GameLayer.BuyUpgrade(building.Position, state.AvailableUpgrades[0].Name, gameId);
                }
                else if (building.Health < 50)
                {
                    GameLayer.Maintenance(building.Position, gameId);
                }

                else if (building.Temperature < 18)
                {
                    var bluePrint = GameLayer.GetResidenceBlueprint(building.BuildingName);
                    var energy    = bluePrint.BaseEnergyNeed + (building.Temperature - state.CurrentTemp)
                                    * bluePrint.Emissivity / 1 + 0.5 - building.CurrentPop * 0.04;
                    GameLayer.AdjustEnergy(building.Position, energy, gameId);
                }
                else if (building.Temperature > 24)
                {
                    var bluePrint = GameLayer.GetResidenceBlueprint(building.BuildingName);
                    var energy    = bluePrint.BaseEnergyNeed + (building.Temperature - state.CurrentTemp)
                                    * bluePrint.Emissivity / 1 - 0.5 - building.CurrentPop * 0.04;
                    GameLayer.AdjustEnergy(building.Position, energy, gameId);
                }
                else
                {
                    GameLayer.Wait(gameId);
                }

                foreach (var message in GameLayer.GetState().Messages)
                {
                    Console.WriteLine(message);
                }

                foreach (var error in GameLayer.GetState().Errors)
                {
                    Console.WriteLine("Error: " + error);
                }
            }
        }