diff -r f1bbe12e2c3d -r 16b711a81a16 2048.html --- a/2048.html Tue Sep 09 20:33:17 2014 +0300 +++ b/2048.html Tue Sep 09 21:18:31 2014 +0300 @@ -37,6 +37,9 @@ div.move-control, div.score-conrol { padding: 3px; } + div.move-control button.suggestion { + background-color: cyan; + } div.ai-control { padding: 2px 2px 2px 1em; } @@ -301,6 +304,7 @@ ui.action.start = function() { ui.score.clear(); ui.message.clear(); + ui.action.clearSuggestion(); board.current = board.create(); board.putRandom(board.current); ui.board.update(board.current); @@ -308,6 +312,7 @@ document.getElementById("start").addEventListener("click", ui.action.start); ui.action.up = function() { + ui.action.clearSuggestion(); var updated = board.move.up(board.current); if (updated) { board.putRandom(board.current); @@ -318,6 +323,7 @@ } document.getElementById("up").addEventListener("click", ui.action.up); ui.action.down = function() { + ui.action.clearSuggestion(); var updated = board.move.down(board.current); if (updated) { board.putRandom(board.current); @@ -328,6 +334,7 @@ } document.getElementById("down").addEventListener("click", ui.action.down); ui.action.left = function() { + ui.action.clearSuggestion(); var updated = board.move.left(board.current); if (updated) { board.putRandom(board.current); @@ -338,6 +345,7 @@ } document.getElementById("left").addEventListener("click", ui.action.left); ui.action.right = function() { + ui.action.clearSuggestion(); var updated = board.move.right(board.current); if (updated) { board.putRandom(board.current); @@ -372,32 +380,68 @@ messageDom.innerHTML = msg; } - function step() { - ui.message.clear(); + 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'); + for (var i = 0; i < btns.length; i++) + btns[i].classList.remove('suggestion'); + } + ui.action.suggest = function() { + ui.action.clearSuggestion(); + ui.message.clear(); + if (ui.action.checkGameOver()) return; - } var tmpBrd = board.create(); 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.ai.current.cleanup(); + if ( ! ui.action.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.action.clearSuggestion(); + if (ui.action.checkGameOver()) return; - } - var updated = board.move[move].call(null, board.current); - if (updated) { - board.putRandom(board.current); - ui.board.update(board.current); - ui.score.update(board.current); - } else { - ui.message.set("Wrong move!"); - } + 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)) + return; + board.move[move].call(null, board.current); + board.putRandom(board.current); + ui.board.update(board.current); + ui.score.update(board.current); } - document.getElementById("step").addEventListener("click", step); + document.getElementById("step").addEventListener("click", ui.action.step); ui.action.finish = function() { ui.message.clear(); + ui.action.clearSuggestion(); var step = 0; var tsFrom = new Date().getTime(); while (!board.gameOver(board.current)) {