-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
282 lines (247 loc) · 6.94 KB
/
script.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* Game of Life */
/* ### Initialization ### */
// declare game grid width
const gridWidth = 100;
const gridHeight = 100;
const gridScale = 5;
// create the grid
var gameGrid = createGrid(gridHeight, gridWidth);
// create javascript canvas to draw game on
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.scale(gridScale, gridScale);
ctx.fillStyle = 'hsl(' + 360 * Math.random() + ', 50%, 50%)';
var fps = 10;
var timeout = 1000;
var playing = false;
// initialize control buttons
setupControlButtons();
play();
/* ### Functions ### */
/* Exercise 1 */
// create an array that contains another array for multi 2d array in js
// return an array with n elements and place an empty array in each of them in the for loop
function createGrid(rows, cols) {
var array = [];
for (var i = 0; i < rows; i++){
array[i] = [];
for (let j = 0; j < cols; j++){
array[i][j] = 0;
}
}
return array;
}
/* Exercise 2 */
// randomly populate the grid with 1 and 0, live and dead cells
function randomCells(){
// iterate through rows
for (var j = 25; j < gridHeight - 25; j++){
// iterate through columns
for (var k = 25; k < gridWidth - 25; k++){
// get a random number then set cells to alive / dead depending on the value
var randomNum = Math.random();
if (randomNum <= 0.5) {
gameGrid[j][k] = 1;
}
else {
gameGrid[j][k] = 0;
}
}
}
// console.log(gameGrid.join('\n'));
}
/* Exercise 3 */
// draw the grid and its content on screen
function drawGrid(){
for (let i = 0; i < gridHeight; i++){
for (let j = 0; j < gridWidth; j++){
// check cell value and draw on grid if it's a living cell
if (gameGrid[i][j] === 1) {
ctx.fillRect(i, j, 1, 1);
}
}
}
}
function countNeighboursLoop( x, y)
{
var totalNeighbours = 0;
for(let i = x - 1; i <= x + 1; i++){
for(let j = y - 1; j <= y + 1; j++){
// check if indexes don't go out of bounds
if(i >= 0 && j >= 0 && i < gridHeight && j < gridWidth){
//don't count the cell itself
if(!(x == i && y == j)){
totalNeighbours += gameGrid[i][j];
}
}
}
}
return totalNeighbours;
}
function countNeighbours( x, y )
{
let totalCells = 0;
let maxX = gridHeight - 1;
let maxY = gridWidth - 1;
// top
if(x > 0){
totalCells += (y > 0) ? gameGrid[x - 1][y - 1] : 0; //top left
totalCells += (y < maxY)? gameGrid[x - 1][y + 1] : 0; //top right
totalCells += gameGrid[x - 1][y]; //top center
}
// bottom
if(x < maxX){
totalCells += (y > 0) ? gameGrid[x + 1][y - 1] : 0; //bottom left
totalCells += (y < maxY)? gameGrid[x + 1][y + 1] : 0; //bottom right
totalCells += gameGrid[x + 1][y]; //bottom center
}
// middle
totalCells += (y > 0) ? gameGrid[x][y - 1] : 0; //middle left
totalCells += (y < maxY) ? gameGrid[x][y + 1]: 0; //middle right
return totalCells;
}
function mirrorEdges(mirrorArray)
{
// mirror edges so organisms can wrap around the grid
for (let i = 0; i < gridHeight; i++) {
//left and right
mirrorArray[i][0] = mirrorArray[i][gridWidth - 2];
mirrorArray[i][gridWidth - 1] = mirrorArray[i][1];
}
for (let i = 0; i < gridWidth; i++) {
//top and bottom
mirrorArray[0][i] = mirrorArray[gridHeight - 2][i];
mirrorArray[gridHeight - 1][i] = mirrorArray[1][i];
}
}
/* Exercise 4 */
// update the game grid every tick according to 'game of life' rules
function updateGrid(){
// create a mirror grid to store changes from update.
var mirrorGrid = createGrid(gridHeight, gridWidth);
for (let i = 0; i < gridHeight; i++){
for (let j = 0; j < gridWidth; j++){
// save total cells
var totalCells = countNeighbours(i, j);
// save game grid in mirror grid in order to apply game rules to it
switch(totalCells){
case 1:
mirrorGrid[i][j] = 0; // kill cell due to underpopulation
break;
case 2:
mirrorGrid[i][j] = gameGrid[i][j]; // keep cell status
break;
case 3:
mirrorGrid[i][j] = 1; // if the cell has 3 neighbors, switch to alive
break;
default:
mirrorGrid[i][j] = 0; // dead cell
}
}
}
mirrorEdges(mirrorGrid);
// apply changes from mirror grid to gameGrid
gameGrid = mirrorGrid;
}
/* Exercise 6 */
// setup control buttons
function setupControlButtons() {
// button to start
var startButton = document.getElementById("start");
startButton.onclick = startButtonHandler;
// button to clear
var clearButton = document.getElementById("clear");
clearButton.onclick = clearButtonHandler;
// button to set random initial state
var randomButton = document.getElementById("random");
randomButton.onclick = randomButtonHandler;
// button to speed up time
var fpsUpButton = document.getElementById("fps_up");
fpsUpButton.onclick = fpsUpButtonHandler;
// button to slpw down time
var fpsDownButton = document.getElementById("fps_down");
fpsDownButton.onclick = fpsDownButtonHandler;
// button to call one step update
var oneUpdateButton = document.getElementById("one_update");
oneUpdateButton.onclick = oneUpdateButtonHandler;
}
function randomButtonHandler(){
// call grid creation functions and game loop
randomCells();
}
function fpsUpButtonHandler(){
if (fps <= 60){
fps += 10;
}
else
fps = 60;
}
function fpsDownButtonHandler(){
if (fps >= 10){
fps -= 10;
}
else
fps = 1;
}
/* Exercise 3 */
// update the grid to see living cells on initialization
function clearGrid(){
location.reload();
}
function startButtonHandler(){
if (playing) {
console.log("Pause the game");
playing = false;
this.innerHTML = "Continue";
} else {
console.log("Continue the game");
playing = true;
this.innerHTML = "Pause";
}
}
function clearButtonHandler(){
clearGrid();
}
function oneUpdateButtonHandler()
{
updateGrid();
}
/* Exercise 5 */
// Main function with game step logic
function play() {
if(playing)
{
updateGrid();
}
// empty canvas before redraw
ctx.clearRect(0,0,gridHeight,gridWidth);
drawGrid();
// measure time it takes for one game tick to complete
// restrict update rate to set frames per second
setTimeout(function() {
requestAnimationFrame(play);
}, timeout / fps);
}
/* Exercise 7 */
// add draw function and pause button
function handleEvent(oEvent) {
var canvas = document.getElementById("myCanvas");
console.log(`mouse click on canvas screenY: ${oEvent.screenX} screenY: ${oEvent.screenY} `);
var rect = canvas.getBoundingClientRect();
console.log(`mouse click on rect top: ${rect.top} left: ${rect.left} `);
let x = oEvent.clientX - rect.left;
let y = oEvent.clientY - rect.top;
console.log(`mouse click offsetX: ${x} offsetY: ${y} `);
let cellX = Math.floor(x / gridScale);
let cellY = Math.floor(y / gridScale);
console.log(`mouse click cellX: ${cellX} cellY: ${cellY} `);
if( cellX >= 0 && cellY >= 0 && cellX < gridHeight && cellY < gridWidth)
{
if(gameGrid[cellX][cellY] == 0){
gameGrid[cellX][cellY] = 1;
}
else{
gameGrid[cellX][cellY] = 0;
}
}
}