Turing Machine
Page 1 of 1
Turing Machine
I just tried to write Java Code for Turing machine. See if anyone interested
TuringMachine.java
State.java
TuringMachine.java
- Code:
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author ThienDinh
*/
public class TuringMachine {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
Scanner in = new Scanner(System.in);
ArrayList<State> states = new ArrayList<>();
ArrayList<String> inputs = new ArrayList<>();
boolean done = false;
System.out.println("Give the determining input: ");
do
{
String inpu = in.next();
if (inpu.equals("end"))
{
break;
} else
{
inputs.add(inpu);
}
} while (true);
System.out.print("How many possible state: ");
int totalState = in.nextInt();
for (int j = 0; j < totalState; j++)
{
states.add(new State(inputs));
System.out.println("State s" + j + ":");
for (int i = 0; i < inputs.size(); i++)
{
System.out.print("Input next state: ");
states.get(j).nextStates.add(in.nextInt());
}
for (int i = 0; i < inputs.size(); i++)
{
System.out.print("Input output: ");
states.get(j).outPuts.add(in.next());
}
for (int i = 0; i < inputs.size(); i++)
{
System.out.print("Input movement: ");
states.get(j).move.add(in.next());
}
}
System.out.print("Print out\n");
printOut(states);
System.out.println("Input single one:");
ArrayList<String> inp = new ArrayList<>();
do
{
String inpu = in.next();
if (inpu.equals("end"))
{
break;
} else
{
inp.add(inpu);
}
} while (true);
modifyTape(states, inp);
}
public static void printOut(ArrayList<State> states)
{
for (int i = 0; i < states.size(); i++)
{
for (int j = 0; j < states.get(i).nextStates.size(); j++)
{
System.out.print(" " + states.get(i).nextStates.get(j));
}
for (int j = 0; j < states.get(i).nextStates.size(); j++)
{
System.out.print(" " + states.get(i).outPuts.get(j));
}
System.out.println();
}
}
public static String getOutput(ArrayList<State> states, ArrayList<String> input)
{
int index = 0;
String output = "";
for (int i = 0; i < input.size(); i++)
{
output += states.get(index).outPut(input.get(i));
index = states.get(index).nextState(input.get(i));
}
return output;
}
public static void modifyTape(ArrayList<State> states, ArrayList<String> tape)
{
int index = 0;
int tapIndex = 0;
// Search for the first available symbol
for (int i = 0; i < tape.size(); i++)
{
if (!tape.get(i).equals("B"))
{
tapIndex = i;
break;
}
}
//Do the process until machine halt
while (true)
{
try
{
String cell = tape.get(tapIndex);
System.out.println("TAPE: " + tape + " TAP INDEX: " + tapIndex);
// Replace the cell at position tapIndex to the output of the state which gets input from that cell
tape.set(tapIndex, states.get(index).outPut(cell));
if(states.get(index).move(cell).equals("R"))
{
tapIndex++;
}else if(states.get(index).move(cell).equals("L"))
{
tapIndex--;
}
index = states.get(index).nextState(cell);
if(index == -1)
{
throw new Exception();
}
} catch (Exception ex)
{
System.out.println("Machine Halt at " + tapIndex);
break;
}
}
}
}
State.java
- Code:
import java.util.ArrayList;
/**
*
* @author ThienDinh
*/
/**
* This class is just for a single state. It can access its next states and
* output determined by set up input
*
* @author ThienDinh
*/
public class State {
public ArrayList<String> inputTypes;
public ArrayList<Integer> nextStates;
public ArrayList<String> outPuts;
public ArrayList<String> move;
public State(ArrayList<String> inputTypes)
{
this.inputTypes = inputTypes;
nextStates = new ArrayList<>();
outPuts = new ArrayList<>();
move = new ArrayList<>();
}
/**
* Return the index of the next state
*
* @param state
* @param input
* @return the index of the next state
*/
public int nextState(String input)
{
for (int i = 0; i < inputTypes.size(); i++)
{
if (inputTypes.get(i).equals(input))
{
return nextStates.get(i);
}
}
return -1;
}
/**
* Return the output of the state
*
* @param state
* @param input
* @return the output of the state
*/
public String outPut(String input)
{
for (int i = 0; i < inputTypes.size(); i++)
{
if (inputTypes.get(i).equals(input))
{
return outPuts.get(i);
}
}
return "";
}
public String move(String input)
{
for (int i = 0; i < inputTypes.size(); i++)
{
if (inputTypes.get(i).equals(input))
{
return move.get(i);
}
}
return "";
}
}
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Sat Feb 15, 2014 10:12 am by doriperni
» Sync2 V2.20.1312 X86x64.rar.rar
Fri Feb 14, 2014 7:37 pm by carmibire
» 9.1 Inheritance Hierarchies
Tue Feb 26, 2013 12:50 pm by ThienDinh
» Useful website
Sat Dec 08, 2012 6:52 pm by tpham1991
» The Machine that Changed the World: Giant Brains. 1992 Documentary
Sat Dec 08, 2012 2:16 am by ThienDinh
» Problems in practice test.
Thu Dec 06, 2012 2:32 pm by ThienDinh
» Newton's Method
Mon Dec 03, 2012 8:32 pm by ThienDinh
» Console Calc
Sun Dec 02, 2012 5:57 pm by Petahwil
» Law Of Cosines
Sat Dec 01, 2012 4:54 pm by Petahwil