remove local storage
This commit is contained in:
parent
1c4fa127a7
commit
500c867cbb
1
ms.html
1
ms.html
|
|
@ -22,7 +22,6 @@
|
||||||
|
|
||||||
<div style="margin: 10px 0px;">
|
<div style="margin: 10px 0px;">
|
||||||
<input id="validate_button" type="button" value="Did I win?" />
|
<input id="validate_button" type="button" value="Did I win?" />
|
||||||
<input id="new_game_button" type="button" value="Create new game!" />
|
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
68
ms.js
68
ms.js
|
|
@ -11,12 +11,6 @@ class Minesweeper {
|
||||||
constructor(opts = {}) {
|
constructor(opts = {}) {
|
||||||
let loadedData = {};
|
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(
|
Object.assign(
|
||||||
this,
|
this,
|
||||||
{
|
{
|
||||||
|
|
@ -35,13 +29,8 @@ class Minesweeper {
|
||||||
{ options: opts },
|
{ options: opts },
|
||||||
loadedData);
|
loadedData);
|
||||||
|
|
||||||
if (this.loadGame) {
|
|
||||||
this.load();
|
|
||||||
} else {
|
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
this.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
//setup the game grid
|
//setup the game grid
|
||||||
init() {
|
init() {
|
||||||
|
|
@ -87,17 +76,6 @@ class Minesweeper {
|
||||||
this.render();
|
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
|
//construct the DOM representing the grid
|
||||||
render() {
|
render() {
|
||||||
const gameContainer = document.getElementById("game_container");
|
const gameContainer = document.getElementById("game_container");
|
||||||
|
|
@ -177,6 +155,7 @@ class Minesweeper {
|
||||||
if (cell.isMine) {
|
if (cell.isMine) {
|
||||||
this.status_msg = "Sorry, you lost!";
|
this.status_msg = "Sorry, you lost!";
|
||||||
this.playing = false;
|
this.playing = false;
|
||||||
|
stopTimer()
|
||||||
document.getElementById("game_status").textContent = this.status_msg;
|
document.getElementById("game_status").textContent = this.status_msg;
|
||||||
document.getElementById("game_status").style.color = "#EE0000";
|
document.getElementById("game_status").style.color = "#EE0000";
|
||||||
} else if (!cell.isFlagged && cell.value == 0) {
|
} else if (!cell.isFlagged && cell.value == 0) {
|
||||||
|
|
@ -230,10 +209,10 @@ class Minesweeper {
|
||||||
} else {
|
} else {
|
||||||
this.status_msg = "Sorry, you lost!";
|
this.status_msg = "Sorry, you lost!";
|
||||||
this.playing = false;
|
this.playing = false;
|
||||||
|
stopTimer()
|
||||||
gameStatus.textContent = this.status_msg;
|
gameStatus.textContent = this.status_msg;
|
||||||
gameStatus.style.color = "#EE0000";
|
gameStatus.style.color = "#EE0000";
|
||||||
}
|
}
|
||||||
this.save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//debugging function to print the grid to console
|
//debugging function to print the grid to console
|
||||||
|
|
@ -248,15 +227,7 @@ class Minesweeper {
|
||||||
return result;
|
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
|
//Cell constructor to represent a cell object in the grid
|
||||||
|
|
@ -294,8 +265,12 @@ function newGame(opts = {}) {
|
||||||
console.log(game.gridToString());
|
console.log(game.gridToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
function startTimer() {
|
function stopTimer() {
|
||||||
clearInterval(countdown);
|
clearInterval(countdown);
|
||||||
|
}
|
||||||
|
|
||||||
|
function startTimer() {
|
||||||
|
stopTimer();
|
||||||
document.getElementById('timer').textContent = 70;
|
document.getElementById('timer').textContent = 70;
|
||||||
countdown = setInterval(updateTimer, 1000);
|
countdown = setInterval(updateTimer, 1000);
|
||||||
}
|
}
|
||||||
|
|
@ -308,30 +283,21 @@ function updateTimer() {
|
||||||
timerDisplay.textContent = currentTime - 1;
|
timerDisplay.textContent = currentTime - 1;
|
||||||
// Call your function here every second
|
// Call your function here every second
|
||||||
} else {
|
} else {
|
||||||
clearInterval(countdown);
|
stopTimer();
|
||||||
alert('Time run out!');
|
alert('Time run out!');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createNewGame() {
|
||||||
window.onload = function () {
|
|
||||||
//attack click to new game button
|
|
||||||
document.
|
|
||||||
getElementById("new_game_button").
|
|
||||||
addEventListener("click", function () {
|
|
||||||
const opts = {
|
const opts = {
|
||||||
rows: 8,
|
rows: 8,
|
||||||
cols: 8,
|
cols: 8,
|
||||||
mines: 10 };
|
mines: 10 };
|
||||||
|
|
||||||
|
|
||||||
if (hasLocalStorage) {
|
|
||||||
localStorage.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
newGame(opts);
|
newGame(opts);
|
||||||
});
|
}
|
||||||
|
|
||||||
|
window.onload = function () {
|
||||||
//attach click event to cells - left click to reveal
|
//attach click event to cells - left click to reveal
|
||||||
document.
|
document.
|
||||||
getElementById("game_container").
|
getElementById("game_container").
|
||||||
|
|
@ -348,7 +314,6 @@ window.onload = function () {
|
||||||
game.movesMade++;
|
game.movesMade++;
|
||||||
document.getElementById("moves_made").textContent = game.movesMade;
|
document.getElementById("moves_made").textContent = game.movesMade;
|
||||||
game.revealCell(cell);
|
game.revealCell(cell);
|
||||||
game.save();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -369,7 +334,6 @@ window.onload = function () {
|
||||||
game.movesMade++;
|
game.movesMade++;
|
||||||
document.getElementById("moves_made").textContent = game.movesMade;
|
document.getElementById("moves_made").textContent = game.movesMade;
|
||||||
game.flagCell(cell);
|
game.flagCell(cell);
|
||||||
game.save();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -382,18 +346,10 @@ window.onload = function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
//create a game
|
//create a game
|
||||||
newGame();
|
createNewGame();
|
||||||
};
|
};
|
||||||
|
|
||||||
//global vars
|
//global vars
|
||||||
var game;
|
var game;
|
||||||
var countdown
|
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;
|
|
||||||
}
|
|
||||||
}();
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue