// =================================================================================================
// 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()
{
};
nearest = function(locations, ai,aj) {
var dis;
var min;
var result = [ai, aj]
for(var i = 1; i < locations.length; i++) {
for(var j = 0; j < locations.length; j++) {
if(locations[i][j]) {
dis = Math.sqrt((ai-i)*(ai-i) + (aj-j)*(aj-j));
if (min == undefined || dis < min) {
min = dis;
result[0] = i;
result[1] = j;
}
}
}
}
return result;
}
this.getAction = function ( x ) // x is an array of [ ai, aj, ei, ej ]
{
var ai = x[0];
var aj = x[1];
var ei = x[2];
var ej = x[3];
var stomach = x[4];
var stomach_max_storage = x[5];
food_locations = x[6];
var foodIAndJ = nearest(food_locations, ai, aj);
//if hungry try eat without being killed
if(stomach_max_storage/2 > stomach) {
console.log("is hungryt", stomach);
if (Math.abs(ej - aj) < 5 || Math.abs(ei - ai) < 5) {
if ( ej < aj ) return ( randomPick ( ACTION_UP, randomPick(ACTION_RIGHT,ACTION_LEFT) ));
if ( ej > aj ) return ( randomPick ( ACTION_DOWN, randomPick(ACTION_RIGHT,ACTION_LEFT) ));
if ( ei < ai ) return ( randomPick ( ACTION_RIGHT, randomPick(ACTION_UP,ACTION_DOWN) ));
if ( ei > ai ) return ( randomPick ( ACTION_LEFT, randomPick(ACTION_UP,ACTION_DOWN) ));
} else {
if ( foodIAndJ[1] > aj ) return ( randomPick ( ACTION_UP, randomPick(ACTION_RIGHT,ACTION_LEFT) ));
if ( foodIAndJ[1] < aj ) return ( randomPick ( ACTION_DOWN, randomPick(ACTION_RIGHT,ACTION_LEFT) ));
if ( foodIAndJ[0] > ai ) return ( randomPick ( ACTION_RIGHT, randomPick(ACTION_UP,ACTION_DOWN) ));
if ( foodIAndJ[0] < ai ) return ( randomPick ( ACTION_LEFT, randomPick(ACTION_UP,ACTION_DOWN) ));
}
//if strong hunt the enemy
} else if ((stomach_max_storage*0.75) < stomach) {
console.log("Strongt", stomach);
if ( ej > aj ) return ( randomPick ( ACTION_UP, randomPick(ACTION_RIGHT,ACTION_LEFT) ));
if ( ej < aj ) return ( randomPick ( ACTION_DOWN, randomPick(ACTION_RIGHT,ACTION_LEFT) ));
if ( ei > ai ) return ( randomPick ( ACTION_RIGHT, randomPick(ACTION_UP,ACTION_DOWN) ));
if ( ei < ai ) return ( randomPick ( ACTION_LEFT, randomPick(ACTION_UP,ACTION_DOWN) ));
} else {
//try to surive.
// if strictly move away, will get stuck at wall, so introduce randomness
console.log("surivingt", stomach);
if ( ej < aj ) return ( randomPick ( ACTION_UP, randomPick(ACTION_RIGHT,ACTION_LEFT) ));
if ( ej > aj ) return ( randomPick ( ACTION_DOWN, randomPick(ACTION_RIGHT,ACTION_LEFT) ));
if ( ei < ai ) return ( randomPick ( ACTION_RIGHT, randomPick(ACTION_UP,ACTION_DOWN) ));
if ( ei > ai ) return ( randomPick ( ACTION_LEFT, randomPick(ACTION_UP,ACTION_DOWN) ));
}
return ( randomintAtoB (0,3) );
};
}