class Node
{
constructor()
{
this.children = [];
this.value = 0;
}
getChildren()
{
return this.children;
}
}
function Mind()
{
this.newRun = function()
{
this.dir = ACTION_UP;
this.pposx = -1;
this.pposy = -1;
this.bposx = -1;
this.bposy = -1;
};
this.is_blocked = function(xp, yp, xb, yb)
{
if (this.pposx != xp)
return false;
if (this.pposy != yp)
return false;
if (this.bposx != xb)
return false;
if (this.bposy != yb)
return false;
if (this.dir == ACTION_LEFT)
this.dir = ACTION_RIGHT;
else if (this.dir == ACTION_RIGHT)
this.dir = ACTION_LEFT;
else if (this.dir == ACTION_UP)
this.dir = ACTION_DOWN;
else if (this.dir == ACTION_DOWN)
this.dir = ACTION_UP;
//this.printDir(this.dir);
return true;
};
this.evaluate = function(xp, yp, xb, yb)
{
distance = 21;
move_lateral = true;
distance = (Math.abs(xp - xb) < distance && Math.abs(xp - xb) > 0 ? Math.abs(xp - xb) : distance);
if (Math.abs(yp - yb) < distance && Math.abs(yp - yb) > 0)
{
distance = Math.abs(yp - yb);
move_lateral = false;
}
if (this.is_blocked(xp, yp, xb, yb) === true)
return this.dir;
if (distance <= 6)
{
if (move_lateral)
{
if (xp > xb)
this.dir = ACTION_RIGHT;
else
this.dir = ACTION_LEFT;
}
else
{
if (yp > yb)
this.dir = ACTION_UP;
else
this.dir = ACTION_DOWN;
}
//this.printDir(this.dir);
this.copyPositions([xp, yp, xb, yb]);
return this.dir;
}
return 4;
};
this.printDir = function(dir)
{
if (dir == ACTION_LEFT)
console.log("ACTION_LEFT")
else if (dir == ACTION_RIGHT)
console.log("ACTION_RIGHT")
else if (dir == ACTION_UP)
console.log("ACTION_UP")
else if (dir == ACTION_DOWN)
console.log("ACTION_DOWN")
else
console.log("DO NOTHING")
}
this.copyPositions = function (state)
{
this.pposx = state[0];
this.pposy = state[1];
this.bposx = state[2];
this.bposy = state[3];
};
this.getAction = function ( state )
{
if (this.pposx == -1)
this.copyPositions(state);
return this.evaluate(state[0], state[1], state[2], state[3]);
};
this.endRun = function()
{
};
}