remove local storage

This commit is contained in:
Vaclav Tvrdik 2024-07-11 15:29:09 +02:00
parent 1c4fa127a7
commit 500c867cbb
2 changed files with 19 additions and 64 deletions

View File

@ -22,7 +22,6 @@
<div style="margin: 10px 0px;">
<input id="validate_button" type="button" value="Did I win?" />
<input id="new_game_button" type="button" value="Create new game!" />
</div>
</body>
</html>

82
ms.js
View File

@ -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;
}
}();