-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.java
More file actions
79 lines (63 loc) · 2.15 KB
/
State.java
File metadata and controls
79 lines (63 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.ArrayList;
public class State{
//transition: {'<read>', '<write>', '<final state>', '<action>'}
private ArrayList<char[]> transitionFuncs;
private ArrayList<State> transitionFuncStates;
private Tape tape;
private boolean isHalted;
public State(Tape tape){
isHalted = false;
transitionFuncs = new ArrayList();
transitionFuncStates = new ArrayList();
this.tape = tape;
}
public void setHalt(boolean bool){
isHalted = bool;
}
public boolean isHalted(){
return isHalted;
}
//addTransition(): Add the transition function with the given parameters to the State
public void addTransition(char read, char write, State nextState, char action, char printOutput){
char[] temp = {read,write,action, printOutput};
transitionFuncs.add(temp);
transitionFuncStates.add(nextState);
}//end: addTransition()
//trantion(): Transition to the next state based on the tape input
public State transition(char input, int step){
State nextState = null;
//Loop through each transition and look for input symbol
for(int i=0; i < transitionFuncs.size(); i++){
char[] rwa = transitionFuncs.get(i); //Read Write Action
//look for the transition function with the given inputer
if(rwa[0]==input){
nextState = transitionFuncStates.get(i);
//Get the write and action
char write = rwa[1];
char action = rwa[2];
char printOutput = rwa[3];
//replace the current tape value with the write value
tape.replace(write);
//Move the tape forward or backward depending on action
if(action == 'R'){
tape.next();
}else if(action == 'L'){
tape.previous();
}else if(action == 'H'){
//Set the halt status of the next State to true so the program ends
nextState.setHalt(true);
}
//print tape if flag is set to '1'
if(printOutput == '1'){
System.out.println("|"+tape.toString() + " -Step: " + (step+1) + " -Head: " + tape.getPointer());
}
if(printOutput == '1' && nextState.isHalted()){
System.out.println("|");
System.out.println("|Resulting Tape");
System.out.println("|"+tape.toString());
}
}//end: if
}//end: for
return nextState;
}//end: transition();
}