void SelectMachine(int machineIndex)
 {
     Console.Clear();
     Console.SetCursorPosition(0, 0);
     selectedMachine = machines[machineIndex - 1];
     Console.Clear();
     Console.SetCursorPosition(0, 0);
 }
Esempio n. 2
0
        private void runBtn_Click(object sender, EventArgs e)
        {
            if (_running)
            {
                // stop the thread
                if (_paused)
                {
                    _manualEvent.Set();
                }
                _turingRunnerThread.Abort();
            }
            else
            {
                // get the machine thread running
                if (_utmFile.Length == 0 || _inputFile.Length == 0)
                {
                    MessageBox.Show("Please specify a turing machine spec file and an input file.");
                    return;
                }

                try
                {
                    var machine = new TuringMachine();
                    _manualEvent = new ManualResetEvent(false);

                    machine.LoadSpec(new StreamReader(_utmFile).ReadToEnd());

                    var runner = new TuringRunner(machine, new StreamReader(_inputFile).ReadToEnd(), _manualEvent);
                    runner.RunEvent    += (snder, args) => BeginInvoke(new EventHandler <TuringRunEventArgs>(OnRunEvent), new[] { snder, args });
                    _turingRunnerThread = new Thread(runner.Run)
                    {
                        Priority = ThreadPriority.BelowNormal,
                        Name     = "Turing Machine Runner"
                    };
                    _turingRunnerThread.Start();             // starts in waiting mode

                    _manualEvent.Set();                      // release the thread
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Esempio n. 3
0
        private void showMachineBtn_Click(object sender, EventArgs e)
        {
            if (_utmFile.Length == 0)
            {
                MessageBox.Show("Please specify a turing machine spec file.");
                return;
            }

            try
            {
                var machine = new TuringMachine();
                machine.LoadSpec(new StreamReader(_utmFile).ReadToEnd());
                ClearOutput();
                WriteOutput(machine.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        void RunProgram()
        {
            Reset();
            Console.Clear();
            Console.WriteLine();
            Console.WriteLine("Files selected: " + fileNames.Count);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Enter the command \"RUN\" to begin the simulations.");
            Console.WriteLine("Enter the command \"BACK\" to return to the main menu.");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine();

            Console.WriteLine("Enter file name of code and press enter:");
            while (!simulationsRunning)
            {
                string name = Console.ReadLine();
                if (name.EndsWith(".txt"))
                {
                    if (File.Exists(name))
                    {
                        fileNames.Add(Directory.GetCurrentDirectory() + "\\" + name);
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine(name + " added.");
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine(name + " does not exist.");
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                }
                else if (name.ToLower() == "run")
                {
                    if (fileNames.Count > 0)
                    {
                        FileLoader loader = new FileLoader();
                        for (int i = 0; i < fileNames.Count; i++)
                        {
                            TuringMachine m = new TuringMachine(loader.LoadTuringInstruction(fileNames[i]), fileNames[i].Substring(fileNames[i].LastIndexOf('\\') + 1));
                            machines.Add(m);
                        }
                        var t = new Thread(StartSimulations);
                        t.Start();
                        //ThreadPool.QueueUserWorkItem((obj) => StartSimulations());
                        //StartSimulations();
                        simulationsRunning = true;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("No files have been added.");
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                }
                else if (name.ToLower() == "back")
                {
                    Console.Clear();
                    Reset();
                    Program.DoWelcomeScreen();
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Entered text should end with .txt to indicate a file, or RUN to indicate the start of the simulations.");
                    Console.ForegroundColor = ConsoleColor.White;
                }
            }
        }
 void StartThread(TuringMachine machine)
 {
     ThreadPool.QueueUserWorkItem((obj) => machine.StartSimulation());
 }