--- a/2048.html Wed Sep 10 18:05:26 2014 +0300
+++ b/2048.html Wed Sep 10 19:29:45 2014 +0300
@@ -335,11 +335,49 @@
turnDom.innerHTML = '' + turn;
}
+ ////////////////////////////////////////////////////////////////
+ // Game flow.
+
+ ui.game = {}
+
+ var messageDom = document.getElementById("message-area");
+ ui.game.clearMessage = function() {
+ messageDom.innerHTML = "";
+ }
+ ui.game.setMessage = function(msg) {
+ messageDom.innerHTML = msg;
+ }
+
+ ui.game.checkGameOver = function() {
+ if (board.gameOver(board.current)) {
+ ui.game.setMessage("Game over!");
+ return true;
+ } else {
+ return false;
+ }
+ }
+ ui.game.checkMoveValid = function(move) {
+ var tmpBrd = board.create();
+ board.copy(board.current, tmpBrd);
+ if (!(move in {"left":0,"right":0,"up":0,"down":0})) {
+ ui.game.setMessage("AI can't find move!");
+ return false;
+ }
+ if ( ! board.move[move].call(null, tmpBrd)) {
+ ui.game.setMessage("AI move "+move+" is ivalid!");
+ return false;
+ }
+ return true;
+ }
+
+ ////////////////////////////////////////////////////////////////
+ // Actions.
+
ui.action = {};
ui.action.start = function() {
ui.score.clear();
- ui.message.clear();
+ ui.game.clearMessage();
ui.action.clearSuggestion();
board.current = board.create();
board.putRandom(board.current);
@@ -402,43 +440,13 @@
case 37: ui.action.left(); break;
case 39: ui.action.right(); break;
}
+ event.preventDefault();
return false;
});
document.getElementById("test").addEventListener("click", function() {
board.move.replaceByBoardJS(ui.brdEngine);
});
-
- ui.message = {};
- var messageDom = document.getElementById("message-area");
- ui.message.clear = function() {
- messageDom.innerHTML = "";
- }
- ui.message.set = function(msg) {
- messageDom.innerHTML = msg;
- }
-
- ui.action.checkGameOver = function() {
- if (board.gameOver(board.current)) {
- ui.message.set("Game over!");
- return true;
- } else {
- return false;
- }
- }
- ui.action.checkMoveValid = function(move) {
- var tmpBrd = board.create();
- board.copy(board.current, tmpBrd);
- if (!(move in {"left":0,"right":0,"up":0,"down":0})) {
- ui.message.set("AI can't find move!");
- return false;
- }
- if ( ! board.move[move].call(null, tmpBrd)) {
- ui.message.set("AI move "+move+" is ivalid!");
- return false;
- }
- return true;
- }
ui.action.clearSuggestion = function() {
var btns = document.querySelectorAll('div.move-control button');
@@ -447,28 +455,28 @@
}
ui.action.suggest = function() {
ui.action.clearSuggestion();
- ui.message.clear();
- if (ui.action.checkGameOver())
+ ui.game.clearMessage();
+ if (ui.game.checkGameOver())
return;
var tmpBrd = board.create();
board.copy(board.current, tmpBrd);
var move = ui.ai.current.analyse(tmpBrd);
ui.ai.current.cleanup();
- if ( ! ui.action.checkMoveValid(move))
+ if ( ! ui.game.checkMoveValid(move))
return;
document.getElementById(move).classList.add('suggestion');
}
document.getElementById("suggest").addEventListener("click", ui.action.suggest);
ui.action.step = function() {
- ui.message.clear();
+ ui.game.clearMessage();
ui.action.clearSuggestion();
- if (ui.action.checkGameOver())
+ if (ui.game.checkGameOver())
return;
var tmpBrd = board.create();
board.copy(board.current, tmpBrd);
var move = ui.ai.current.analyse(tmpBrd);
ui.ai.current.cleanup();
- if ( ! ui.action.checkMoveValid(move))
+ if ( ! ui.game.checkMoveValid(move))
return;
board.move[move].call(null, board.current);
board.putRandom(board.current);
@@ -478,7 +486,7 @@
document.getElementById("step").addEventListener("click", ui.action.step);
ui.action.finish = function() {
- ui.message.clear();
+ ui.game.clearMessage();
ui.action.clearSuggestion();
var step = 0;
var tsFrom = new Date().getTime();
@@ -487,7 +495,7 @@
board.copy(board.current, tmpBrd);
var move = ui.ai.current.analyse(tmpBrd);
if (typeof move === 'undefined') {
- ui.message.set("I don't know how to move!");
+ ui.game.setMessage("I don't know how to move!");
return;
}
var updated = board.move[move].call(null, board.current);
@@ -496,7 +504,7 @@
} else {
ui.board.update(board.current);
ui.score.update(board.current);
- ui.message.set("Wrong move!");
+ ui.game.setMessage("Wrong move!");
return;
}
step++;
@@ -505,7 +513,7 @@
ui.board.update(board.current);
ui.score.update(board.current);
ui.score.speed(step*1000.0/(tsTo-tsFrom), step);
- ui.message.set("Game over!");
+ ui.game.setMessage("Game over!");
ui.ai.current && ui.ai.current.cleanup();
}
document.getElementById("finish").addEventListener("click", ui.action.finish);