From 54b38fbb042ca97d10f6804674940279b69f2645 Mon Sep 17 00:00:00 2001 From: synth-ruiner Date: Wed, 18 Mar 2015 12:24:11 +0000 Subject: [PATCH] Saving/loading functionality Also removed an unused variable --- index.html | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 62824f4..49d8aa0 100644 --- a/index.html +++ b/index.html @@ -285,7 +285,6 @@ var gameBoardHeight = 9; var score = 0; var firstClick = true; - var currentlyIterating = false; var mineChance = 0.2; var clickholdMs = 200; var timeout; //hold timer @@ -328,6 +327,37 @@ return row; } + function saveGame() { + if (typeof(Storage) == "undefined") { + return; + } + + //variables + localStorage.setItem("score", score); + localStorage.setItem("mineChance", mineChance); + localStorage.setItem("firstClick", firstClick); + + //game board + localStorage.setItem("gameBoard", $('#game').html()); + } + + function loadGame() { + //check if storage available and if saved game exists + if (typeof(Storage) == "undefined" || localStorage.getItem("gameBoard") == null) { + drawGameBoard(); + return; + } + + //variables + score = parseInt(localStorage.getItem("score")); + mineChance = parseFloat(localStorage.getItem("mineChance")); + firstClick = (localStorage.getItem("firstClick") == "true"); + + //game board + $('#game').html(localStorage.getItem("gameBoard")); + $('#setup').hide(); + } + $.fn.check = function() { //unclicked tiles if ($(this).filter('li:not(.revealed):not(.flagged)').length > 0) { @@ -748,6 +778,7 @@ updateScore(); updateMinesLeft(); + saveGame(); }); $(window).resize(function() { @@ -757,7 +788,7 @@ resizeToWindow(); //instantiate the game - drawGameBoard(); + loadGame(); //loadGame will draw the game board if no saved game is found $('#gameOver').hide(); $('button.cancel').hide();