Skip to content

Commit

Permalink
fix the bug when rows decrease
Browse files Browse the repository at this point in the history
  • Loading branch information
modouaicha023 committed Nov 18, 2023
1 parent c3ecf99 commit cdbd7d3
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function updateCellClasses() {

function createCellsElements(rows, cols, allCells) {
gridContainer.innerHTML = "";
// allCells = [];
allCells.length = 0;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const cell = document.createElement('div');
Expand All @@ -52,14 +52,14 @@ function newRound(r, l) {
gridContainer.style.gridTemplateRows = `repeat(${inputRows.value}, 20px)`;
gridContainer.style.gridTemplateColumns = `repeat(${inputColunms.value}, 20px)`;

// set alive some cell
function setRandomAliveCell(Nrows, Ncolumns) {
for (let index = 0; index < Nrows * Ncolumns; index++) {
const randomRow = Math.floor(Math.random() * Nrows);
const randomCol = Math.floor(Math.random() * Ncolumns);
cellsMatrix[randomRow][randomCol] = 1;
}
}
// set alive some cell
setRandomAliveCell(inputRows.value, inputColunms.value);
updateCellClasses();

Expand Down Expand Up @@ -131,32 +131,27 @@ function newRound(r, l) {
console.log("Game Stopped");
}
function resetGame() {
stopGame();
cellsMatrix = initMatrix(10, 10);
updateCellClasses()

// window.location.reload();
window.location.reload();
}

btnPlay.addEventListener("click", startGame);
btnStop.addEventListener("click", stopGame);
btnReset.addEventListener("click", resetGame);
}



inputRows.addEventListener("change", () => {
function updateRowsCols() {
clearInterval(intervalId);
intervalId = null;
newRound(inputRows.value, inputColunms.value);

if (inputRows.value > 0 && inputColunms.value > 0)
newRound(inputRows.value, inputColunms.value);
}

inputRows.addEventListener("change", () => {
updateRowsCols();
});
inputColunms.addEventListener("change", () => {
clearInterval(intervalId);
intervalId = null;
newRound(inputRows.value, inputColunms.value);

inputColunms.addEventListener("change", () => {
updateRowsCols()
});


Expand Down

0 comments on commit cdbd7d3

Please sign in to comment.