// =================================================================================================
// 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()
{
};
function myCondRec(map,x, y){
if (x > 0 && x < gridsize && y > 0 && y < gridsize && map[x][y] !== 'I' && map[x][y] !== 'M' && map[x][y] === 0){
/*console.log("okokokok");*/
return true;
}
return false;
}
function determineWays(map, x, y, value){
/*console.log("det");*/
var list = [[x,y,value]];
var xx,yy;
while (list.length !== 0){
var start = list.shift();
xx = start[0];
yy = start[1];
newvalue = start[2];
var tmp;
if (myCondRec(map,xx - 1,yy) === true){
map[xx - 1][yy] = newvalue;
tmp = [xx - 1 , yy, newvalue + 1];
list.push(tmp);
}
if (myCondRec(map,xx + 1,yy) === true){
map[xx + 1][yy] = newvalue;
tmp = [xx + 1 , yy, newvalue + 1];
list.push(tmp);
}
if (myCondRec(map,xx ,yy - 1) === true){
map[xx][yy - 1] = newvalue;
tmp = [xx, yy - 1, newvalue + 1];
list.push(tmp);
}
if (myCondRec(map,xx ,yy + 1) === true){
map[xx ][yy + 1] = newvalue;
tmp = [xx, yy + 1, newvalue + 1];
list.push(tmp);
}
}
return map;
}
function createDjikstraMap(map,x,y){
var newmap = jQuery.extend(true, [], map);
for (i = 0; i < newmap.length; i++){
for (j= 0; j < newmap[i].length; j++){
if (newmap[i][j] !== 0){
newmap[i][j] = 'M';
}
}
}
newmap[x][y] = 'I';
newmap = determineWays(newmap, x, y, 1);
for (i = 0; i < newmap.length; i++){
for (j= 0; j < newmap[i].length; j++){
if (newmap[i][j] === 'I'){
newmap[i][j] = 0;
}
}
}
return newmap;
}
function selectNextMove(map, bestPos, posAct){
var xBest = bestPos[0];
var yBest = bestPos[1];
var xAct = posAct[0];
var yAct = posAct[1];
var count = map[xBest][yBest];
console.log("count : " + count);
if (count !== undefined && Number.isInteger(count) === true && count > 1){
for (count;count <= 1;count--){
if (map[xBest + 1][yBest] !== 'M' && map[xBest + 1][yBest] == (count - 1)){
xBest = xBest + 1;
yBest = yBest;
count = count - 1;
}
else if (map[xBest - 1][yBest] !== 'M' && map[xBest - 1][yBest] == (count - 1)){
xBest = xBest - 1;
yBest = yBest;
count = count - 1;
}
else if (map[xBest][yBest + 1] !== 'M' && map[xBest][yBest + 1] == (count - 1)){
xBest = xBest;
yBest = yBest + 1;
count = count - 1;
}
else if (map[xBest][yBest - 1] !== 'M' && map[xBest][yBest - 1] == (count - 1)){
xBest = xBest;
yBest = yBest - 1;
count = count - 1;
}
else{
console.log("t con");
}
}
}
return [xBest, yBest];
}
function showMapPerso(map){
var tmp = "";
for (i = 0; i < gridsize; i++){
for(j = 0; j < gridsize; j++){
tmp += map[i][j];
tmp += 't';
}
tmp += 'n';
}
console.log(tmp);
}
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 map = x[4];
djkEnnemy = createDjikstraMap(map, aj,ai);
djkAI = createDjikstraMap(map, ej,ei);
var bestDist = 0;
var bestPosX = 0;
var bestPosY = 0;
for (var i = 0; i < map.length; i++){
for (var j= 0; j < map[i].length; j++){
if (djkAI[i][j] !== undefined && djkAI[i][j] !== 'M' && djkAI[i][j] < djkEnnemy[i][j] && (djkEnnemy[i][j] - djkAI[i][j]) > bestDist){
bestDist = djkEnnemy[i][j] - (djkAI[i][j]);
bestPosX = i;
bestPosY = j;
//bestPos[0] = i;
//bestPos[1] = j;
}
}
}
console.log(bestDist + " " + bestPosX + " " + bestPosY);
/*if (aj < bestPosX)return (ACTION_DOWN);
if (aj > bestPosX)return (ACTION_UP);
if (ai > bestPosY )return(ACTION_RIGHT);
if (ai < bestPosY)return (ACtiON_LEFT);
return(ACTION_STAYSTILL);*/
// if strictly move away, will get stuck at wall, so introduce randomness
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) );
//return(ACTION_STAYSTILL);
//return(3);
};
}