/
experiment-2.js
105 lines (97 loc) · 2.37 KB
/
experiment-2.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
100
101
102
103
104
105
(function(){
var points = undefined;
var previousPoints = undefined;
var currentRow = 0;
var rules = {
'01110':0,
'01100':1,
'01010':1,
'01000':0,
'00110':1,
'00100':1,
'00010':1,
'00000':0,
'11110':0,
'11100':1,
'11010':1,
'11000':0,
'10110':1,
'10100':1,
'10010':1,
'10000':0,
'11111':0,
'11101':1,
'11011':1,
'11001':0,
'10111':1,
'10101':1,
'10011':1,
'10001':0,
'01111':0,
'01101':1,
'01011':1,
'01001':0,
'00111':1,
'00101':1,
'00011':1,
'00001':0,
};
function mutateRules(rules){
var keys = Object.keys(rules);
var output = {};
keys.forEach(function(k){
output[k] = rules[k];
});
var twiddle = selectOne(keys);
output[twiddle] = rules[twiddle] === 1 ? 0 : 1;
return output;
}
function mutatePoints(points){
var out = [];
for(var i = 0; i < points.length; i++){
if(Math.random()<0.1){
out[i] = points[i] === 0 ? 1 : 0;
} else {
out[i] = points[i];
}
}
return out;
}
function previousConfig(array,i){
var iprevprev = (i-2 + array.length) % array.length;
var iprev = (i-1 + array.length) % array.length;
var inext = (i+1 + array.length) % array.length;
var inextnext = (i+2 + array.length) % array.length;
var config = [array[iprevprev],array[iprev],array[i],array[inext],array[inextnext]].join('');
return config;
}
function drawRow(context,width,height,row,array){
row = row % height;
for(var i = 0; i < array.length; i ++){
context.fillStyle = array[i] === 0 ? "black" : "white";
context.fillRect(i,row,1,1);
}
}
function calculateNewState(oldstate, rules){
var newstate = new Array(oldstate.length);
for(var i = 0; i < oldstate.length; i ++){
newstate[i] = rules[previousConfig(oldstate,i)];
}
return newstate;
}
window.init = function(context, width, height){
points = new Array(width);
for(var i = 0; i < width;i = i + 1){
points[i] = Math.random()<0.5 ? 1 : 0;
};
};
window.drawFrame = function(context,width,height){
drawRow(context,width,height,currentRow,points);
points = calculateNewState(points,rules);
if(Math.random()<0.1) rules = mutateRules(rules);
if(Math.random()<0.1) points = mutatePoints(points);
currentRow = currentRow + 1;
};
})();
//| current pattern | 111 | 110 | 101 | 100 | 011 | 010 | 001 | 000 |
//| new state for center cell | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 |