Move rule and AI engine to separate files.
<!DOCTYPE html>
<html>
<head>
<title>2048 AI</title>
<meta name="viewport" content="width=device-width; initial-scale=1.0"/>
<meta charset="utf-8"/>
<script src="rule.js"></script>
<script src="board.js"></script>
<script src="perf.js"></script>
<script src="ai.js"></script>
<style>
body {
width: 100%;
}
h1, div.area {
text-align: center;
margin: 10px auto;
}
#board {
margin: 10px auto;
}
#board td {
width: 40px;
height: 40px;
border: 1px solid red;
margin: 0;
text-align: center;
}
.report > .name {
font-weight: bold;
}
</style>
</head>
<body>
<h1>2048</h1>
<div id="score-area" class="area">Score: <span id="score">0</span>, Max: <span id="max">0</span>, Speed: <span id="speed">0</span> t/s, Turn: <span id="turn">0</span></div>
<div id="message-area" class="area"></div>
<table id="board">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<div id="control-area" class="area">
<div>
<button id="start">Start</button>
<button id="step">Step</button>
<button id="loop">Loop</button>
<button id="finish">Finish</button>
</div>
<div>
<button id="left">left</button>
<button id="up">up</button>
<button id="down">down</button>
<button id="right">right</button>
</div>
<h1>AI</h1>
<div>
<button id="ai-random">random</button>
<button id="ai-next-max-score">next max score</button>
<button id="ai-next-max-value">next max value</button>
</div>
</div>
<div id="report-area" class="area">
<h1>Reports</h1>
<div class="report" class="area">
<div class="name">next max score</div>
<table>
<tr>
<th>Max</th>
<th>Count</th>
<th>%</th>
</tr>
<tr>
<td>128</td><td>8</td><td><div style="background: red; width: 100px">x</div></td>
</tr>
<tr>
<td>256</td><td>4</td><td><div style="background: red; width: 50px">x</div></td>
</tr>
<tr>
<td>512</td><td>12</td><td><div style="background: red; width: 150px">x</div></td>
</tr>
</table>
</div>
</div>
<script>
"use strict";
var boardDom = document.getElementById("board");
var ui = {};
ui.board = {};
ui.board.set = function(i, j, val) {
boardDom.querySelectorAll("tr")[i].querySelectorAll("td")[j].innerHTML = val;
}
ui.board.update = function(brd) {
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
ui.board.set(i, j, (brd[i][j] >= 1) ? brd[i][j] : "");
}
}
}
ui.score = {};
var scoreDom = document.getElementById("score");
var maxDom = document.getElementById("max");
var speedDom = document.getElementById("speed");
var turnDom = document.getElementById("turn");
ui.score.clear = function(brd) {
scoreDom.innerHTML = '0';
maxDom.innerHTML = '0';
speedDom.innerHTML = '0';
turnDom.innerHTML = '0';
}
ui.score.update = function(brd) {
var score = board.score(brd);
scoreDom.innerHTML = '' + score.score;
maxDom.innerHTML = '' + score.max;
}
ui.score.speed = function(speed, turn) {
speedDom.innerHTML = '' + speed;
turnDom.innerHTML = '' + turn;
}
function start() {
ui.score.clear();
ui.message.clear();
board.current = board.create();
board.putRandom(board.current);
ui.board.update(board.current);
}
document.getElementById("start").addEventListener("click", start);
function up() {
var updated = board.move.up(board.current);
if (updated) {
board.putRandom(board.current);
ui.board.update(board.current);
ui.score.update(board.current);
}
}
document.getElementById("up").addEventListener("click", up);
function down() {
var updated = board.move.down(board.current);
if (updated) {
board.putRandom(board.current);
ui.board.update(board.current);
ui.score.update(board.current);
}
}
document.getElementById("down").addEventListener("click", down);
function left() {
var updated = board.move.left(board.current);
if (updated) {
board.putRandom(board.current);
ui.board.update(board.current);
ui.score.update(board.current);
}
}
document.getElementById("left").addEventListener("click", left);
function right() {
var updated = board.move.right(board.current);
if (updated) {
board.putRandom(board.current);
ui.board.update(board.current);
ui.score.update(board.current);
}
}
document.getElementById("right").addEventListener("click", right);
document.body.addEventListener("keydown", function(event) {
var key = event.keyCode || event.which;
switch (key) {
case 38: up(); break;
case 40: down(); break;
case 37: left(); break;
case 39: right(); break;
}
return false;
});
ui.message = {};
var messageDom = document.getElementById("message-area");
ui.message.clear = function() {
messageDom.innerHTML = "";
}
ui.message.set = function(msg) {
messageDom.innerHTML = msg;
}
function step() {
ui.message.clear();
if (board.gameOver(board.current)) {
ui.message.set("Game over!");
return;
}
var tmpBrd = board.create();
board.copy(board.current, tmpBrd);
var move = ui.ai.analyse(tmpBrd);
if (typeof move === 'undefined') {
ui.message.set("I don't know how to move!");
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!");
}
}
document.getElementById("step").addEventListener("click", step);
function finish() {
ui.message.clear();
var step = 0;
var tsFrom = new Date().getTime();
while (!board.gameOver(board.current)) {
var tmpBrd = board.create();
board.copy(board.current, tmpBrd);
var move = ui.ai.analyse(tmpBrd);
if (typeof move === 'undefined') {
ui.message.set("I don't know how to move!");
return;
}
var updated = board.move[move].call(null, board.current);
if (updated) {
board.putRandom(board.current);
} else {
ui.board.update(board.current);
ui.score.update(board.current);
ui.message.set("Wrong move!");
return;
}
step++;
}
var tsTo = new Date().getTime();
ui.board.update(board.current);
ui.score.update(board.current);
ui.score.speed(step*1000.0/(tsTo-tsFrom), step);
ui.message.set("Game over!");
}
document.getElementById("finish").addEventListener("click", finish);
////////////////////////////////////////////////////////////////
// Register AIs.
ui.brdEngine = BoardArr2d; // TODO make user selectable
document.getElementById("ai-random").addEventListener("click", function() {
ui.ai = new ai.random(ui.brdEngine);
});
document.getElementById("ai-next-max-score").addEventListener("click", function() {
ui.ai = new ai.nextMaxScore(ui.brdEngine);
});
document.getElementById("ai-next-max-value").addEventListener("click", function() {
ui.ai = new ai.nextMaxValue(ui.brdEngine);
});
</script>
</body>
</html>