Iterate by AI until score or max value occur.
--- a/2048.html Thu Sep 18 11:01:03 2014 +0300
+++ b/2048.html Sat Sep 20 00:44:00 2014 +0300
@@ -79,7 +79,7 @@
border: 1px solid tan;
border-radius: 4px;
}
- div.ai > div.option > input.positive, div.settings > div.setting > input {
+ div.ai > div.option > input.positive, div.settings > div.setting > input, div.ai-control input.positive {
text-align: right;
max-width: 4em;
margin-right: 2px;
@@ -162,6 +162,11 @@
<button id="step">Step</button>
<button id="finish">Finish</button>
</div>
+ <div class="ai-control">
+ <button id="until">Continue</button>
+ until <input type="text" class="positive" id="until-score" value="10000"> score
+ and <input type="text" class="positive" id="until-max-value" value="9"> max value
+ </div>
<div class="clearfix"></div>
<div class="move-control">
<table>
@@ -484,14 +489,17 @@
return false;
return true;
}
- ui.game.finishStep = function() {
- board.putRandom(ui.board.position);
- ui.board.turn++;
+ ui.game.refresh = function() {
ui.board.update(ui.board.position);
ui.score.update(ui.board.position, ui.board.turn);
localStorage.savedBoard = JSON.stringify(ui.board.position);
localStorage.savedTurn = ui.board.turn;
}
+ ui.game.finishStep = function() {
+ board.putRandom(ui.board.position);
+ ui.board.turn++;
+ ui.game.refresh();
+ }
////////////////////////////////////////////////////////////////
// Actions.
@@ -613,6 +621,57 @@
}
document.getElementById("finish").addEventListener("click", ui.action.finish, false);
+ ui.action.until = function() {
+ if ( ! ui.ai.current) {
+ ui.game.setMessage('Select AI!');
+ return;
+ }
+ if ( ! ui.game.beginStep())
+ return;
+ var step = 0;
+ var safeBdr = board.create();
+ var tsFrom = new Date().getTime();
+ var scoreLimit = parseInt(document.getElementById("until-score").value);
+ if (!isFinite(scoreLimit) || scoreLimit < 0) {
+ scoreLimit = 1;
+ document.getElementById("until-score").value = scoreLimit;
+ }
+ var maxValLimit = parseInt(document.getElementById("until-max-value").value);
+ if (!isFinite(maxValLimit) || maxValLimit < 0 || maxValLimit > 13) {
+ maxValLimit = 1;
+ document.getElementById("until-max-value").value = maxValLimit;
+ }
+ while (true) {
+ if (board.gameOver(ui.board.position)) {
+ ui.game.setMessage("Game over!");
+ break;
+ }
+ var stat = board.score(ui.board.position);
+ if (stat.score >= scoreLimit && stat.max >= maxValLimit)
+ break;
+ board.copy(ui.board.position, safeBdr);
+ var move = ui.ai.current.analyse(safeBdr);
+ if (typeof move === 'undefined') {
+ ui.game.setMessage("I don't know how to move!");
+ return;
+ }
+ var updated = board.move[move].call(null, ui.board.position);
+ if (updated) {
+ board.putRandom(ui.board.position);
+ } else {
+ ui.game.finishStep();
+ ui.game.setMessage("Wrong move!");
+ return;
+ }
+ step++;
+ }
+ var tsTo = new Date().getTime();
+ ui.game.refresh();
+ ui.score.speed(parseFloat((step*1000.0/(tsTo-tsFrom)).toPrecision(3)), step);
+ ui.ai.current && ui.ai.current.cleanup();
+ }
+ document.getElementById("until").addEventListener("click", ui.action.until, false);
+
////////////////////////////////////////////////////////////////
// Register AIs.