--- a/2048.html Wed Sep 10 19:46:51 2014 +0300
+++ b/2048.html Wed Sep 10 20:16:06 2014 +0300
@@ -314,6 +314,9 @@
}
ui.board.enableEdit();
+ ////////////////////////////////////////////////////////////////
+ // Score statistic.
+
ui.score = {};
var scoreDom = document.getElementById("score");
var maxDom = document.getElementById("max");
@@ -336,6 +339,19 @@
}
////////////////////////////////////////////////////////////////
+ // Suggestion UI.
+
+ ui.suggestion = {};
+ ui.suggestion.btns = document.querySelectorAll('div.move-control button');
+ ui.suggestion.clear = function() {
+ for (var i = 0; i < ui.suggestion.btns.length; i++)
+ ui.suggestion.btns[i].classList.remove('suggestion');
+ }
+ ui.suggestion.set = function(dir) {
+ document.getElementById(dir).classList.add('suggestion');
+ }
+
+ ////////////////////////////////////////////////////////////////
// Game flow.
ui.game = {};
@@ -371,6 +387,20 @@
return true;
}
+ ui.game.beginStep = function() {
+ ui.suggestion.clear();
+ ui.game.clearMessage();
+ if (ui.game.checkGameOver())
+ return false;
+ return true;
+ }
+ ui.game.finishStep = function() {
+ board.putRandom(board.current);
+ ui.board.update(board.current);
+ ui.score.update(board.current);
+ localStorage.savedBoard = board.current;
+ }
+
////////////////////////////////////////////////////////////////
// Actions.
@@ -378,8 +408,8 @@
ui.action.start = function() {
ui.score.clear();
+ ui.suggestion.clear();
ui.game.clearMessage();
- ui.action.clearSuggestion();
board.current = board.create();
board.putRandom(board.current);
ui.board.update(board.current);
@@ -392,12 +422,10 @@
return function() {
if (ui.game.checkGameOver())
return;
- ui.action.clearSuggestion();
+ ui.suggestion.clear();
var updated = board.move[dir](board.current);
if (updated) {
- board.putRandom(board.current);
- ui.board.update(board.current);
- ui.score.update(board.current);
+ ui.game.finishStep();
ui.ai.current && ui.ai.current.cleanup();
}
}
@@ -423,15 +451,12 @@
board.move.replaceByBoardJS(ui.brdEngine);
});
- ui.action.clearSuggestion = function() {
- var btns = document.querySelectorAll('div.move-control button');
- for (var i = 0; i < btns.length; i++)
- btns[i].classList.remove('suggestion');
- }
ui.action.suggest = function() {
- ui.action.clearSuggestion();
- ui.game.clearMessage();
- if (ui.game.checkGameOver())
+ if ( ! ui.ai.current) {
+ ui.game.setMessage('Select AI!');
+ return;
+ }
+ if ( ! ui.game.beginStep())
return;
var tmpBrd = board.create();
board.copy(board.current, tmpBrd);
@@ -439,13 +464,15 @@
ui.ai.current.cleanup();
if ( ! ui.game.checkMoveValid(move))
return;
- document.getElementById(move).classList.add('suggestion');
+ ui.suggestion.set(move);
}
document.getElementById("suggest").addEventListener("click", ui.action.suggest);
ui.action.step = function() {
- ui.game.clearMessage();
- ui.action.clearSuggestion();
- if (ui.game.checkGameOver())
+ if ( ! ui.ai.current) {
+ ui.game.setMessage('Select AI!');
+ return;
+ }
+ if ( ! ui.game.beginStep())
return;
var tmpBrd = board.create();
board.copy(board.current, tmpBrd);
@@ -454,15 +481,16 @@
if ( ! ui.game.checkMoveValid(move))
return;
board.move[move].call(null, board.current);
- board.putRandom(board.current);
- ui.board.update(board.current);
- ui.score.update(board.current);
+ ui.game.finishStep();
}
document.getElementById("step").addEventListener("click", ui.action.step);
ui.action.finish = function() {
- ui.game.clearMessage();
- ui.action.clearSuggestion();
+ if ( ! ui.ai.current) {
+ ui.game.setMessage('Select AI!');
+ return;
+ }
+ ui.game.beginStep();
var step = 0;
var tsFrom = new Date().getTime();
while (!board.gameOver(board.current)) {
@@ -477,16 +505,14 @@
if (updated) {
board.putRandom(board.current);
} else {
- ui.board.update(board.current);
- ui.score.update(board.current);
+ ui.game.finishStep();
ui.game.setMessage("Wrong move!");
return;
}
step++;
}
var tsTo = new Date().getTime();
- ui.board.update(board.current);
- ui.score.update(board.current);
+ ui.game.finishStep();
ui.score.speed(step*1000.0/(tsTo-tsFrom), step);
ui.game.setMessage("Game over!");
ui.ai.current && ui.ai.current.cleanup();
@@ -560,7 +586,12 @@
ui.brdEngine = BoardArr2d; // TODO make user selectable
- ui.action.start();
+ if (localStorage.savedBoard) {
+ board.current = localStorage.savedBoard;
+ ui.board.update(board.current);
+ } else {
+ ui.action.start();
+ }
</script>