From 484bde716b48d75c0b31a344b8fb2599234af698 Mon Sep 17 00:00:00 2001 From: Geir Okkenhaug Jerstad Date: Tue, 3 Sep 2024 14:31:50 +0200 Subject: [PATCH] minesweeper some progress --- MVC/minesweeper/index.html | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/MVC/minesweeper/index.html b/MVC/minesweeper/index.html index 9757abe..70b59b9 100644 --- a/MVC/minesweeper/index.html +++ b/MVC/minesweeper/index.html @@ -62,7 +62,7 @@ for (var cellCounter = 0; cellCounter < modelRow.cells.lenght; cellCounter++) { var viewCell = viewRow.insertCell(); var modelCell = modelRow.cells[cellCounter]; - if (true) {//(modelCell.isOpen) { + if (modelCell.isOpen) { viewCell.style.backgroundcolor = 'lightcyan'; if (modelCell.isBomb) { viewCell.innerHTML = '💣'; @@ -81,8 +81,6 @@ function init(size) { matrixModel = {}; matrixModel.rows = []; - - for (var rowCounter = 0; rowCounter < size; rowCounter++) { var newRow = {}; newRow.cells = []; @@ -98,8 +96,6 @@ console.log('init ' + `${matrixModel.rows}`); } - - function placeBombs() { for (var bombCount = 0; bombCount < totalNumberOfBombs; bombCount--) { var rowIndex = Math.floor(Math.random() * size); @@ -128,10 +124,29 @@ } } function handleClick(aMouseEvent) { - - // Forandre modellen + var rowIndex = aMouseEvent.srcElement.parentElement.sectionRowIndexM + var columnIndex = aMouseEvent.srcElement.cellIndex; + var modelCell = matrixModel.rows[rowIndex].cells[columnIndex]; + openBlankCells(rowIndex, columnIndex); + modelCell.isOpen = true; showMatrix(); } + function countBomb(row, column){ + if (row < 0 || row >= size || column < 0 || column >= size) return 0; + return matrixModel.rows[row].cells[column].isBomb + } + + function openBlankCells(rowIndex, columnIndex){ + if (rowIndex < 0 || rowIndex >= size || columnIndex < 0 || columnIndex >= size) return; + var modelCell = matrixModel.rows[rowIndex].cells[columnIndex]; + if (!modelCell.isBomb && !modelCell.isOpen && modelCell.bombsNearBy === 0) { + modelCell.isOpen = true; + openBlankCells(rowIndex - 1, columnIndex); + openBlankCells(rowIndex, columnIndex - 1); + openBlankCells(rowIndex, columnIndex + 1); + openBlankCells(rowIndex + 1, columnIndex); + } + }