minesweeper some progress

This commit is contained in:
Geir Okkenhaug Jerstad 2024-09-03 14:31:50 +02:00
parent e30ed748ad
commit 484bde716b

View file

@ -62,7 +62,7 @@
for (var cellCounter = 0; cellCounter < modelRow.cells.lenght; cellCounter++) { for (var cellCounter = 0; cellCounter < modelRow.cells.lenght; cellCounter++) {
var viewCell = viewRow.insertCell(); var viewCell = viewRow.insertCell();
var modelCell = modelRow.cells[cellCounter]; var modelCell = modelRow.cells[cellCounter];
if (true) {//(modelCell.isOpen) { if (modelCell.isOpen) {
viewCell.style.backgroundcolor = 'lightcyan'; viewCell.style.backgroundcolor = 'lightcyan';
if (modelCell.isBomb) { if (modelCell.isBomb) {
viewCell.innerHTML = '💣'; viewCell.innerHTML = '💣';
@ -81,8 +81,6 @@
function init(size) { function init(size) {
matrixModel = {}; matrixModel = {};
matrixModel.rows = []; matrixModel.rows = [];
for (var rowCounter = 0; rowCounter < size; rowCounter++) { for (var rowCounter = 0; rowCounter < size; rowCounter++) {
var newRow = {}; var newRow = {};
newRow.cells = []; newRow.cells = [];
@ -98,8 +96,6 @@
console.log('init ' + `${matrixModel.rows}`); console.log('init ' + `${matrixModel.rows}`);
} }
function placeBombs() { function placeBombs() {
for (var bombCount = 0; bombCount < totalNumberOfBombs; bombCount--) { for (var bombCount = 0; bombCount < totalNumberOfBombs; bombCount--) {
var rowIndex = Math.floor(Math.random() * size); var rowIndex = Math.floor(Math.random() * size);
@ -128,10 +124,29 @@
} }
} }
function handleClick(aMouseEvent) { function handleClick(aMouseEvent) {
var rowIndex = aMouseEvent.srcElement.parentElement.sectionRowIndexM
// Forandre modellen var columnIndex = aMouseEvent.srcElement.cellIndex;
var modelCell = matrixModel.rows[rowIndex].cells[columnIndex];
openBlankCells(rowIndex, columnIndex);
modelCell.isOpen = true;
showMatrix(); 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);
}
}
</script> </script>
</body> </body>