function Mind() {
var exploredPosition;
var deadEnd;
const axis = [
[0, -1], [1, 0], [0, 1], [-1, 0]
];
this.newRun = function()
{
exploredPosition = [];
deadEnd = {};
};
this.endRun = function()
{
};
// x: ai position in x
// y: ai position in x
// env: array of information for each direction (.open = no wall)
this.getAction = function (state)
{
console.log(state, exploredPosition, deadEnd);
for (var i = 0; i < state.env.length; i++) {
if (!state.env[i].open)
continue;
var a = {
x: state.x + axis[i][0],
y: state.y + axis[i][1],
};
if (typeof(deadEnd[a.x.toString() + 'x' + a.y.toString()]) == 'undefined' &&
typeof(exploredPosition.find(function (b) { return a.x == b.x && a.y == b.y; })) == 'undefined') {
exploredPosition.unshift(a);
return a;
}
}
if (exploredPosition.length > 0)
deadEnd[exploredPosition[0].x.toString() + 'x' + exploredPosition[0].y.toString()] = true;
if (exploredPosition.length < 2) { // no exit found, wtf append ?
this.endRun();
return state;
}
exploredPosition.shift();
return exploredPosition[0];
};
}