c3a4bbe5792829db2e5e801c3b263afe3827be35
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
) {
34 this.blank
= blank
|| 0;
39 this.cellWidth
= canvas
.width
/ this.rows
;
40 this.cellHeight
= canvas
.height
/ this.columns
;
42 for (var i
= 0; i
< this.rows
; i
++) {
45 var y
= i
* this.cellHeight
;
46 for (var ii
= 0; ii
< this.columns
; ii
++) {
47 world
.push(new cell(x
, y
, this.cellWidth
, this.cellHeight
));
49 if (this.blank
) { world
[ii
].kill(); }
50 else { Math
.random() > .5 ? world
[ii
].revive() : world
[ii
].kill(); }
52 this.population
.push(world
);
55 this.start = function() {
56 this.tickID
= setInterval(function(){tick(automaton
)}, 100);
60 this.stop = function() {
61 clearInterval(this.tickID
);
65 this.toggle = function() {
66 this.running
? this.stop() : this.start();
69 this.redraw = function() {
70 for (var i
= 0; i
< this.rows
; i
++) {
71 for (var ii
= 0; ii
< this.columns
; ii
++) {
72 if (this.population
[i
][ii
].state
== 1) {
73 this.population
[i
][ii
].redraw();
79 this.serialize = function() {
81 for (var i
= 0; i
< this.rows
; i
++) {
82 for (var ii
= 0; ii
< this.columns
; ii
++) {
83 serial
+= this.population
[i
][ii
].state
;
90 function tick(automaton
) {
91 automaton
.generation
+= 1;
92 var universe
= automaton
.population
;
94 for (var i
= 0; i
< universe
.length
; i
++) {
96 for (var ii
= 0; ii
< universe
[i
].length
; ii
++) {
97 var neighbors
= (universe
[i
][ii
+1] ? universe
[i
][ii
+1].state
: 0) +
98 (universe
[i
][ii
-1] ? universe
[i
][ii
-1].state
: 0) +
100 (universe
[i
+1][ii
] ? universe
[i
+1][ii
].state
: 0) +
101 (universe
[i
+1][ii
+1] ? universe
[i
+1][ii
+1].state
: 0) +
102 (universe
[i
+1][ii
-1] ? universe
[i
+1][ii
-1].state
: 0)
105 (universe
[i
-1][ii
] ? universe
[i
-1][ii
].state
: 0) +
106 (universe
[i
-1][ii
-1] ? universe
[i
-1][ii
-1].state
: 0) +
107 (universe
[i
-1][ii
+1] ? universe
[i
-1][ii
+1].state
: 0)
109 universe
[i
][ii
].neighbors
= neighbors
;
110 if (universe
[i
][ii
].state
== 1) {
111 if (neighbors
< 2 || neighbors
> 3) {
112 newUniverse
[i
][ii
] = 0;
114 newUniverse
[i
][ii
] = 1;
117 newUniverse
[i
][ii
] = (neighbors
== 3 ? 1 : 0);
121 for (var i
in newUniverse
) {
122 for (var ii
in newUniverse
[i
]) {
123 newUniverse
[i
][ii
] ? universe
[i
][ii
].revive() : universe
[i
][ii
].kill();
126 $('#generation')[0].innerHTML
= automaton
.generation
;
129 var automaton
= new universe
;
131 $('#controls #start-automaton').click(function(e
){
133 $('#controls #start-automaton').hide();
134 $('#controls #stop-automaton').show();
137 $('#controls #stop-automaton').click(function(e
){
139 $('#controls #stop-automaton').hide();
140 $('#controls #start-automaton').show();
143 $('#controls #reseed-automaton').click(function(e
){
145 $('#controls #stop-automaton').hide();
146 $('#controls #start-automaton').show();
147 automaton
= new universe();
150 $('#controls #custom-seed-automaton').click(function(e
){
152 $('#controls #stop-automaton').hide();
153 $('#controls #start-automaton').show();
154 automaton
= new universe(1);
157 $('#2d-automaton').click(function(e
) {
158 if (!automaton
.running
) {
159 var x
= e
.pageX
- $('#2d-automaton').offset().left
;
160 var y
= e
.pageY
- $('#2d-automaton').offset().top
;
161 var row
= Math
.floor(y
/ automaton
.cellHeight
);
162 var column
= Math
.floor(x
/ automaton
.cellWidth
);
163 automaton
.population
[row
][column
].toggle();