Ejemplo n.º 1
0
        public SimulationResult Simulation(GameInfosForDebug gameInfosDebug)
        {
            var simResult = new SimulationResult();
            var agent     = new SimulationAgent();

            while (agent.TotalMs < GameInfos.TIMEOUT_FOR_A_TURN_IN_MS && agent.SimRun <= GameInfos.MAX_SIMULATIONS_RUN)
            {
                var t0 = DateTime.UtcNow;

                var infosTurn = new GameTurnInfos(
                    nash: TurnInfos.Nash,
                    humans: TurnInfos.Humans,
                    zombies: TurnInfos.Zombies
                    );

                var tmpResult = SimulateGame(infosTurn, gameInfosDebug);

                if (tmpResult.Points >= simResult.Points)
                {
                    simResult = new SimulationResult(tmpResult);
                }

                var t1 = DateTime.UtcNow;
                agent.TotalMs += Tools.TimeDifferenceInMillisecond(t0, t1);
                agent.SimRun++;
            }

            DebugInfos.WriteDebugMessage(
                functionName: "Simulation end",
                strings: new string[] { $"total sim run {agent.SimRun} in {agent.TotalMs} ms\n" },
                debugLevel: DebugInfos.INFOS
                );

            return(simResult);
        }
Ejemplo n.º 2
0
        private SimulationResult SimulateGame(GameTurnInfos infosTurn, GameInfosForDebug gameInfosDebug)
        {
            var rand           = new Random();
            var sr             = new SimulationResult();
            var simInfos       = new SimulationInfos();
            var moves          = new List <Tuple <int, int> >();
            var gameDebug      = new GameInfosForDebug();
            var turnInfosDebug = new DebugInfosForEachTurn();

            simInfos.SimStartingRandomMovesNum = rand.Next(GameInfos.MAX_SIMULATION_RANDOM_STARTING_MOVES + 1);

            ComputePlayerTarget(infosTurn, simInfos);

            while (!simInfos.SimZombieAllDead && !simInfos.SimFailure && simInfos.SimMovesCount < GameInfos.MAX_MOVES)
            {
                // Simulate a turn of the game.
                simInfos.Moves.Add(Turn(infosTurn, sr, simInfos));

    #if DEBUG_MODE
                turnInfosDebug.Nash = infosTurn.Nash;
                turnInfosDebug.SetHumansOrZombies(infosTurn.Humans);
                turnInfosDebug.SetHumansOrZombies(infosTurn.Zombies);
                turnInfosDebug.Points  = sr.Points;
                turnInfosDebug.Move    = simInfos.Moves.Last();
                turnInfosDebug.NumTurn = simInfos.SimMovesCount;
                gameDebug.DebugInfosForTurn.Add(new DebugInfosForEachTurn(turnInfosDebug));
    #endif

                simInfos.SimMovesCount++;
            }

            if (simInfos.SimZombieAllDead &&
                !simInfos.SimFailure &&
                ((sr.Points + ActualScore) > BestSimulation.SimPoints ||
                 (sr.Points + ActualScore) == BestSimulation.SimPoints && (simInfos.SimMovesCount < (BestSimulation.SimMovesCount - NumTurn))))
            {
                simInfos.SimPoints = sr.Points + ActualScore;
                BestSimulation     = simInfos;
                NewBest            = true;
    #if DEBUG_MODE
                gameInfosDebug.SetDebugInfosForTurn(gameDebug.DebugInfosForTurn);
    #endif
            }

            return(sr);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            string[] inputs;
            var      previousHumansCount  = 0;
            var      previousZombiesCount = 0;
            var      simulation           = new SimulationGame();

            var gameInfosDebug = new GameInfosForDebug();

            // game loop
            while (true)
            {
                inputs = Console.ReadLine().Split(' ');
                var nashPosition = new Tuple <int, int>(int.Parse(inputs[0]), int.Parse(inputs[1]));
                var nash         = new PlayerNash(nashPosition);

                int humanCount = int.Parse(Console.ReadLine());
                var humans     = new List <Human>();
                for (var i = 0; i < humanCount; i++)
                {
                    inputs = Console.ReadLine().Split(' ');
                    var human = new Human(
                        id: int.Parse(inputs[0]),
                        pos: new Tuple <int, int>(int.Parse(inputs[1]), int.Parse(inputs[2]))
                        );
                    humans.Add(human);
                }

                int zombieCount = int.Parse(Console.ReadLine());
                var zombies     = new List <Zombie>();
                for (var i = 0; i < zombieCount; i++)
                {
                    inputs = Console.ReadLine().Split(' ');
                    var zombie = new Zombie(
                        id: int.Parse(inputs[0]),
                        pos: new Tuple <int, int>(int.Parse(inputs[1]), int.Parse(inputs[2])),
                        nextPos: new Tuple <int, int>(int.Parse(inputs[3]), int.Parse(inputs[4]))
                        );
                    zombies.Add(zombie);
                }

                if (zombieCount < previousZombiesCount)
                {
                    int zombiesDead = previousZombiesCount - zombieCount;
                    int humanPoints = 10 * previousHumansCount * previousHumansCount;

                    for (var i = 0; i < zombiesDead; i++)
                    {
                        var tmpPoints = humanPoints;

                        if (zombiesDead > 1)
                        {
                            tmpPoints *= Tools.Fibonacci(i + 1);
                        }
                        simulation.ActualScore += tmpPoints;
                    }
                }

                var turnInfos = new GameTurnInfos(nash, humans, zombies);

                // For each turn, we want to simulate the maximum of game played to find the best next move possible.
                simulation.InitTurnInfos(turnInfos);
                var simResult = simulation.Simulation(gameInfosDebug);

                if (simulation.NewBest)
                {
                    simulation.NumTurn = 0;
                    simulation.NewBest = false;
                }

    #if DEBUG_MODE
                DebugInfos.WriteSimulTurnInfos(gameInfosDebug.DebugInfosForTurn[simulation.NumTurn], DebugInfos.DEBUG);
    #endif

                Tools.PrintMove(simulation.BestSimulation.Moves[simulation.NumTurn]);

                previousHumansCount  = humanCount;
                previousZombiesCount = zombieCount;

                simulation.NumTurn++;
            }
        }