Save board between browser sessions.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Wed, 10 Sep 2014 20:27:57 +0300
changeset 42 716508d5494c
parent 41 359719178176
child 43 8b1577211149
Save board between browser sessions.
2048.html
--- a/2048.html	Wed Sep 10 20:16:06 2014 +0300
+++ b/2048.html	Wed Sep 10 20:27:57 2014 +0300
@@ -398,7 +398,7 @@
       board.putRandom(board.current);
       ui.board.update(board.current);
       ui.score.update(board.current);
-      localStorage.savedBoard = board.current;
+      localStorage.savedBoard = JSON.stringify(board.current);
     }
 
     ////////////////////////////////////////////////////////////////
@@ -587,8 +587,10 @@
     ui.brdEngine = BoardArr2d; // TODO make user selectable
 
     if (localStorage.savedBoard) {
-      board.current = localStorage.savedBoard;
-      ui.board.update(board.current);
+      try {
+        board.current = JSON.parse(localStorage.savedBoard);
+        ui.board.update(board.current);
+      } catch (ex) { }
     } else {
       ui.action.start();
     }