ai.js
author Oleksandr Gavenko <gavenkoa@gmail.com>
Sun, 07 Sep 2014 03:33:33 +0300
changeset 12 a9a44cfc3e08
parent 10 70ece7f758a0
child 14 9b49e710f5a7
permissions -rw-r--r--
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() { }