forked from dp50mm/chaos-theory-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule30.html
149 lines (124 loc) · 3.92 KB
/
rule30.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
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
<!DOCTYPE html>
<html>
<head>
<title>Rule 30</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<style>
/* Center the svg on the web page */
.svg-container {
width:1000px;
height:1000px;
margin-top:100px;
margin-left:auto;
margin-right:auto;
}
/* Add hover effect to game tiles to make the interaction more intuitive*/
.game-tile:hover {
cursor:pointer;
stroke-width:1px;
}
/* Center the controls on the web page */
.controls {
width:1000px;
margin-left:auto;
margin-right:auto;
}
</style>
<script>
$(document).ready(function() {
var rows = 99;
var cols = 99;
var grid = [];
var width = 1000;
var height = 1000;
for (var r = 0; r < rows; r++) {
var row = [];
for (var c = 0; c < cols; c++) {
row.push({
alive: false
});
}
grid.push(row);
}
// set alive cell.
var cell = grid[0][Math.floor(cols / 2)];
cell.alive = true;
var rules = [
[[1,1,1],0],
[[1,1,0],0],
[[1,0,1],0],
[[1,0,0],1],
[[0,1,1],1],
[[0,1,0],1],
[[0,0,1],1],
[[0,0,0],0]
];
var svg = d3.select('.svg-container').append('svg')
.attr('width', width)
.attr('height', height);
function draw(svg) {
svg.selectAll('rect').remove();
for (var r = 0; r < rows; r++) {
for (var c = 0; c < cols; c++) {
svg.append('rect')
.attr('x', c*(width/cols))
.attr('y', r*(height/rows))
.attr('width', width/cols)
.attr('height', height/rows)
.attr('fill', function() {
if(grid[r][c].alive) {
return 'rgba(0,0,0,0.8)';
} else {
return 'transparent';
}
})
.attr('stroke', 'grey')
.attr('class', 'game-tile')
.attr('r', r)
.attr('c', c);
}
}
}
function drawRow() {
for (var c = 0; c < cols; c++) {
var cell = grid[r][c];
var left = grid[r-1][c-1];
var middle = grid[r-1][c];
var right = grid[r-1][c+1];
for (var t = 0; t < rules.length; t++){
var rule = rules[t];
var leftApplies = false;
var middleApplies = false;
var rightApplies = false;
if (left && left.alive === !!rule[0][0]){
leftApplies = true;
}
if (middle && middle.alive === !!rule[0][1]){
middleApplies = true;
}
if (right && right.alive === !!rule[0][2]){
rightApplies = true;
}
if (leftApplies && middleApplies && rightApplies){
cell.alive = !!rule[1];
}
}
}
draw(svg);
// if it's the last row
if (r === rows - 1){
clearInterval(interval);
}
r++;
}
draw(svg);
var r = 1;
var interval = setInterval(drawRow, 250);
});
</script>
</head>
<body>
<div class='svg-container'></div>
</body>
</html>