Exemplo n.º 1
0
        public Machine Step()
        {
            if (State < 0)
            {
                return(this);
            }

            return
                (TransitionTable
                 .Where(t => t.InitialState == State && t.Read == Head.Read())
                 .Select(t => new Machine(t.NextState, Head.Write(t.Write).Move(t.HeadDirection), TransitionTable))
                 .First());
        }
Exemplo n.º 2
0
        public Machine Step()
        {
            if (State < 0)
            {
                return(this);
            }
            var machine = TransitionTable
                          .Where(t => t.InitialState == State && t.Read == Head.Read())
                          .DefaultIfEmpty(new Transition(0, Head.Blank, Head.Read(), HeadDirection.NoMove,
                                                         TuringMachine.State.Error))
                          .Select(
                t => new Machine(t.NextState, Head.Write(t.Write).Move(t.HeadDirection), TransitionTable, Alphabet))
                          .First();

            return(machine);
        }
        //Compute the Strip based on States Table.
        private void TuringMachine()
        {
            int iterations = 0;

            //Initialize States, Head, CurrentState.
            List <State> StateList = InitializeStates();

            //VALIDATION 0: Check if there are parameters to execute in state 0.
            if (StateList[0].Parameters == null)
            {
                Interaction.MsgBox("There are no instructions to start computation.");
                return;
            }


            Head Head = new Head(txbStrip.Text);

            int CurrentState = 0;

            //Run machine until state is HALT (-1).
            while (CurrentState != -1)
            {
                //Update cycle counter.
                updateIterations(iterations += 1);

                try
                {
                    //VALIDATION 1: Check if state has response for the current position.
                    if (StateList[CurrentState].Parameters.ContainsKey(Head.Read()))
                    {
                        //STEP 1: READ AND SAVE THE CURRENT SYMBOL.
                        char readSymbol = Head.Read();

                        //STEP 2: WRITE NEW SYMBOL IN HEAD.STRIP[POSITION].
                        Head.Write(StateList[CurrentState].Parameters[readSymbol].newSymbol);

                        //STEP 3: MOVE HEAD.POSITION.
                        Head.Move(StateList[CurrentState].Parameters[readSymbol].Direction);

                        //VISUAL: Update Visuals
                        updateVisuals(CurrentState, Head);
                        this.Update();
                        Thread.Sleep(Properties.Settings.Default.IterationDelay);

                        //VALIDATION 2: Check if HEAD has gone off right end of tape.
                        if (Head.Position >= Head.Strip.Length)
                        {
                            Interaction.MsgBox("Halt: Head has gone out of the right end of tape. Halting Problem?");
                            CurrentState = -1;
                        }
                        //VALIDATION 3: Check if HEAD has gone off left end of tape.
                        else if (Head.Position < 0)
                        {
                            Interaction.MsgBox("Halt: Head has gone out of the left end of tape. Halting Problem?");
                            CurrentState = -1;
                        }
                        //STEP 4: CHANGE STATE, UPDATE STRIP.
                        else
                        {
                            CurrentState = StateList[CurrentState].Parameters[readSymbol].newState;
                        }
                    }
                    else
                    {
                        Interaction.MsgBox("Halt: State " + CurrentState + " does not have rule for symbol " + Head.Read());
                        return;
                    }
                }
                catch (ArgumentOutOfRangeException e)
                {
                    Interaction.MsgBox("Halt: State '" + CurrentState + "' does not have rule for symbol '" + Head.Read() + "'");
                    return;
                }
            }

            //FINAL STEP: MACHINE HAS REACHED HALT STATE, PRINT STRIP AND SUCESS MESSAGE.
            if (CurrentState == -1 && Head.Position >= 0)
            {
                Interaction.MsgBox("Turing Machine: Computation complete.");
            }
        }