Moves for 2d array board + test toolkit.
"use strict";
var ai = {};
// Each strategy is a function that except current board position as 2d array and context from
// previous call to share state/precomputed values between calls.
////////////////////////////////////////////////////////////////
// Random AI.
////////////////////////////////////////////////////////////////
ai.random = function(brdEngine) {
this.brdEngine = brdEngine;
}
ai.random.prototype.analyse = function(brd) {
var origBrd = new this.brdEngine(brd);
while (true) {
var rnd = Math.floor(Math.random()*4);
if (origBrd[["canUp", "canDown", "canLeft", "canRight"][rnd]]())
return ["up", "down", "left", "right"][rnd];
}
}
/* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
ai.random.prototype.cleanup = function() { }
////////////////////////////////////////////////////////////////
// 1 level deep on max scores.
////////////////////////////////////////////////////////////////
ai.nextMaxScore = function(brdEngine) {
this.brdEngine = brdEngine;
}
ai.nextMaxScore.prototype.analyse = function(brd) {
var origBrd = new this.brdEngine(brd);
var nextBrd = new this.brdEngine();
var maxScore = -1;
var action;
if (origBrd.up(nextBrd)) {
maxScore = board.score(tmpBrd).score;
action = "up";
}
board.copy(brd, tmpBrd);
if (board.move.left(tmpBrd)) {
var score = board.score(tmpBrd).score;
if (maxScore < score) {
action = "left";
maxScore = score;
}
}
board.copy(brd, tmpBrd);
if (board.move.down(tmpBrd)) {
var score = board.score(tmpBrd).score;
if (maxScore < score) {
action = "down";
maxScore = score;
}
}
board.copy(brd, tmpBrd);
if (board.move.right(tmpBrd)) {
var score = board.score(tmpBrd).score;
if (maxScore < score) {
action = "right";
maxScore = score;
}
}
return action;
}
/* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
ai.nextMaxScore.prototype.cleanup = function() { }
////////////////////////////////////////////////////////////////
// 1 level deep on max value.
////////////////////////////////////////////////////////////////
ai.nextMaxValue = function(brdEngine) {
this.brdEngine = brdEngine;
}
ai.nextMaxValue.prototype.analyse = function(brd) {
var tmpBrd = board.create();
board.copy(brd, tmpBrd);
var maxMax = -1;
var action;
if (board.move.up(tmpBrd)) {
maxMax = board.score(tmpBrd).max;
action = "up";
}
board.copy(brd, tmpBrd);
if (board.move.left(tmpBrd)) {
var max = board.score(tmpBrd).max;
if (maxMax < max) {
action = "left";
maxMax = max;
}
}
board.copy(brd, tmpBrd);
if (board.move.down(tmpBrd)) {
var max = board.score(tmpBrd).max;
if (maxMax < max) {
action = "down";
maxMax = max;
}
}
board.copy(brd, tmpBrd);
if (board.move.right(tmpBrd)) {
var max = board.score(tmpBrd).max;
if (maxMax < max) {
action = "right";
maxMax = max;
}
}
return action;
}
/* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
ai.nextMaxScore.prototype.cleanup = function() { }