// =================================================================================================
// Sample Mind for more complex starter WWM World
// =================================================================================================
// World tells us agent position and enemy position
// World does not tell us of existence of walls
// if return invalid move (not empty square) World just ignores it and we miss a turn
function randomPick ( a, b )
{
if ( randomBoolean() )
return a;
else
return b;
}
function Mind() {
//--- public functions / interface / API ----------------------------------------------------------
this.newRun = function()
{
};
this.endRun = function()
{
};
this.getAction = function ( x ) // x is an array of [ ai, aj, pai, paj ]
{
var ai = x[0];
var aj = x[1];
var pai = x[2];
var paj = x[3];
var curDis = Math.round(Math.sqrt((5-ai)*(5-ai)+(5-aj)*(5-aj)));
var preDis = Math.round(Math.sqrt((5-pai)*(5-pai)+(5-paj)*(5-paj)));
var lastMoveGood = curDis < preDis;
var lastAction = ACTION_STAYSTILL;
if(pai < ai) { lastAction = ACTION_RIGHT; }
if(paj < aj) { lastAction = ACTION_UP; }
if(ai < pai) { lastAction = ACTION_LEFT; }
if(aj < paj) { lastAction = ACTION_DOWN; }
if(lastMoveGood) {
//do it again
if(lastAction == ACTION_LEFT) { return (randomPick (ACTION_LEFT, (randomPick(ACTION_UP, ACTION_DOWN)))); }
if(lastAction == ACTION_RIGHT) { return (randomPick (ACTION_RIGHT, (randomPick(ACTION_UP, ACTION_DOWN)))); }
if(lastAction == ACTION_UP) { return (randomPick (ACTION_UP, (randomPick(ACTION_LEFT, ACTION_RIGHT)))); }
if(lastAction == ACTION_DOWN) { return (randomPick (ACTION_DOWN, (randomPick (ACTION_LEFT, ACTION_RIGHT)))); }
else { return (randomPick (ACTION_LEFT, ACTION_UP)); }
}
else {
//go the otherway
if(lastAction == ACTION_LEFT) { return (randomPick (ACTION_RIGHT, (randomPick(ACTION_UP, ACTION_DOWN)))); }
if(lastAction == ACTION_RIGHT) { return (randomPick (ACTION_LEFT, (randomPick(ACTION_UP, ACTION_DOWN)))); }
if(lastAction == ACTION_UP) { return (randomPick (ACTION_DOWN, (randomPick (ACTION_LEFT, ACTION_RIGHT)))); }
if(lastAction == ACTION_DOWN) { return (randomPick (ACTION_UP, (randomPick(ACTION_LEFT, ACTION_RIGHT)))); }
else { return (randomPick (ACTION_LEFT, ACTION_UP)); }
}
return ( randomintAtoB (0,3) );
}
}