-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
174 lines (129 loc) · 4.74 KB
/
game.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
var G_MATRIX = {
config: {
matsize: 100, // size of the side of the square.
// eg: 64 creates a 64x64 matrix.
cellsize: 2, // size of the side of a single cell
colors: ['black', 'white'], // [background, foreground]
stroked: false, // if true, will draw the borders of each cells
start_pos: [0, 0], // Coordinates of the top right corner of the matrix
// relative to the beginning of the canvas.
refresh_rate: 1000 / 60, // speed of evolution of the game (in ms)
},
get_cell_value: function (i, j) {
if (i<0) {
i += this.config.matsize;
}
if (i >= this.config.matsize) {
i -= this.config.matsize;
}
if (j < 0) {
j += this.config.matsize;
}
if (j >= this.config.matsize) {
j -= this.config.matsize;
}
return this.cells[i + j*this.config.matsize];
},
// draw the matrix on the context given in argument
draw: function (context) {
var x = this.config.start_pos[0];
var y = this.config.start_pos[1];
var line;
for (var i = 0; i < this.config.matsize; i++) {
for (var j = 0; j < this.config.matsize; j++) {
context.beginPath();
context.rect(x, y, this.config.cellsize, this.config.cellsize);
context.fillStyle = this.config.colors[this.get_cell_value(j, i)];
context.fill();
if (this.config.stroked)
context.stroke();
context.closePath();
x += this.config.cellsize;
}
// beginning of the next line
x = this.config.start_pos[0];
y += this.config.cellsize;
}
},
// updates the value of 'cells' according to the laws
// of Conway's Game of Life
next_iteration: function() {
newmatrix = [];
for (i=0; i < this.config.matsize; ++i) {
for (j=0; j < this.config.matsize; ++j) {
// gathering the number of live neighbours
neighbours = this.get_cell_value(j-1, i-1);
neighbours += this.get_cell_value(j-1, i);
neighbours += this.get_cell_value(j-1, i+1);
neighbours += this.get_cell_value(j, i-1);
neighbours += this.get_cell_value(j, i+1);
neighbours += this.get_cell_value(j+1, i-1);
neighbours += this.get_cell_value(j+1, i);
neighbours += this.get_cell_value(j+1, i+1);
// the current value of the cell
value = this.get_cell_value(j, i);
// modification of the value according to Conway's laws
if (value === 1) {
if (neighbours < 2) {value = 0;} // underpopulation
if (neighbours > 3) {value = 0;} // overcrowding
if (neighbours === 2 || neighbours === 3) {value = 1;}
}
else {
if (neighbours === 3) {value = 1;} // reproduction
}
newmatrix.push(value);
}
}
this.cells = newmatrix;
},
// helper function that returns a randomly filled matrix
init: function () {
var matrix = [];
for (var i=0; i < this.config.matsize; ++i) {
for (var j = 0; j < this.config.matsize; ++j) {
// push random value: 0 or 1.
matrix.push(Math.floor(Math.random() * 2));
}
}
this.cells = matrix;
},
blink: function() {
this.config.matsize = 5;
this.config.cellsize = 20;
var matrix = [0,0,0,0,0,
0,0,1,0,0,
0,1,0,1,0,
0,1,0,1,0,
0,0,1,0,0];
this.cells = matrix;
}
};
// polyfill to get a RequestAnimationFrame
window.requestAnimFrame = (function(callback) {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, G_MATRIX.config.refresh_rate);
};
})();
function animate (canvas) {
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
G_MATRIX.draw(context);
G_MATRIX.next_iteration();
// request new frame
requestAnimFrame(function() {
animate(canvas);
});
}
window.onload = function () {
var canvas = document.getElementById('myCanvas');
// random initial state
//G_MATRIX.blink();
G_MATRIX.init();
// launch animation
animate(canvas);
};