-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
121 lines (103 loc) · 3.26 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
const CELL_WIDTH = 20;
const CELL_HEIGHT = 20;
const NROW = 800 / CELL_WIDTH;
const NCOLUMN = 800 / CELL_HEIGHT;
const canvas = document.getElementById('mycanvas');
const ctx = canvas.getContext('2d');
const color = ["black", "white"];
let currentBoard = new Array(NROW).fill(0).map(() => new Array(NCOLUMN).fill(0));
let nextBoard = new Array(NROW).fill(0).map(() => new Array(NCOLUMN).fill(0));
function renderCanvas(board) {
for (let i = 0; i < NROW; i++) {
for (let j = 0; j < NCOLUMN; j++) {
ctx.fillStyle = color[board[i][j]];
ctx.fillRect(i * CELL_WIDTH, j * CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT);
}
}
}
function countAliveNbors(board, x, y) {
let aliveNbor = 0;
for (let dr = -1; dr <= 1; dr++) {
for (let dc = -1; dc <= 1; dc++) {
if (dr !== 0 || dc !== 0) {
const r = x + dr;
const c = y + dc;
if (r >= 0 && r < NROW && c >= 0 && c < NCOLUMN) {
aliveNbor += board[r][c];
}
}
}
}
return aliveNbor;
}
function computeNextBoard(board, anotherBoard) {
for (let i = 0; i < NROW; i++) {
for (let j = 0; j < NCOLUMN; j++) {
const aliveNbor = countAliveNbors(board, i, j);
if (board[i][j] === 1) {
anotherBoard[i][j] = aliveNbor === 2 || aliveNbor === 3 ? 1 : 0;
} else {
anotherBoard[i][j] = aliveNbor === 3 ? 1 : 0;
}
}
}
}
canvas.addEventListener("click", (e) => {
mouseX = e.offsetX;
mouseY = e.offsetY;
const x = Math.floor(mouseX / CELL_WIDTH);
const y = Math.floor(mouseY / CELL_HEIGHT);
currentBoard[x][y] = 1 - currentBoard[x][y];
renderCanvas(currentBoard);
});
function changeColor() {
const newState = 1 - state;
state = newState;
document.getElementById("colorbtn").innerText =
"Change Color : " + color[newState];
}
function clearCanvas() {
currentBoard = new Array(NROW).fill(0).map(() => new Array(NCOLUMN).fill(0));
nextBoard = new Array(NROW).fill(0).map(() => new Array(NCOLUMN).fill(0));
renderCanvas(currentBoard);
}
function nextCanvas() {
computeNextBoard(currentBoard, nextBoard);
[currentBoard, nextBoard] = [nextBoard, currentBoard];
renderCanvas(currentBoard);
}
let isPlaying = false;
let intervalId;
function play() {
if (!isPlaying) {
document.getElementById("playbtn").innerText = "Pause"
isPlaying = true;
intervalId = setInterval(() => {
computeNextBoard(currentBoard, nextBoard);
[currentBoard, nextBoard] = [nextBoard, currentBoard];
renderCanvas(currentBoard);
// Check if all cells are dead
let allDead = true;
for (let i = 0; i < NROW; ++i) {
for (let j = 0; j < NCOLUMN; ++j) {
if (currentBoard[i][j] === 1) {
allDead = false;
break;
}
}
if (!allDead) break;
}
if (allDead) {
clearInterval(intervalId);
document.getElementById("playbtn").innerText = "Play"
console.log("All cells are dead. Stopping the game.");
isPlaying = false;
}
}, 1000/9);
} else {
document.getElementById("playbtn").innerText = "Play"
clearInterval(intervalId);
isPlaying = false;
}
}
renderCanvas(currentBoard);