Example #1
0
        static void Main(string[] args)
        {
            Console.Write("Please enter a path to the source file: ");
            string userInput = Console.ReadLine();

            if (userInput.Length < 7)
            {
                Console.WriteLine("File name isnt big enough to possibly be a .TM file made from a .txt file");
                Console.WriteLine("Press any key to exit program and try again.");
                Console.ReadKey();
                Environment.Exit(0);
            }
            if (userInput[userInput.Length - 7] != '.' && userInput[userInput.Length - 6] != 'T' && userInput[userInput.Length - 5] != 'M')
            {
                Console.WriteLine("File was not a .TM file");
                Console.WriteLine("Press any key to exit program and try again.");
                Console.ReadKey();
                Environment.Exit(0);
            }

            TuringMachine testMachine;

            try { testMachine = new TuringMachine(userInput); }
            catch (FormatException x)
            {
                Console.WriteLine(x.Message);
                Console.WriteLine("Press any key to exit program and try again.");
                Console.ReadKey();
                Environment.Exit(0);
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine("File could not be accessed due to permission level.");
                Console.WriteLine("Press any key to exit program and try again.");
                Console.ReadKey();
                Environment.Exit(0);
            }
            catch (System.IO.FileNotFoundException)
            {
                Console.WriteLine("File does not exist");
                Console.WriteLine("Press any key to exit program and try again.");
                Console.ReadKey();
                Environment.Exit(0);
            }

            testMachine = new TuringMachine(userInput);
            try { testMachine.run(testMachine); }
            catch (FormatException x)
            {
                Console.WriteLine(x.Message);
                Console.WriteLine("Press any key to exit program and try again.");
                Console.ReadKey();
                Environment.Exit(0);
            }



            Console.ReadKey();
        }
        public void run(TuringMachine machine)
        {
            currentState = startState;
            header       = 1;

            tape = setTape();
            showTape();

            bool foundEdge;

            while (currentState != acceptState || currentState != rejectState)
            {
                foundEdge = false;
                List <Edge> potentialEdges = new List <Edge>();
                try { potentialEdges = transitionFunction[currentState]; }
                catch
                {
                    currentState = rejectState;
                    continue;
                }

                foreach (Edge x in potentialEdges)
                {
                    if (potentialEdges.Count == 0)
                    {
                        currentState = rejectState;
                    }

                    if (x.read.ToString().Equals(tape[header]))
                    {
                        tape[header] = x.write.ToString();
                        if (x.moveHeader == 'R')
                        {
                            if (header < tape.Count() - 2)
                            {
                                header++;
                                tape[header - 2] = tape[header - 1];
                                tape[header - 1] = "[" + x.nextState.name + "]";
                            }
                            else
                            {
                                resizeArray(tape);
                                header++;
                                tape[header - 2] = tape[header - 1];
                                tape[header - 1] = "[" + x.nextState.name + "]";
                            }
                        }
                        else
                        {
                            if (header > 1)
                            {
                                header--;
                                tape[header]     = tape[header - 1];
                                tape[header - 1] = "[" + x.nextState.name + "]";
                                //header--;
                            }
                            else
                            {
                                throw new FormatException("Header fell off the front of the tape. Only infinite in the other direction.");
                            }
                        }
                        currentState = x.nextState;
                        foundEdge    = true;
                        break;
                    }
                }
                if (!foundEdge)
                {
                    currentState = rejectState;
                }
                Console.Write("\n");
                if (currentState == acceptState)
                {
                    Console.Write("Accept: ");
                }

                else if (currentState == rejectState)
                {
                    Console.Write("Reject: ");
                }

                showTape();
            }
        }