-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
157 lines (108 loc) · 2.87 KB
/
script.js
File metadata and controls
157 lines (108 loc) · 2.87 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var times2;
var rules = [];
var currentState = 0;
var currentPosition = 0;
var counter = 0;
var newSymbol = "B";
$(document).ready(function(){
//setRules($("#rules").text());
$("#1step").click(function(){
nextStep();
});
$("#setTape").click(function(){
$("#machine").text($("#tapeChanger").val());
});
$("#setNewSymbol").click(function(){
var symbol = $("#newSymbolChanger").val();
if(symbol.length == 1 && symbol != "" && symbol != " "){
newSymbol = symbol;
alert("Symbol Setted");
}else{
alert("Error: Symbol not Valid");
}
});
$("#set").click(function(){
setRules($("textarea").val());
});
$("#autoStep").click(function(){
times2 = setInterval(function () {nextStep()}, 100);
});
$("#stopStep").click(function(){
clearInterval(times2);
});
});
//var tape = $("textarea").text();
//var len = tape.length;
function nextStep(){
//var tape1 = $("#machine").text();
//console.log(tape1.length);
//console.log(currentPosition);
for(var i = 0;i < rules.length;i++){
if(rules[i][0] == currentState && rules[i][0] != "h" && rules[i][1] == getCurrentPositionNumber() ){
setCurrentPositionNumber(rules[i][2]);
if(rules[i][3] == "r"){
var tape1 = $("#machine").text();
if(currentPosition == (tape1.length-1)){
////alert("UGUALE");
$("#machine").text(tape1+newSymbol);
////currentPosition++;
//console.log("tape+1");
}
currentPosition++;
counter++
//$("#machine").text(tape1+"0");
//currentPosition++;
}else{
console.log(currentPosition);
var tape1 = $("#machine").text();
if(currentPosition == 0){
////alert("UGUALE");
$("#machine").text("B"+tape1);
currentPosition++;
//console.log("tape-1");
}
currentPosition--;
counter++;
}
currentState = rules[i][4];
$("#counter").text(counter);
return;
}
}
}
function setRules(textRules){
/*
var rule = [0,0,1,"r",0];
var rule2 = [0,3,5,"l",0];
var rule3 = [0,1,6,"l",1];
var rule4 = [1,1,7,"l",0];
//rules.push(rule);
//rules.push(rule2);
//rules.push(rule3);
//rules.push(rule4);
*/
rules = [];
var splitted = textRules.split("\n");
for(var i = 0; i < splitted.length; i++){
var splitArray = splitted[i].split(" ");
rules.push(splitArray);
}
console.log(rules);
alert("Rules Setted")
}
function getCurrentPositionNumber(){
var tape1 = $("#machine").text();
var strChars = tape1.split("");
//console.log(strChars);
return strChars[currentPosition];
}
function setCurrentPositionNumber(num){
var tape2 = $("#machine").text();
var strChars = tape2.split("");
strChars[currentPosition] = ""+num;
var finalString = strChars.join("");
//console.log(finalString);
$("#machine").text(finalString);
//console.log(strChars);
//return strChars[currentPosition];
}