Ejemplo n.º 1
0
 public override Direction Think(GameState gs)
 {
     return(UseSmart ? SmartAgent.Think(gs) : DumbAgent.Think(gs));
 }
        static void Main(string[] args)
        {
            Process _process       = Process.GetCurrentProcess();
            bool    _takearguments = false;

            // How many arguments have been stored in the game.
            if (args.Length > 0)
            {
                Console.WriteLine("Arguments found.");
                _takearguments = true;
                HandleArguments(args);
            }

            // Required for storing the name of the agent.
            string _agentName = "";

            if (!_takearguments)
            {
                Console.WriteLine("Name of controller: ");
                _agentName = Console.ReadLine();
                Console.Clear();

                // Determine which ghosts are going to be added to the gameplay.
                while (Ghosts.Count < 4)
                {
                    Console.WriteLine(string.Format("({0} Ghosts) - Which ghosts (r) (bl) (br) (p) or (n) / (a)?", Ghosts.Count.ToString()));
                    string _ghostname = Console.ReadLine();

                    // Determine that the ghost name exists first
                    if (GHOST_CODES.Contains(_ghostname))
                    {
                        Ghosts.Add(_ghostname);
                        Console.Clear();
                    }
                    else if (_ghostname == "n") // Cancel out of it
                    {
                        break;
                    }
                    else if (_ghostname == "a")
                    {
                        // Clear out the list of ghosts that are entered already
                        // Do something else with them
                        Ghosts.Clear();
                        Ghosts.Add("bl");
                        Ghosts.Add("r");
                        Ghosts.Add("p");
                        Ghosts.Add("br");
                    }
                    else
                    {
                        Console.Clear();
                        Console.WriteLine("Ghost name is not valid");
                    }
                }

                Console.Clear();
                Console.WriteLine("How many games do you wish to simulate?");
                string _count  = Console.ReadLine();
                int    _result = 0;

                // Determine that the value that has been inputted is
                // in fact valid.
                while (!int.TryParse(_count, out _result))
                {
                    Console.Clear();
                    Console.WriteLine("Please try again: ");
                    _count = Console.ReadLine();
                }

                // Set the new count of games that we want to simulate.
                gamesToPlay = _result;

                Console.Clear();
                string _consoleoutput = "";

                while (_consoleoutput != "n" && _consoleoutput != "y")
                {
                    // Determine if we want to log output to be silence while we do this
                    Console.WriteLine("Silence output?");
                    _consoleoutput = Console.ReadLine();
                }

                if (_consoleoutput == "n")
                {
                    m_RemainQuiet = false;
                }
                else if (_consoleoutput == "y")
                {
                    m_RemainQuiet = true;
                }
            }

            // Get some strange invocation error here.
            // tryLoadController(_agentName);

            int cores        = System.Environment.ProcessorCount;
            int gamesForEach = gamesToPlay / cores;

            for (int i = 0; i < cores; i++)
            {
                // add multicore support
            }

            // Output the available cores.
            Console.WriteLine(string.Format("Cores Available: {0}", System.Environment.ProcessorCount));


            gs           = new GameState();
            gs.GameOver += new EventHandler(GameOverHandler);
            gs.StartPlay();

            // DEFINE CONTROLLER //
            //BasePacman controller = new TestPac();
            BasePacman controller = new LucPacScripted();

            // Turn off the logging
            if (controller.GetType() == typeof(LucPac) && m_RemainQuiet)
            {
                LucPac.RemainQuiet = true;
            }

            if (controller.GetType() == typeof(LucPacScripted) && m_RemainQuiet)
            {
                LucPacScripted.RemainQuiet = true;
            }

            //BasePacman controller = new SmartDijkstraPac();
            gs.Controller = controller;

            Stopwatch watch      = new Stopwatch();
            int       percentage = -1;
            int       lastUpdate = 0;

            watch.Start();
            while (gamesPlayed < gamesToPlay)
            {
                int newPercentage = (int)Math.Floor(((float)gamesPlayed / gamesToPlay) * 100);
                if (newPercentage != percentage || gamesPlayed - lastUpdate >= 100)
                {
                    lastUpdate = gamesPlayed;
                    percentage = newPercentage;
                    Console.Clear();
                    Console.WriteLine("Simulating ... " + percentage + "% (" + gamesPlayed + " : " + gamesToPlay + ")");
                    Console.WriteLine(" - Elapsed: " + formatSeconds((watch.ElapsedMilliseconds / 1000.0) + "") + "s, Estimated total: " + formatSeconds(((watch.ElapsedMilliseconds / 1000.0) / percentage * 100) + "") + "s");
                    Console.WriteLine(" - Current best: " + highestScore);
                    Console.WriteLine(" - Current worst: " + lowestScore);
                    if (gamesPlayed > 0)
                    {
                        Console.WriteLine(" - Current avg.: " + (totalScore / gamesPlayed));
                    }
                    for (int i = scores.Count - 1; i >= 0 && i > scores.Count - 100; i--)
                    {
                        Console.Write(scores[i] + ",");
                    }
                }
                // update gamestate
                Direction direction = controller.Think(gs);
                gs.Pacman.SetDirection(direction);
                // update stream
                currentGame.WriteByte((byte)Math.Floor(gs.Pacman.Xf));
                currentGame.WriteByte((byte)Math.Floor(gs.Pacman.Yf));
                currentGame.WriteByte((byte)gs.Pacman.Direction);
                currentGame.WriteByte((byte)gs.Pacman.Lives);
                currentGame.WriteByte((byte)(gs.Pacman.Score / 255));
                currentGame.WriteByte((byte)(gs.Pacman.Score % 255));

                foreach (Pacman.Simulator.Ghosts.Ghost g in gs.Ghosts)
                {
                    currentGame.WriteByte((byte)g.X);
                    currentGame.WriteByte((byte)g.Y);
                    currentGame.WriteByte((byte)((g.Chasing == true) ? 1 : 0));
                    currentGame.WriteByte((byte)((g.Entered == true) ? 1 : 0));
                    currentGame.WriteByte((byte)g.Direction);
                    currentGame.WriteByte((byte)((g.IsEaten == true) ? 1 : 0));
                }
                // update game
                gs.Update();
                ms += GameState.MSPF;
            }
            watch.Stop();

            // shut down controller
            controller.SimulationFinished();

            // write best/worst to disk
            using (BinaryWriter bw = new BinaryWriter(new FileStream(System.Environment.CurrentDirectory + "/best" + highestScore + ".dat", FileMode.Create))) {
                bestGame.WriteTo(bw.BaseStream);
            }
            using (BinaryWriter bw = new BinaryWriter(new FileStream(System.Environment.CurrentDirectory + "/worst" + lowestScore + ".dat", FileMode.Create))) {
                worstGame.WriteTo(bw.BaseStream);
            }

            // write results
            using (StreamWriter sw = new StreamWriter(File.Open("scores.txt", FileMode.Create))) {
                foreach (int s in scores)
                {
                    sw.Write(s + "\n");
                }
            }

            // output results
            Console.Clear();
            long seconds = ms / 1000;

            Console.WriteLine("Games played: " + gamesPlayed);
            Console.WriteLine("Avg. score: " + (totalScore / gamesPlayed));
            Console.WriteLine("Highest score: " + highestScore + " points");
            Console.WriteLine("Lowest score: " + lowestScore + " points");
            Console.WriteLine("Max Pills Eaten: " + maxPillsEaten);
            Console.WriteLine("Min Pills Eaten: " + minPillsEaten);
            Console.WriteLine("Average Pills Eaten: " + pillsEatenTotal / gamesPlayed);
            Console.WriteLine("Max Ghosts Eaten: " + maxGhostsEaten);
            Console.WriteLine("Min Ghosts Eaten: " + minGhostsEaten);
            Console.WriteLine("Average Ghosts Eaten: " + totalGhostsEaten / gamesPlayed);
            Console.WriteLine("Longest game: " + ((float)longestGame / 1000.0f) + " seconds");
            Console.WriteLine("Total simulated time: " + (seconds / 60 / 60 / 24) + "d " + ((seconds / 60 / 60) % 24) + "h " + ((seconds / 60) % 60) + "m " + (seconds % 60) + "s");
            Console.WriteLine("Avg. simulated time pr. game: " + ((float)ms / 1000.0f / gamesPlayed) + " seconds");
            Console.WriteLine("Simulation took: " + (watch.ElapsedMilliseconds / 1000.0f) + " seconds");
            Console.WriteLine("Speed: " + (ms / watch.ElapsedMilliseconds) + " (" + ((ms / watch.ElapsedMilliseconds) / 60) + "m " + ((ms / watch.ElapsedMilliseconds) % 60) + " s) simulated seconds pr. second");
            Console.WriteLine("For a total of: " + gamesPlayed / (watch.ElapsedMilliseconds / 1000.0f) + " games pr. second");
            Console.ReadLine();
        }