I am trying to code the basic functions of a Turing machine. So far, the program takes user input and stores it into a List as shown below:
public String cmdLoop()
{
Scanner getReq = new Scanner( System.in );
for( ; ; )
{
say( "current read/write head position: " + currHeadPos );
say( "" );
SecondMenu();
say( "Type commands here:");
ArrayList<String>inputs = new ArrayList<String>();
String userInput = getReq.nextLine();
do{
userInput = getReq.nextLine();
inputs.add(userInput);
RunCmd(userInput);
}
while(!userInput.equals("Done"));
}
}
SecondMenu is as below
public void SecondMenu()
{
say( "\t?\tprint current cell" );
say( "\t=\tassign new symbol to current cell" );
say( "\tE\terase current cell" );
say( "\tL\tmove head to left" );
say( "\tR\tmove head to right" );
say( "\tB\trewind to beginning of tape" );
say( "\tD\tdump contents of tape" );
say( "\tT\tset Transition commands" );
}
public void say( String s )
{
System.out.println( s );
}
Each input is a command. In other words, how the program will run is based on what the user types in, and the method that runs these commands is called RunCmd. Some examples of the commands are below.
void RunCmd(String userInput)
{ char command = userInput.charAt(0);
say("");
if(command == '?')
{
dumpTape(currHeadPos, currHeadPos + 1);
}
else
if(command == '=')
{
tapeContents[currHeadPos] = userInput.charAt(1);
if( endOfTape == 0 )
{
endOfTape++;
}
}
else
if( command == 'E' )
{
//use a space to signal 'empty' so that cells will print out ok
tapeContents[ currHeadPos ] = ' ';
}
else
if( command == 'L' )
{
if( currHeadPos == 0 )
{
say( "" );
say( "Tape rewound, unable to move LEFT" );
}
else
{
currHeadPos--;
}
}
else
if( command == 'R' )
{
currHeadPos++;
endOfTape++;
}
How can I get the program to iterate through the loop and run all the input commands at once? For example:
User inputs =1, R, =0, R
The program would make the cell under the header = 1, move Right, = 0, move Right again, all in one run.
*I don't want the program to ask for a command after each input, and typing 'Done' will end inputs. I don't want the program to display SecondMenu anytime other than when the program starts.
*I want to be able to input like 100 inputs once the program runs, store them in a List, and have the program iterate through the array and run all 100 commands (based on user input) in one run. I've tried using for loops, while loops, and iterator (though I have no idea how to use this and may have done it wrong)
Edited for clarification.