-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (61 loc) · 1.51 KB
/
index.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
var G = ''
const loops = 100
main()
function main() {
setNeighbors()
setGraphSelect()
d3.select("#run").on("click", run );
d3.select("#cycles").node().value = loops
d3.select("#cycles").on("change", run )
d3.select("#mode").on("change", run )
run()
}
function run() {
let g = graphs[G]
// reset values in vertices
var gKeys = Object.keys(g.V)
gKeys.forEach ( key => g.V[key].value = 0)
g.V[gKeys[0]].value = 1
// reset population
populations = []
populations.push(p())
//render graph
let graph = new Graph("#graph")
let mode = d3.select("#mode").node().value
let cycles = d3.select("#cycles").node().value
//run the game for number of cycles
for(var i=0; i < cycles; i++) {
switch (mode) {
case "random":
sRandom()
break;
case "round":
let n = ((i + 1) % gKeys.length)
let k = gKeys[n]
s(k)
break;
}
}
let table = new Table("#table", populations)
const sumPop = populations.map( row => row.reduce( (tot, val) => tot + val ))
let plot = new Plot("#plot", sumPop)
let code = new Code("#code")
}
function setGraphSelect() {
let select = d3.select("#graphs")
select
.on("change", (d) => {
G = document.getElementById("graphs").value
console.log(G)
run()
})
let keys = Object.keys(graphs)
select.selectAll("option")
.data(keys)
.enter()
.append("option")
.attr("value", (d) => d )
.text( (d) => d )
select.node().value = keys[0]
G = keys[0]
}