1 $(document
).ready(function(){
3 var canvas
= $('#2d-automaton')[0];
4 var c
= canvas
.getContext('2d');
7 function cell(x
, y
, h
, l
) {
13 this.kill = function() {
14 c
.fillStyle
= "rgb(255,255,255)";
15 c
.fillRect(this.x
, this.y
, this.h
, this.l
);
18 this.revive = function() {
19 c
.fillStyle
= "rgb(0,0,0)";
20 c
.fillRect(this.x
, this.y
, this.h
, this.l
);
23 this.toggle = function() {
24 this.state
? this.kill() : this.revive();
28 function universe(blank
) {
32 this.blank
= blank
|| 0;
37 var cellWidth
= canvas
.width
/ this.rows
;
38 var cellHeight
= canvas
.height
/ this.columns
;
40 for (var i
= 0; i
< this.rows
; i
++) {
43 var y
= i
* cellHeight
;
44 for (var ii
= 0; ii
< this.columns
; ii
++) {
45 world
.push(new cell(x
, y
, cellWidth
, cellHeight
));
47 if (this.blank
) { world
[ii
].kill(); }
48 else { Math
.random() > .5 ? world
[ii
].revive() : world
[ii
].kill(); }
50 this.population
.push(world
);
53 this.redraw = function() {
54 for (var i
= 0; i
< this.rows
; i
++) {
55 for (var ii
= 0; ii
< this.columns
; ii
++) {
56 if (this.population
[i
][ii
].state
== 1) {
57 this.population
[i
][ii
].redraw();
63 this.serialize = function() {
65 for (var i
= 0; i
< this.rows
; i
++) {
66 for (var ii
= 0; ii
< this.columns
; ii
++) {
67 serial
+= this.population
[i
][ii
].state
;
74 function tick(automaton
) {
75 automaton
.generation
+= 1;
76 var universe
= automaton
.population
;
78 for (var i
= 0; i
< universe
.length
; i
++) {
80 for (var ii
= 0; ii
< universe
[i
].length
; ii
++) {
81 var neighbors
= (universe
[i
][ii
+1] ? universe
[i
][ii
+1].state
: 0) +
82 (universe
[i
][ii
-1] ? universe
[i
][ii
-1].state
: 0) +
84 (universe
[i
+1][ii
] ? universe
[i
+1][ii
].state
: 0) +
85 (universe
[i
+1][ii
+1] ? universe
[i
+1][ii
+1].state
: 0) +
86 (universe
[i
+1][ii
-1] ? universe
[i
+1][ii
-1].state
: 0)
89 (universe
[i
-1][ii
] ? universe
[i
-1][ii
].state
: 0) +
90 (universe
[i
-1][ii
-1] ? universe
[i
-1][ii
-1].state
: 0) +
91 (universe
[i
-1][ii
+1] ? universe
[i
-1][ii
+1].state
: 0)
93 universe
[i
][ii
].neighbors
= neighbors
;
94 if (universe
[i
][ii
].state
== 1) {
95 if (neighbors
< 2 || neighbors
> 3) {
96 newUniverse
[i
][ii
] = 0;
98 newUniverse
[i
][ii
] = 1;
101 newUniverse
[i
][ii
] = (neighbors
== 3 ? 1 : 0);
105 for (var i
in newUniverse
) {
106 for (var ii
in newUniverse
[i
]) {
107 newUniverse
[i
][ii
] ? universe
[i
][ii
].revive() : universe
[i
][ii
].kill();
110 $('#generation')[0].innerHTML
= automaton
.generation
;
113 var automaton
= new universe
;
117 $('#controls #start-automaton').click(function(e
){
118 tickID
= setInterval(function(){tick(automaton
)}, 100);
119 $('#controls #start-automaton').hide();
120 $('#controls #stop-automaton').show();
124 $('#controls #stop-automaton').click(function(e
){
125 if (running
) clearInterval(tickID
);
126 $('#controls #stop-automaton').hide();
127 $('#controls #start-automaton').show();
131 $('#controls #reseed-automaton').click(function(e
){
132 if (running
) clearInterval(tickID
);
133 $('#controls #stop-automaton').hide();
134 $('#controls #start-automaton').show();
135 automaton
= new universe();
139 $('#controls #custom-seed-automaton').click(function(e
){
140 if (running
) clearInterval(tickID
);
141 $('#controls #stop-automaton').hide();
142 $('#controls #start-automaton').show();
143 automaton
= new universe(1);
147 $('#2d-automaton').click(function(e
) {
149 var x
= e
.pageX
- $('#2d-automaton').offset().left
;
150 var y
= e
.pageY
- $('#2d-automaton').offset().top
;
151 var row
= Math
.floor(y
/ cellHeight
);
152 var column
= Math
.floor(x
/ cellWidth
);
153 automaton
.population
[row
][column
].toggle();