Our teacher doesn't give much feedback on our code itself. Was just looking for some feedback on how I could improve on this. This is the method that tests the string against the Turing machine that is built from a file.
The entire project is on GitHub.
public boolean run(String input) {
String currentState = startState;
ArrayList<Transition> pT;
ArrayList<Character> chars = new ArrayList<>();
int i = 0;
for (char c : input.toCharArray()) {
if (!inputAlphabet().contains(c)) {
return false;
} else {
chars.add(c);
}
}
tape.setCells(chars);
tape.setHeadPosition(i);
System.out.println(currentState);
while (i != tape.getCells().size()) {
pT = new ArrayList<>();
for (Transition t : transitions) {
if (t.getFromState().equals(currentState)) {
pT.add(t);
}
}
for (int j = 0; j < pT.size(); j++) {
if (tape.getCells().get(i) == pT.get(j).getInputSymbol()) {
if (pT.get(j).getWriteSymbol() != EMPTY_SYMBOL) {
ArrayList<Character> temp;
temp = tape.getCells();
temp.set(i, pT.get(j).getWriteSymbol());
tape.setCells(temp);
}
if (pT.get(j).getDirection().charAt(0) == DIRECTION_RIGHT) {
i++;
tape.setHeadPosition(i);
} else if (pT.get(j).getDirection().charAt(0) == DIRECTION_LEFT) {
i--;
tape.setHeadPosition(i);
}
currentState = pT.get(j).getToState();
System.out.println(currentState);
j = pT.size();
}
}
if (rejectStates.contains(currentState)) {
return false;
}
}
pT = new ArrayList<>();
for (Transition t : transitions) {
if (t.getFromState().equals(currentState)) {
pT.add(t);
}
}
for (int k = 0; k < pT.size(); k++) {
if (pT.get(k).getInputSymbol() == EMPTY_SYMBOL) {
currentState = pT.get(k).getToState();
System.out.println(currentState);
if (pT.get(k).getDirection().charAt(0) == DIRECTION_RIGHT) {
i++;
tape.setHeadPosition(i);
} else if (pT.get(k).getDirection().charAt(0) == DIRECTION_LEFT) {
i--;
tape.setHeadPosition(i);
}
k = pT.size();
}
}
return acceptStates.contains(currentState);
}