summaryrefslogtreecommitdiff
path: root/turing-machine/js/turing_machine.js
diff options
context:
space:
mode:
Diffstat (limited to 'turing-machine/js/turing_machine.js')
-rw-r--r--turing-machine/js/turing_machine.js18
1 files changed, 4 insertions, 14 deletions
diff --git a/turing-machine/js/turing_machine.js b/turing-machine/js/turing_machine.js
index 9c21983..a61b43a 100644
--- a/turing-machine/js/turing_machine.js
+++ b/turing-machine/js/turing_machine.js
@@ -1,16 +1,9 @@
class TuringMachine {
- constructor(
- tape = [],
- rules = [],
- initialState = "q0",
- blankSymbol = "B",
- acceptState = "f"
- ) {
+ constructor(tape = [], rules = [], initialState = "q0", acceptState = "f") {
this.tape = tape;
this.rules = this.parseRules(rules);
this.state = initialState;
this.head = 0;
- this.blankSymbol = blankSymbol;
this.acceptState = acceptState;
this.iteration = 0;
@@ -51,7 +44,7 @@ class TuringMachine {
}
step() {
- const currentSymbol = this.tape[this.head] || this.blankSymbol;
+ const currentSymbol = this.tape[this.head];
const key = `${this.state},${currentSymbol}`;
if (!(key in this.rules)) {
return false;
@@ -60,6 +53,7 @@ class TuringMachine {
const [newState, action] = rule.split(",");
this.state = newState;
+ this.iteration++;
if (action === "R") {
this.head += 1;
@@ -73,10 +67,6 @@ class TuringMachine {
return false;
}
- if (this.head >= 0 && this.head < this.tape.length) {
- this.iteration++;
- return true;
- }
- return false;
+ return this.head >= 0 && this.head < this.tape.length;
}
}