Example #1
0
        public static void Main()
        {
            PlanetarySystem planets = new PlanetarySystem(0);
            SimulationTimer timer   = new SimulationTimer();
            bool            running = false; //set to true when a simulation is running
            bool            paused  = false; //set to true when a simulation is paused

            Console.WriteLine("Welcome to the solar planet simulator! Select from the menu.");
            while (true)
            {
                Console.WriteLine();
                Console.Write("(s)tart, (p)ause, (r)esume, (g)et status, (q)uit? ");

                string choice;
                try
                {
                    choice = Console.ReadLine().Trim().ToLower();
                }
                catch (System.IO.IOException e)
                {
                    Console.WriteLine($"IO Exception: {e.Message}");
                    continue;
                }

                string result = "Please choose from the menu";

                if (choice.StartsWith("s"))
                {
                    running = true;

                    Console.Write("How many planets (1 to 9) [In addition to the Sun]? ");
                    string numPlanet = "";
                    if (GetInt(out int num1, 1, 9, ref numPlanet))
                    {
                        planets = new PlanetarySystem(num1);

                        Console.Write("Simulation dt (ms) [1 to 1000]? (1 simulation dt (ms) = 1 day of actual time) ");
                        string time = "";

                        if (GetInt(out int interval, 1, 1000, ref time))
                        {
                            Console.Write("Simulation duration (how many dt's) [1 to 1000]? ");
                            string numDt = "";

                            if (GetInt(out int duration, 1, 1000, ref numDt))
                            {
                                timer = new SimulationTimer();
                                timer.SetTimer(planets, duration, interval);
                                Console.WriteLine("A new simulation of {0} planets initiated.", num1);
                            }
                            else
                            {
                                Console.WriteLine(numDt);
                            }
                        }
        int duration = 0;           //The simulation duration

        /// <summary>
        /// creates and starts a timer. The timer ticks once every simulationTimeInterval to update simulation
        /// </summary>
        /// <param name="nPlanetsAndSun">The current planetary system to simulate.</param>
        /// <param name="duration">The simulation duration in ms.</param>
        /// <param name="simulationTimeInterval">The simulation time interval (dt).</param>
        public void SetTimer(PlanetarySystem nPlanetsAndSun, int duration, int simulationTimeInterval)
        {
            this.simulationTimeInterval = simulationTimeInterval;             //milliseconds (dt provided by the user)

            timer          = new System.Timers.Timer(simulationTimeInterval); //Create a timer
            timer.Elapsed += UpdatePlanetsAndSimulationTime;                  //Sets which method to call when timer elapses
            timer.Enabled  = true;
            timer.Start();

            this.nPlanetsAndSun = nPlanetsAndSun;
            this.duration       = duration; //milliseconds
        }
Example #3
0
        public static void Main()
        {
            PlanetarySystem planets = new PlanetarySystem(0);
            SimulationTimer timer   = new SimulationTimer();
            bool            running = false;               //set to true when a simulation is running
            bool            paused  = false;               //set to true when a simulation is paused
            bool            planetarySystemExists = false; //set to true when PlantarySystem is created

            Console.WriteLine("Welcome to the solar planet simulator! Select from the menu.");

            while (true)
            {
                Console.WriteLine();
                Console.Write("(s)tart, (p)ause, (r)esume, (g)et status, (q)uit? ");

                string choice;

                try
                {
                    choice = Console.ReadLine().Trim().ToLower();
                }
                catch (System.IO.IOException e)
                {
                    Console.WriteLine($"IO Exception: {e.Message}");
                    continue;
                }

                // time variables for calculating duraiton and verifying is running
                int dtInt       = 0;
                int durationInt = 0;

                string result = "Please choose from the menu";

                if (choice.StartsWith("s"))
                {
                    // Reset flags (in case somebody wants to create another simulation
                    running = false;
                    paused  = false;

                    int numberInt;

                    // If no simulation is currently running, ask for number of planets
                    if (!running)
                    {
                        Console.WriteLine();
                        Console.Write("How many planets (1 to 9) [In addition to the Sun]? ");

                        // If entered number of planets is valid, ask for simulation dt
                        if (GetInt(out numberInt, 1, 9, ref result))
                        {
                            Console.Write("Simulation dt (ms) [1 to 1000]? (1 simulation dt (ms) = 1 day of actual time) ");

                            // If simulation dt time is valid, ask for the simulation duration
                            if (GetInt(out dtInt, 1, 1000, ref result))
                            {
                                Console.Write("Simulation duration (how many dt's) [1 to 1000] ");

                                // If duration time is valid, initiate simulation
                                if (GetInt(out durationInt, 1, 1000, ref result))
                                {
                                    // Initialize the planetary system with 3 planets
                                    planets = new PlanetarySystem(numberInt);

                                    // Create new timer (if 2 sims in a row) and set the timer to the entered duration and dt
                                    timer = new SimulationTimer();
                                    timer.SetTimer(planets, durationInt, dtInt);

                                    // Set the running flag to true
                                    running = true;
                                    planetarySystemExists = true;

                                    // Print confirmation message to user
                                    result = $"A simulation of {numberInt} planets initiated.";
                                }
                            }
                        }
                    }
                }
                else if (choice.StartsWith("p"))
                {
                    // If no simulation is running, print message
                    if (!running)
                    {
                        result = "No simulation is running to be paused.";
                    }
                    // Else if the simulation is already paused, print message to user
                    else if (paused)
                    {
                        result = "Already paused.";
                    }
                    else
                    {
                        // Pause the timer and set paused flag to true
                        timer.Pause();
                        paused = true;

                        // Print confirmation message to user
                        result = "Simulation paused.";
                    }
                }
                else if (choice.StartsWith("r"))
                {
                    // If no simulation is running, print message
                    if (!running)
                    {
                        result = "No simulation is running to be resumed.";
                    }
                    // Else if the simulation is not paused, print message to user
                    // Else if the simulation is not paused, print message to user
                    else if (!paused)
                    {
                        result = "No simulation is paused to be resumed.";
                    }
                    // Else, resume the simulation and set paused flag to false
                    else
                    {
                        // Pause the timer and set paused flag to true
                        timer.Resume();
                        paused = false;

                        // Print confirmation message to user
                        result = "Simulation resumed.";
                    }
                }
                else if (choice.StartsWith("g"))
                {
                    // If no simulation is running, print message
                    if (!planetarySystemExists)
                    {
                        result = "No simulation is running.";
                    }
                    else
                    {
                        // Print out the current simulation time and current state of the planets
                        string time = $"At time: {timer.GetSimulationTime()}\n";
                        planets.GetCurrentState(out result);

                        result = time + result;
                    }
                }
                else if (choice.StartsWith("q"))
                {
                    // Exit superloop
                    break;
                }

                // Assign running is true if it is larger than the duration and planetarysystemexists
                running = (dtInt * durationInt >= timer.GetDuration() && planetarySystemExists);

                Console.WriteLine();
                // Result string to print is updated in one of the conditional statements
                Console.WriteLine(result);
            }
        }
Example #4
0
        public static void Main()
        {
            PlanetarySystem planets = new PlanetarySystem(0);
            SimulationTimer timer   = new SimulationTimer();
            bool            running = false; //set to true when a simulation is running
            bool            paused  = false; //set to true when a simulation is paused

            Console.WriteLine("Welcome to the solar planet simulator! Select from the menu.");
            while (true)
            {
                Console.WriteLine();
                Console.Write("(s)tart, (p)ause, (r)esume, (g)et status, (q)uit? ");

                string choice;
                try
                {
                    choice = Console.ReadLine().Trim().ToLower();
                }
                catch (System.IO.IOException e)
                {
                    Console.WriteLine($"IO Exception: {e.Message}");
                    continue;
                }

                string result = "Please choose from the menu";

                if (choice.StartsWith("s"))
                {
                    /*
                     * Ask for:
                     * 1. n: number of planets
                     * 2. dt: simulation timer interval
                     * 3. duration: duration of simulation (in terms of how many intervals)
                     */
                    bool   check;
                    string error;
                    int    n, dt, duration, iterations;

                    do
                    {
                        error = "";
                        Console.Write("How many planets (In addition to the sun) [1 - 9]:  ");
                        check = GetInt(out n, 1, 9, ref error);
                        if (!error.Equals(""))
                        {
                            Console.WriteLine(error);
                        }
                    } while (!check);

                    do
                    {
                        error = "";
                        Console.Write("Simulation dt (ms) [1 - 1000]:  ");
                        check = GetInt(out dt, 1, 1000, ref error);
                        if (!error.Equals(""))
                        {
                            Console.WriteLine(error);
                        }
                    } while (!check);

                    do
                    {
                        error = "";
                        Console.Write("Simulation duration (how many dt's) [1 - 1000]  ");
                        check = GetInt(out iterations, 1, 1000, ref error);
                        if (!error.Equals(""))
                        {
                            Console.WriteLine(error);
                        }
                    } while (!check);

                    running  = true;
                    duration = dt * iterations;
                    timer    = new SimulationTimer();
                    planets  = new PlanetarySystem(n);

                    timer.SetTimer(planets, duration, dt);
                    Console.WriteLine("A new simulation of " + n + " planets is initiated");
                }
                else if (choice.StartsWith("p"))
                {
                    if (running)
                    {
                        timer.Pause();
                        paused  = true;
                        running = false;
                        Console.WriteLine("Simulation has pasued.");
                    }
                    else
                    {
                        Console.WriteLine("No simulation is running to be paused.");
                    }
                }
                else if (choice.StartsWith("r"))
                {
                    if (paused)
                    {
                        timer.Resume();
                        paused  = false;
                        running = true;
                        Console.WriteLine("Simulation has resumed.");
                    }
                    else
                    {
                        Console.WriteLine("No simulation is paused to be resumed.");
                    }
                }
                else if (choice.StartsWith("g"))
                {
                    if (running)
                    {
                        string time = "Current Time: " + timer.GetSimulationTime() + "\n";
                        Console.WriteLine(time);

                        string status;
                        planets.GetCurrentState(out status);
                        Console.Write(status);
                    }
                    else
                    {
                        Console.WriteLine("No simulation is running.");
                    }
                }
                else if (choice.StartsWith("q"))
                {
                    break;
                }
                Console.WriteLine();
                Console.WriteLine(result);
            }
        }
Example #5
0
        public static void Main()
        {
            PlanetarySystem planets = new PlanetarySystem(0);
            SimulationTimer timer   = new SimulationTimer();
            bool            running = false; //set to true when a simulation is running
            bool            paused  = false; //set to true when a simulation is paused

            Console.WriteLine("Welcome to the solar planet simulator! Select from the menu.");
            while (true)
            {
                Console.WriteLine();
                Console.Write("(s)tart, (p)ause, (r)esume, (g)et status, (q)uit? ");

                string choice;
                try
                {
                    choice = Console.ReadLine().Trim().ToLower();
                }
                catch (System.IO.IOException e)
                {
                    Console.WriteLine($"IO Exception: {e.Message}");
                    continue;
                }

                string result = "Please choose from the menu";

                if (choice.StartsWith("s"))
                {
                    Console.WriteLine();
                    Console.Write("How many planets (1 to 9) [In addition to the Sun]? ");

                    if (GetInt(out int numPlanets, 1, 9, ref result))
                    {
                        Console.Write("Simulation dt (ms) [1 to 1000]? (1 simulation dt (ms) = 1 day of actual time) ");
                        if (GetInt(out int dt, 1, 1000, ref result))
                        {
                            Console.Write("Simulation duration (how many dt's) [1 to 1000]? ");
                            if (GetInt(out int duration, 1, 1000, ref result))
                            {
                                result  = $"A new simulation of {numPlanets} planets initiated.";
                                planets = new PlanetarySystem(numPlanets);
                                timer.SetTimer(planets, duration, dt);
                                running = true;
                            }
                        }
                    }
                }
                else if (choice.StartsWith("p"))
                {
                }
                else if (choice.StartsWith("r"))
                {
                    if (running == true)
                    {
                        result = "No Simulation is paused to be resumed";
                    }
                    else
                    {
                        timer.Resume();
                        result  = "Simulation resumed.";
                        running = true;
                    }
                }
                else if (choice.StartsWith("g"))
                {
                    Console.WriteLine();
                    Console.Write($"At time: {timer.GetSimulationTime()}");
                    planets.GetCurrentState(out result);
                }
                else if (choice.StartsWith("q"))
                {
                    break;
                }
                Console.WriteLine();
                Console.WriteLine(result);
            }
        }