const direction = [[-1, 0], [1, 0], [0, 1], [0, -1], [0, 0]];
class Node {
constructor(state, move, player) {
this.player = player;
this.state = state;
this.move = move;
this.value = Math.abs(state[0] - state[2]) + Math.abs(state[1] - state[3]);
this.child = [];
}
addDepth(map) {
var x;
var y;
var c;
var i = 0;
if (this.child.length) {
for (c = 0; c < this.child.length; ++c) {
this.child[c].addDepth(map);
}
}
else
{
if (this.player) {
for (i = 0; i < 5; ++i) {
x = this.state[0] + direction[i][0];
y = this.state[1] + direction[i][1];
if (!map[y][x]) {
this.child.push(new Node([x, y, this.state[2], this.state[3]], i, !this.player));
}
}
}
else {
for (i = 0; i < 5; ++i) {
x = this.state[2] + direction[i][0];
y = this.state[3] + direction[i][1];
if (!map[y][x]) {
this.child.push(new Node([this.state[0], this.state[1], x, y], i, !this.player));
}
}
}
}
}
getBest() {
if (!this.child.length || this.value === 0) {
return [this.value, this.move];
}
var cur;
var c;
var tmp;
if (this.player) {
cur = [0, 0];
for (c = 0; c < this.child.length; ++c) {
tmp = this.child[c].getBest();
if (tmp[0] > cur[0]) {
cur = [tmp[0], this.child[c].move];
}
}
}
else
{
cur = [100, 0];
for (c = 0; c < this.child.length; ++c) {
tmp = this.child[c].getBest();
if (tmp[0] < cur[0]) {
cur = [tmp[0], this.child[c].move];
}
}
}
return cur;
}
}
function Mind()
{
this.newRun = function()
{
this.map = [];
for (var i = 0; i < 20; ++i) {
this.map.push([]);
for (var j = 0; j < 20; ++j) {
if (i === 0 || i === 19 || j === 0 || j === 19) {
this.map[i].push(1);
}
else {
this.map[i].push(0);
}
}
}
this.dir = 0;
this.last = [0, 0];
};
this.setWall = function(state) {
if (state[0] != this.last[0] || state[1] != this.last[1]) {
return false;
}
x = state[0] + direction[this.dir][0];
y = state[1] + direction[this.dir][1];
if (x == state[3] || y == state[4]) {
return false;
}
this.map[y][x] = 1;
return true;
};
this.moveTree = function(state) {
var i;
var reset = true;
for (i = 0; i < this.tree.child.length; ++i) {
if (this.tree.child[i].state[0] == state[0] && this.tree.child[i].state[1] == state[1]) {
this.tree = this.tree.child[i];
reset = false;
break;
}
}
if (reset) {
return false;
}
reset = true;
for (i = 0; i < this.tree.child.length; ++i) {
if (this.tree.child[i].state[2] == state[2] && this.tree.child[i].state[3] == state[3]) {
this.tree = this.tree.child[i];
reset = false;
break;
}
}
return !reset;
};
this.printMap = function() {
for (var i = 0; i < this.map.length; ++i) {
console.log(i, this.map[i].join(' '));
}
};
this.reset = function( state ) {
this.tree = new Node(state, 4, true);
this.tree.addDepth(this.map);
this.tree.addDepth(this.map);
this.tree.addDepth(this.map);
this.tree.addDepth(this.map);
this.tree.addDepth(this.map);
this.tree.addDepth(this.map);
this.tree.addDepth(this.map);
};
this.getAction = function ( state )
{
if (this.last[0] === 0) {
this.reset(state);
this.last = [state[0], state[1]];
return (1);
}
if (!this.moveTree(state)) {
this.reset(state);
}
if (this.setWall(state)) {
this.reset(state);
}
else
{
this.tree.addDepth(this.map);
this.tree.addDepth(this.map);
}
this.last = [state[0], state[1]];
this.dir = this.tree.getBest()[1];
return ( this.dir );
};
this.endRun = function()
{
};
}