-
Notifications
You must be signed in to change notification settings - Fork 0
/
sidewinder.html
55 lines (48 loc) · 1.36 KB
/
sidewinder.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
background: #000;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="/grid.js"></script>
<script>
function sidewinder(cells) {
function toss() {
if (Math.floor(Math.random() * 2) === 0) {
return heads;
} else {
return tails;
}
}
for (var i = cellsHigh - 1; i >= 0; i--) {
j = 0;
while (j < cellsWide) {
idx = i * cellsWide + j;
// sidewinder build a 'run' by going E if a toss of a coin is tails.
// when the toss is heads, stop expanding the run and select randomly
// out of the run which cell expands N.
run = [idx];
while (j < cellsWide - 1 && (i === 0 || toss() === tails)) { // if i === 0, expand E by default
cells[idx].links.push(idx+1);
cells[idx+1].links.push(idx);
run.push(idx+1);
j++;
idx++;
} // toss() was heads, or E border is reached
if (i > 0) { // only expand N if there is an upper row
selected = run[Math.floor(Math.random() * run.length)];
cells[selected].links.push(cells[selected].N);
cells[cells[selected].N].links.push(selected);
}
j++;
}
}
}
initGrid();
sidewinder(cells);
drawGrid();
d3.select(self.frameElement).style("height", height + "px");
</script>