-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm.js
101 lines (80 loc) · 1.77 KB
/
algorithm.js
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
class Algorithm {
constructor(args, vars, lines, blocks, transition){
for (var i = 0; i < lines.length; i++) {
lines[i] = i + ". " + lines[i]
}
this.lines = lines
if (blocks == null){
blocks = []
for (var i in lines){
blocks.push(i)
}
}
this.blocks = blocks
this.transition = transition
this.vars = {};
for (let k of args){
this.vars[k] = null
}
for (let k of vars){
this.vars[k] = null
}
this.state = null
this.end_state = transition.length - 1
let p = createElement('pre')
p.parent('pseudoCode')
this.code = createElement('code')
this.code.parent(p)
this.update_code()
}
update_code(){
let t = ""
for (var i = 0; i < this.blocks.length; i++) {
if (i == this.state){
t += "<curr_state>"
}
let b = []
for (let j of this.blocks[i]){
b.push(this.lines[j])
}
t += b.join('\n')
if (i == this.state){
t += "</curr_state>"
}
t += "\n"
}
this.code.html(t)
}
load_data(kwargs){
for (let x in kwargs){
this.vars[x] = kwargs[x]
}
this.state = 0
this.update_code()
}
_step(){
console.log("Step not implemented")
throw new Error("Step function not implemented")
}
step(){
let nxt = this._step()
if (nxt == null){
console.log("no choice")
this.state = this.transition[this.state]
}
else{
console.log("choice: " + Number(nxt))
this.state = this.transition[this.state][Number(nxt)]
}
console.log("Current state: " + this.state)
this.update_code()
}
finish(){
while (this.state != this.end_state){
this.step()
}
}
draw(){
console.log(this.state, this.vars)
}
}