1 $(document
).ready(function(){
3 var canvas
= $('#2d-automaton')[0];
4 var c
= canvas
.getContext('2d');
9 var cellWidth
= canvas
.width
/ rows
;
10 var cellHeight
= canvas
.height
/ columns
;
12 function cell(x
, y
, h
, l
) {
18 this.kill = function() {
19 c
.fillStyle
= "rgb(255,255,255)";
20 c
.fillRect(this.x
, this.y
, this.h
, this.l
);
23 this.revive = function() {
24 c
.fillStyle
= "rgb(0,0,0)";
25 c
.fillRect(this.x
, this.y
, this.h
, this.l
);
28 this.toggle = function() {
29 this.state
? this.kill() : this.revive();
33 function universe(blank
) {
36 var blank
= blank
|| 0;
38 for (var i
= 0; i
< rows
; i
++) {
41 var y
= i
* cellHeight
;
42 for (var ii
= 0; ii
< columns
; ii
++) {
43 world
.push(new cell(x
, y
, cellWidth
, cellHeight
));
45 if (blank
) { world
[ii
].kill(); }
46 else { Math
.random() > .5 ? world
[ii
].revive() : world
[ii
].kill(); }
48 this.population
.push(world
);
51 this.highlight = function() {
52 for (var i
= 0; i
< rows
; i
++) {
53 for (var ii
= 0; ii
< columns
; ii
++) {
54 if (this.population
[i
][ii
].state
== 1) {
55 this.population
[i
][ii
].highlight();
61 this.redraw = function() {
62 for (var i
= 0; i
< rows
; i
++) {
63 for (var ii
= 0; ii
< columns
; ii
++) {
64 if (this.population
[i
][ii
].state
== 1) {
65 this.population
[i
][ii
].redraw();
72 function tick(automaton
, blank
) {
73 automaton
.generation
+= 1;
74 var universe
= automaton
.population
;
76 for (var i
= 0; i
< universe
.length
; i
++) {
78 for (var ii
= 0; ii
< universe
[i
].length
; ii
++) {
79 var neighbors
= (universe
[i
][ii
+1] ? universe
[i
][ii
+1].state
: 0) +
80 (universe
[i
][ii
-1] ? universe
[i
][ii
-1].state
: 0) +
82 (universe
[i
+1][ii
] ? universe
[i
+1][ii
].state
: 0) +
83 (universe
[i
+1][ii
+1] ? universe
[i
+1][ii
+1].state
: 0) +
84 (universe
[i
+1][ii
-1] ? universe
[i
+1][ii
-1].state
: 0)
87 (universe
[i
-1][ii
] ? universe
[i
-1][ii
].state
: 0) +
88 (universe
[i
-1][ii
-1] ? universe
[i
-1][ii
-1].state
: 0) +
89 (universe
[i
-1][ii
+1] ? universe
[i
-1][ii
+1].state
: 0)
91 universe
[i
][ii
].neighbors
= neighbors
;
92 if (universe
[i
][ii
].state
== 1) {
93 if (neighbors
< 2 || neighbors
> 3) {
94 newUniverse
[i
][ii
] = 0;
96 newUniverse
[i
][ii
] = 1;
99 newUniverse
[i
][ii
] = (neighbors
== 3 ? 1 : 0);
103 for (var i
in newUniverse
) {
104 for (var ii
in newUniverse
[i
]) {
105 newUniverse
[i
][ii
] ? universe
[i
][ii
].revive() : universe
[i
][ii
].kill();
108 $('#generation')[0].innerHTML
= automaton
.generation
;
111 var automaton
= new universe
;
115 $('#controls #start-automaton').click(function(e
){
116 tickID
= setInterval(function(){tick(automaton
)}, 100);
117 $('#controls #start-automaton').hide();
118 $('#controls #stop-automaton').show();
122 $('#controls #stop-automaton').click(function(e
){
123 if (running
) clearInterval(tickID
);
124 $('#controls #stop-automaton').hide();
125 $('#controls #start-automaton').show();
129 $('#controls #reseed-automaton').click(function(e
){
130 if (running
) clearInterval(tickID
);
131 $('#controls #stop-automaton').hide();
132 $('#controls #start-automaton').show();
133 automaton
= new universe();
137 $('#controls #custom-seed-automaton').click(function(e
){
138 if (running
) clearInterval(tickID
);
139 $('#controls #stop-automaton').hide();
140 $('#controls #start-automaton').show();
141 automaton
= new universe(1);
145 $('#2d-automaton').click(function(e
) {
147 var x
= e
.pageX
- $('#2d-automaton').offset().left
;
148 var y
= e
.pageY
- $('#2d-automaton').offset().top
;
149 var row
= Math
.floor(y
/ cellHeight
);
150 var column
= Math
.floor(x
/ cellWidth
);
151 automaton
.population
[row
][column
].toggle();