so i tried to write the Game of Life in html canvas and JavaScript and with the help of many online tutorials i managed to write some code i still believe in. But when I launch the html page in the browser and start the game itself (that is, i was able to pick the starting cells), the site slows down incredibly. I checked how far does the code make it with console.log(...), so I found out it dies somewhere in the main loop. One thing I don't understand is that, when checking the values of some for loop variables, it seems like they are getting over the limit given in the for. Thank you for your help, it is possible I am missing something obvious.
// variables etc.
var pGame = 0;
var sGame = 0;
const sc = 20;
const c = document.getElementById("canvas");
c.addEventListener("mousedown", fillPixel);
const ctx = c.getContext("2d");
ctx.scale(sc, sc);
const columns = c.width / sc;
const rows = c.height / sc;
function createTable() {
return new Array(columns).fill(null)
.map(() => new Array(rows).fill(0));
}
var tableOne = createTable();
var tableTwo = createTable();
//functions
function fillPixel(event) {
if (sGame == 0) {
var x = Math.floor((event.clientX - canvas.offsetLeft - 5) / sc);
var y = Math.floor((event.clientY - canvas.offsetTop - 5) / sc);
if (tableOne[x][y] == 0) {
ctx.fillRect(x, y, 1, 1);
tableOne[x][y] = 1;
console.log("filled x" + x + " y" + y);
}else{
ctx.clearRect(x, y, 1, 1);
tableOne[x][y] = 0;
console.log("cleared x" + x + " y" + y);
}
}
}
function pauseGame() {
if (sGame == 1) {
if (pGame == 0) {
pGame = 1;
document.getElementById("b1").innerHTML = "resume";
}else{
pGame = 0;
document.getElementById("b1").innerHTML = "pause";
startGame();
}
}
}
function resetGame(){
sGame = 0;
pGame = 0;
document.getElementById("b1").innerHTML = "pause";
tableOne = createTable();
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function startGame() {
sGame = 1;
console.log("while");
while (pGame == 0) {
tableOne = createTable();
for (let col = 0; col < tableOne.length; col++){
for (let row = 0; row < tableOne[col].length; row++){
console.log("col" + col + " row" + row);
const cell = tableOne[col][row];
let neighbours = 0;
for (let i = -1; i < 2; i++){
for (let j = -1; j < 2; j++){
if (i == 0 && j == 0) {
continue;
}
const xCell = col + i;
const yCell = row + j;
if (xCell >= 0 && yCell >= 0 && xCell < 70 && yCell < 20) {
neighbours += tableOne[xCell][yCell];
}
}
}
console.log("applying rules");
if (cell == 1 && (neighbours == 2 || neighbours == 3)) {
tableTwo[col][row] = 1;
}else if (cell == 0 && neighbours == 3) {
tableTwo[col][row] = 1;
}
}
}
console.log("drawing");
tableOne = tableTwo.map(arr => [...arr]);
tableTwo = createTable();
for (let k = 0; k < tableOne.length; k++){
for (let l = 0; l < tableOne[k]length; l++){
if (tableOne[k][l] == 1) {
ctx.fillRect(k, l, 1, 1);
}
}
}
}
}
body {
background-color: #F1E19C;
margin: 0;
}
.button {
background-color: #2C786E;
color: #FFFFFF;
border: none;
padding: 10px 20px;
text-align: center;
font-size: 16px;
}
#header {
background-color: #2C786E;
font-family: 'Times New Roman';
padding: 10px 15px;
color: #FFFFFF;
font-size: 20px;
}
#footer {
position: absolute;
bottom: 5px;
left: 0;
width: 100%;
text-align: center;
font-family: 'Roboto';
}
#canvas {
border: 5px solid #813152;
margin-top: 5px;
margin-left: auto;
margin-right: auto;
display: block;
cursor: crosshair
}
#btns {
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="tres.css">
</head>
<body>
<div id="header">
<h1>Game of Life</h1>
</div>
<p>
<canvas id="canvas" width="1400" height="400"></canvas>
</p>
<p id="btns">
<button class="button" onclick="startGame()"> start </button>
<button class="button" id="b1" onclick="pauseGame()"> pause </button>
<button class="button" onclick="resetGame()"> clear </button>
</p>
<div id="footer">
<p>©2020</p>
</div>
<script src="dos.js"></script>
<body/>
</html>