Jump to content

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(tapethis + " --- " + this.transitions.get(tsp));
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(tapethis + " --- " + tsp);
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 += this.headtape.previousget(i) + " ";
}
 
s += "[H] ";
for (int i = headPos + 1; i < this.headtape.nextsize(); i++) {
s += this.headtape.nextget(i) + " ";
}
 
return s + "]";
} catch (Exception e) {
return "";
}
}
Line 776 ⟶ 794:
{{out}}
 
<pre>[ [H] 1, 1, 1 ] --- (q0,1)=>(q0,1)/1
[ 1, [H] 1, 1 ] --- (q0,1)=>(q0,1)/1
[ 1, 1, [H] 1 ] --- (q0,1)=>(q0,1)/1
[ 1, 1, 1, [H] b ] --- (q0,b)=>(qf,1)/0
[ 1, 1, 1, [H] 1 ] --- (qf,1)
Output (si): [1, 1, 1, 1]
 
[ [H] 0 ] --- (a,0)=>(b,1)/1
[ 1, [H] 0 ] --- (b,0)=>(a,1)/-1
[ [H] 1, 1 ] --- (a,1)=>(c,1)/-1
[ [H] 0, 1, 1 ] --- (c,0)=>(b,1)/-1
[ [H] 0, 1, 1, 1 ] --- (b,0)=>(a,1)/-1
[ [H] 0, 1, 1, 1, 1 ] --- (a,0)=>(b,1)/1
[ 1, [H] 1, 1, 1, 1 ] --- (b,1)=>(b,1)/1
[ 1, 1, [H] 1, 1, 1 ] --- (b,1)=>(b,1)/1
[ 1, 1, 1, [H] 1, 1 ] --- (b,1)=>(b,1)/1
[ 1, 1, 1, 1, [H] 1, 0] --- (b,01)=>(b,1)/1
[1, 1, 1, 1, 1, 1 [H] 0 ] --- (b,0)=>(a,1)/-1
[ 1, 1, 1, 1, [H] 1, 1 ] --- (a,1)=>(c,1)/-1
[ 1, 1, 1, [H] 1, 1, 1 ] --- (c,1)=>(halt,1)/0
[ 1 1 1 [H] 1 1 1 ] --- (halt,1)
Output (bb): [1, 1, 1, 1, 1, 1]</pre>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.