Anonymous user
Universal Turing machine: Difference between revisions
Fixed a bug in the code - head was placed incorrectly after extending tape to the right.
m (→{{header|Java}}: Generics and foreach started in Java 5, more-correct highlighting, no signatures on examples) |
(Fixed a bug in the code - head was placed incorrectly after extending tape to the right.) |
||
Line 587:
public class UTM {
private LinkedList<String> tape;
private String blankSymbol;
Line 699 ⟶ 698:
while (this.transitions.containsKey(tsp)) { // While a matching transition exists.
System.out.println(
Transition trans = this.transitions.get(tsp);
this.head.set(trans.to.tapeSymbol); // Write tape symbol.
tsp.state = trans.to.state; // Change state.
if (trans.direction == -1) { // Go left.
tsp.tapeSymbol = this.head.previous(); // Memorize tape symbol.▼
if (!this.head.hasPrevious()) {
this.head.add(this.blankSymbol); // Extend tape.
this.head.previous();▼
this.head.next();▼
tsp.tapeSymbol = this.blankSymbol; // Memorize tape symbol.
}
▲ tsp.tapeSymbol = this.head.previous(); // Memorize tape symbol.
} else if (trans.direction == 1) { // Go right.
tsp.tapeSymbol = this.head.next(); // Memorize tape symbol.
Line 716 ⟶ 713:
this.head.add(this.blankSymbol); // Extend tape.
this.head.previous();
this.head.next();▼
tsp.tapeSymbol = this.blankSymbol; // Memorize tape symbol.
}
Line 724 ⟶ 720:
}
System.out.println(
if (this.terminalStates.contains(tsp.state)) {
Line 730 ⟶ 726:
} else {
return null;
}
▲ }
@Override
public String toString() {
try {
int headPos = this.head.previousIndex();
String s = "[ ";
for (int i = 0; i <= headPos; i++) {
}
s += "[H] ";
}
return s + "]";
} catch (Exception e) {
return "";
}
}
Line 776 ⟶ 794:
{{out}}
<pre>[ [H] 1
[ 1
[ 1
[ 1
[ 1
Output (si): [1, 1, 1, 1]
[ [H] 0 ] --- (a,0)=>(b,1)/1
[ 1
[ [H] 1
[ [H] 0
[ [H] 0
[ [H] 0
[ 1
[ 1
[ 1
[ 1
[
[ 1
[ 1
[ 1 1 1 [H] 1 1 1 ] --- (halt,1)
Output (bb): [1, 1, 1, 1, 1, 1]</pre>
|