Code viewer for Mind: Mind Blown
const GRID_SIZE = 20;

var oldposition = [];
var goodMoves = [];
var mappedMaze = [];

var currentposition = null;
var prevposition = null;

function getMoves(position) 
{
    var ai = position[0];
    var aj = position[1];

    return [{ x: ai-1, y: aj }, { x: ai+1, y: aj }, { x: ai, y: aj+1 }, { x: ai, y: aj-1 }, { x: ai, y: aj }];
}

function whereAmI(position) 
{
    var ai = position[0];
    var aj = position[1];
    return{
        x: ai,
        y: aj
    };
}

function whereAreThey(position) 
{
    var ei = position[2];
    var ej = position[3];
    return{
        x: ei,
        y: ej
    };
}

function updateMap() 
{
    // check if we were able to move
    if (goodMoves.length < 1) return;

    var lastMove = goodMoves[goodMoves.length - 1];
    var currentPos = whereAmI(currentposition);
    var prevPos = whereAmI(prevposition);
    var moveBlocked = currentPos.x === prevPos.x && currentPos.y === prevPos.y && lastMove !== ACTION_STAYSTILL;
    if (!moveBlocked) return;

    enemySquare = whereAreThey(prevposition);

    if (lastMove === ACTION_UP) 
    { 
        // did I move up
        if (enemySquare.y === (currentPos.y + 1) && enemySquare.x === currentPos.x) return;
        // enemy not blocking square, update map
        mappedMaze[currentPos.y + 1][currentPos.x] = 1;
    } 
    else if (lastMove === ACTION_DOWN) 
    { 
        // did I move down
        if (enemySquare.y === currentPos.y - 1 && enemySquare.x === currentPos.x) return;
        // enemy not blocking square, update map
        mappedMaze[currentPos.y - 1][currentPos.x] = 1;
    } 
    else if (lastMove === ACTION_LEFT) 
    { 
        // did I move left
        if (enemySquare.y === currentPos.y && enemySquare.x === currentPos.x - 1) return;
        // enemy not blocking square, update map
        mappedMaze[currentPos.y][currentPos.x - 1] = 1;
    } 
    else if (lastMove === ACTION_RIGHT) 
    { 
        // did I move up
        if (enemySquare.y === currentPos.y && enemySquare.x === currentPos.x + 1) return;
        // enemy not blocking square, update map
        mappedMaze[currentPos.y][currentPos.x + 1] = 1;
    }
}



function getDistance(pointA, pointB) 
{
    if (pointA === null || pointB === null) return -1;
    return Math.sqrt(Math.pow(pointB.x - pointA.x, 2) + Math.pow(pointB.y - pointA.y, 2));
}

function getMove(position) 
{
    var potentialMoves = getMoves(position);
    potentialMoves = potentialMoves.map(function checkIfMoveBlocked(move) 
    {
        if (mappedMaze[move.y][move.x] === 1) return null;
        return move;
    })
    var enemySquare = whereAreThey(position);

    var distances = potentialMoves.map(function (pos) 
    {
        return getDistance(pos, enemySquare);
    });

    // console.log('moves', potentialMoves, distances);

    var bestMove = 4;
    var largestDistance = 0;
    for (var i = 0; i <= 3; i++) 
    {
        var distance = distances[i];
        if (distance > largestDistance) 
        {
            largestDistance = distance;
            bestMove = i;
        }
    }

    return bestMove;
}

function Mind() {

    this.newRun = function () 
    {
        // init mappedMaze
        // 0 for free square, 1 for blocked
        for (i = 0; i < GRID_SIZE; i++) 
        {
            mappedMaze.push([]);
            for (j = 0; j < GRID_SIZE; j++) 
            {
                if (i === 0 || i === 19 || j === 0 || j === 19) mappedMaze[i].push(1); // wall
                else mappedMaze[i].push(0);
            }
        }
    };

    this.getAction = function (position) 
    {   
        // position: [ai, aj, ei, ej]
        prevposition = currentposition;
        currentposition = position;
        oldposition.push(position);

        updateMap();

        var move = getMove(position);
        // console.log(move, mapLajout);
        goodMoves.push(move);
        return (move);
    };


    this.endRun = function () {};

}