Ejemplo n.º 1
0
        static void FastRender(TransitionRuleTableTuringMachine <string, char> tM)//, string oldS, char oldT)
        {
            Console.SetCursorPosition(20, 0);
            Console.Write($"{tM.IsHalted}         ");
            Console.SetCursorPosition(20, 1);
            Console.Write($"{tM.InFinalState}         ");
            int halfWidth = Console.BufferWidth / 2;

            Console.SetCursorPosition(halfWidth - (tM.State.Length / 2) - 20, 2);
            Console.WriteLine($"                    {tM.State}                    ");
            Console.WriteLine();
            for (int i = tM.Head - halfWidth; i < tM.Head + halfWidth; i++)
            {
                Console.Write(tM.Tape[i]);
            }
            Console.SetCursorPosition(0, 6);

            /*foreach (var key in tM.Instructions.Keys)
             * {
             *  if(key.state == oldS && key.tape == oldT)
             *  {
             *      Console.ForegroundColor = ConsoleColor.Red;
             *  }
             *  else
             *  {
             *      Console.ForegroundColor = ConsoleColor.White;
             *  }
             *  Console.WriteLine($"({key.state}, {key.tape})\t->\t{tM.Instructions[key].movement}, {tM.Instructions[key].newState}, {tM.Instructions[key].tapeSymbol}");
             * }
             * Console.ForegroundColor = ConsoleColor.White;*/
            if (tM.Instructions.ContainsKey((tM.State, tM.Tape[tM.Head])))
            {
                var v = tM.Instructions[(tM.State, tM.Tape[tM.Head])];
Ejemplo n.º 2
0
        static void RenderTapeFromArbitrary(TransitionRuleTableTuringMachine <string, char> tM, int center)
        {
            int halfWidth = Console.BufferWidth / 2;

            Console.SetCursorPosition(0, 4);
            for (int i = center - halfWidth; i < center + halfWidth; i++)
            {
                Console.Write(tM.Tape[i]);
            }
        }
Ejemplo n.º 3
0
        static bool Run()
        {
            Console.WriteLine("Please select one of the following files by number:");
            string[] files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.txt", SearchOption.AllDirectories);
            for (int i = 0; i < files.Length; i++)
            {
                Console.WriteLine($"{i}:\t{Path.GetFileNameWithoutExtension(files[i])}");
            }
            int j = -1;

            while (j == -1)
            {
                string inp1 = Console.ReadLine();
                if (inp1 == "")
                {
                    return(false);
                }
                if (!int.TryParse(inp1, out j) || j < 0 || j >= files.Length)
                {
                    Console.WriteLine("Please enter a valid index.");
                    j = -1;
                }
            }
            string selectedFile = files[j];

            Console.WriteLine($"{Path.GetFileNameWithoutExtension(files[j])} selected.\nPlease enter tape input, 'C' for until-empty-line, 'F' for from-file, or 'D' for default.");
            string inp = Console.ReadLine();

            if (inp == "D")
            {
                turingMachine = Parse(File.ReadAllText(selectedFile));
            }
            else if (inp == "C")
            {
                string final = "";
                while (inp != "")
                {
                    inp    = Console.ReadLine();
                    final += inp;
                }
                turingMachine = Parse(File.ReadAllText(selectedFile), final.ToCharArray());
            }
            else if (inp == "F")
            {
                turingMachine = Parse(File.ReadAllText(selectedFile), File.ReadAllText(Console.ReadLine()).ToCharArray());
            }
            else
            {
                turingMachine = Parse(File.ReadAllText(selectedFile), inp.ToCharArray());
            }
            RenderSetup();
            FastRender(turingMachine);
            runTimer = new Timer(TimerUpdate);
            (int width, int height) = (Console.BufferWidth, Console.BufferHeight);
            RunMode runningMode   = RunMode.OncePerPress;
            int     tempHead      = 0;
            string  stateToJumpTo = null;
            bool    nextCharClear = false;

            while (true)
            {
                while (!Console.KeyAvailable)
                {
                }
                bool advance = true;
                while (Console.KeyAvailable)
                {
                    if (nextCharClear)
                    {
                        RenderSetup();
                        FastRender(turingMachine);
                        nextCharClear = false;
                    }
                    ConsoleKeyInfo key = Console.ReadKey(true);
                    switch (key.Key)
                    {
                    case ConsoleKey.Enter:
                        advance = false;
                        Console.WriteLine();
                        stateToJumpTo = Console.ReadLine();
                        RenderSetup();
                        FastRender(turingMachine);
                        continue;

                    case ConsoleKey.U:
                        advance = false;
                        Console.Clear();
                        Console.WriteLine(UTMify(turingMachine));
                        nextCharClear = true;
                        continue;

                    case ConsoleKey.T:
                        advance = false;
                        RenderSetup();
                        FastRender(turingMachine);
                        Console.WriteLine();
                        Dictionary <char, int> counts = new Dictionary <char, int>();
                        foreach (char c in ((Tape <char>)turingMachine.Tape).Values.Values)
                        {
                            if (counts.ContainsKey(c))
                            {
                                counts[c]++;
                            }
                            else
                            {
                                counts.Add(c, 1);
                            }
                        }
                        Console.WriteLine();
                        Console.WriteLine("Tape makeup:");
                        foreach (char c in counts.Keys)
                        {
                            Console.WriteLine($"{c}:\t{counts[c]}");
                        }
                        nextCharClear = true;
                        continue;

                    case ConsoleKey.D1:
                    case ConsoleKey.D2:
                    case ConsoleKey.D3:
                    case ConsoleKey.D4:
                        advance     = false;
                        runningMode = (RunMode)(key.Key - ConsoleKey.D1);
                        if (key.Modifiers.HasFlag(ConsoleModifiers.Shift))
                        {
                            period = 400;
                            runTimer.Dispose();
                            runTimer     = new Timer(TimerUpdate, runningMode, 400, 400);
                            timerRunning = true;
                        }
                        else
                        {
                            runTimer.Change(Timeout.Infinite, Timeout.Infinite);
                            timerRunning = false;
                        }
                        break;

                    case ConsoleKey.OemPlus:
                        period = period <= 1 ? 1 : (period / 2);
                        if (timerRunning)
                        {
                            runTimer.Change(period, period);
                        }
                        advance = !timerRunning;
                        break;

                    case ConsoleKey.OemMinus:
                        period *= 2;
                        if (timerRunning)
                        {
                            runTimer.Change(period, period);
                        }
                        advance = !timerRunning;
                        break;

                    case ConsoleKey.LeftArrow:
                        tempHead--;
                        advance = false;
                        RenderTapeFromArbitrary(turingMachine, tempHead);
                        continue;

                    case ConsoleKey.RightArrow:
                        tempHead++;
                        advance = false;
                        RenderTapeFromArbitrary(turingMachine, tempHead);
                        continue;

                    case ConsoleKey.Escape:
                        return(true);

                    default:
                        tempHead = turingMachine.Head;
                        advance  = true;
                        break;
                    }
                }
                if (!advance)
                {
                    continue;
                }
                //Render(turingMachine);
                if ((width, height) != (Console.BufferWidth, Console.BufferHeight))
                {
                    (width, height) = (Console.BufferWidth, Console.BufferHeight);
                    RenderSetup();
                }
                Update(runningMode, stateToJumpTo);
                tempHead = turingMachine.Head;
            }
        }