From 500c867cbb4441b6c8f518d1a00c67aee5756065 Mon Sep 17 00:00:00 2001 From: Vaclav Tvrdik Date: Thu, 11 Jul 2024 15:29:09 +0200 Subject: [PATCH] remove local storage --- ms.html | 1 - ms.js | 82 +++++++++++++-------------------------------------------- 2 files changed, 19 insertions(+), 64 deletions(-) diff --git a/ms.html b/ms.html index a5c488a..8691a17 100644 --- a/ms.html +++ b/ms.html @@ -22,7 +22,6 @@
-
diff --git a/ms.js b/ms.js index 7427883..605bb38 100644 --- a/ms.js +++ b/ms.js @@ -11,12 +11,6 @@ class Minesweeper { constructor(opts = {}) { let loadedData = {}; - //check if a game is saved in localStorage - if (hasLocalStorage && localStorage["minesweeper.data"]) { - loadedData = JSON.parse(localStorage["minesweeper.data"]); - this.loadGame = true; - } - Object.assign( this, { @@ -35,12 +29,7 @@ class Minesweeper { { options: opts }, loadedData); - if (this.loadGame) { - this.load(); - } else { - this.init(); - } - this.save(); + this.init(); } //setup the game grid @@ -87,17 +76,6 @@ class Minesweeper { this.render(); } - //populate the grid from loaded data - need to create cell objects from raw data - load() { - for (let r = 0, r_len = this.grid.length; r < r_len; r++) { - for (let c = 0, c_len = this.grid[r].length; c < c_len; c++) { - this.grid[r][c] = new Cell(this.grid[r][c]); - } - } - - this.render(); - } - //construct the DOM representing the grid render() { const gameContainer = document.getElementById("game_container"); @@ -177,6 +155,7 @@ class Minesweeper { if (cell.isMine) { this.status_msg = "Sorry, you lost!"; this.playing = false; + stopTimer() document.getElementById("game_status").textContent = this.status_msg; document.getElementById("game_status").style.color = "#EE0000"; } else if (!cell.isFlagged && cell.value == 0) { @@ -230,10 +209,10 @@ class Minesweeper { } else { this.status_msg = "Sorry, you lost!"; this.playing = false; + stopTimer() gameStatus.textContent = this.status_msg; gameStatus.style.color = "#EE0000"; } - this.save(); } //debugging function to print the grid to console @@ -248,15 +227,7 @@ class Minesweeper { return result; } - //save the game object to localstorage - save() { - if (!hasLocalStorage) { - return false; - } else { - let data = JSON.stringify(this); - localStorage["minesweeper.data"] = data; - } - }} + } //Cell constructor to represent a cell object in the grid @@ -294,8 +265,12 @@ function newGame(opts = {}) { console.log(game.gridToString()); } -function startTimer() { +function stopTimer() { clearInterval(countdown); +} + +function startTimer() { + stopTimer(); document.getElementById('timer').textContent = 70; countdown = setInterval(updateTimer, 1000); } @@ -308,30 +283,21 @@ function updateTimer() { timerDisplay.textContent = currentTime - 1; // Call your function here every second } else { - clearInterval(countdown); + stopTimer(); alert('Time run out!'); } } +function createNewGame() { + const opts = { + rows: 8, + cols: 8, + mines: 10 }; + + newGame(opts); +} window.onload = function () { - //attack click to new game button - document. - getElementById("new_game_button"). - addEventListener("click", function () { - const opts = { - rows: 8, - cols: 8, - mines: 10 }; - - - if (hasLocalStorage) { - localStorage.clear(); - } - - newGame(opts); - }); - //attach click event to cells - left click to reveal document. getElementById("game_container"). @@ -348,7 +314,6 @@ window.onload = function () { game.movesMade++; document.getElementById("moves_made").textContent = game.movesMade; game.revealCell(cell); - game.save(); } } }); @@ -369,7 +334,6 @@ window.onload = function () { game.movesMade++; document.getElementById("moves_made").textContent = game.movesMade; game.flagCell(cell); - game.save(); } } }); @@ -382,18 +346,10 @@ window.onload = function () { }); //create a game - newGame(); + createNewGame(); }; //global vars var game; var countdown -//check support for local storage: credit - http://diveintohtml5.info/storage.html -const hasLocalStorage = function () { - try { - return "localStorage" in window && window["localStorage"] !== null; - } catch (e) { - return false; - } -}();