//based on a turing machine
//does not handle when in a junction you encounter another junciton you do not go back to the first junction
function Mind() {
//--- public functions / interface / API ----------------------------------------------------------
this.newRun = function()
{
};
this.getAction = function ( x ) // x is an array of [ ai, aj, upOccu, leftOccu, rightOccu, downOccu, ei, ej, junction ]
{
ai = x[0];
aj = x[1];
ei = x[6];
ej = x[7];
junction = x[8];
returned = junction.pop();
juncDirection = junction.pop();
rightOccu = x[4];
leftOccu = x[3];
upOccu = x[2];
downOccu = x[5];
juncI = x[9];
juncJ = x[10];
var path = -1;
junction.push(juncDirection);
//if you have returned to the junction follow the appropriate path
if(returned == 'returned')
{
if(juncDirection == 'right')
path = 1;
else if(juncDirection == 'down')
path = 2;
else if(juncDirection == 'left')
path = 3;
else if(juncDirection == 'up')
path = 4;
else if(juncDirection == 'down 1')
path = 5;
else if(juncDirection == 'right 2')
path = 6;
else if(juncDirection == 'right 1')
path = 7;
else if(juncDirection == 'down 2')
path = 8;
else if(juncDirection == 'up 1')
path = 9;
else if(juncDirection == 'left 2')
path = 10;
else if(juncDirection == 'left 1')
path = 11;
else if(juncDirection == 'up 2')
path = 12;
}
//end state go left
if(path == 1)
{
return 0;
}
//end state go up
else if(path == 2)
{
return 2;
}
//end state go right
else if(path == 3)
{
return 1;
}
//end state go down
else if(path == 4)
{
return 3;
}
//change state
else if(path == 5)
{
return 7;
}
//end state go up
else if(path == 6)
{
return 2;
}
//change state
else if(path == 7)
{
return 9;
}
//end state go left
else if(path == 8)
{
return 0;
}
//change state
else if(path == 9)
{
return 11;
}
//end state go down
else if(path == 10)
{
return 3;
}
//change state
else if(path == 11)
{
return 15;
}
//end state go right
else if(path == 12)
{
return 1;
}
//returned to junction
else if(juncI == ai && juncJ == aj)
{
console.log('returned to junction');
return 5;
}
//3 way junction coming from left
else if(upOccu == false && rightOccu == false && downOccu == false && aj + 1 != ej && ai + 1 != ei && aj - 1 != ej)
{
console.log('junction down');
return 6;
}
//3 way junction coming from down
else if(upOccu == false && rightOccu == false && leftOccu == false && aj + 1 != ej && ai + 1 != ei && ai - 1 != ei)
{
console.log('junction right');
return 8;
}
//3 way junction coming from right
else if(upOccu == false && downOccu == false && leftOccu == false && aj + 1 != ej && aj - 1 != ej && ai - 1 != ei)
{
console.log('junction up');
return 10;
}
//3 way junction coming from up
else if(rightOccu == false && downOccu == false && leftOccu == false && ai + 1 != ei && aj - 1 != ej && ai - 1 != ei)
{
console.log('junction left');
return 14;
}
//2 way junction coming from up/down
else if(rightOccu == false && leftOccu == false && ai + 1 != ei && ai - 1 != ei)
{
console.log('junction right');
return 12;
}
//2 way junction coming from right/left
else if(downOccu == false && upOccu == false && aj + 1 != ej && aj - 1 != ej)
{
return 13;
}
//go right
else if(rightOccu == false && ai + 1 != ei)
{
console.log('right');
return 1;
}
//go up
else if(upOccu == false && aj + 1 != ej)
{
console.log('up');
return 2;
}
//go down
else if(downOccu == false && aj - 1 != ej)
{
console.log('down');
return 3;
}
//go left
else if(leftOccu == false && ai - 1 != ei)
{
console.log('left');
return 0;
}
//deadend go back
else
{
console.log('go back');
return 4;
}
};
this.endRun = function()
{
};
}