Code viewer for Mind: Cloned New Mind

// Cloned by PierreBougon on 18 Nov 2018 from Mind "New Mind" by PierreBougon 
// Please leave this clone trail here.
 
var lastMove = 4
var lastPos = {x : 0, y : 0}

function Mind() 
{ 
	
	this.newRun = function()                  
	{
	};

    function random()
    {
        var d = new Date();
        var seed = d.getTime();
        var x = Math.sin(seed++) * 10000;
        return x - Math.floor(x);
    }

    function getDistance(a, b)
    {
        console.log(a, b, Math.pow(b.x - a.x, 2), Math.pow(b.y - a.y, 2), Math.sqrt(Math.pow(b.x - a.x, 2) - Math.pow(b.y - a.y, 2)))
        return Math.sqrt(Math.abs(Math.pow(b.x - a.x, 2) - Math.pow(b.y - a.y, 2)))
    }

    function choseMovement(mPos, enPos, dist)
    {
        listDist = []
        needNewMove = false
        
        if ((lastPos.x === mPos.x && lastPos.y === mPos.y) || random() < 0.3)
            needNewMove = true

        listDist.push(getDistance({x : mPos.x + 1, y :  mPos.y}, enPos))
        listDist.push(getDistance({x : mPos.x - 1, y :  mPos.y}, enPos))
        listDist.push(getDistance({x : mPos.x, y :  mPos.y + 1}, enPos))
        listDist.push(getDistance({x : mPos.x, y :  mPos.y - 1}, enPos))

        minDist = Math.max(...listDist)

        if (needNewMove === true)
        {
            console.log("Best move doesn't work just try to get out of this stuck postion")
            return Math.floor(random() * 100 % 4)
        }
        if (listDist[0] == minDist)
            return 1
        if (listDist[1] == minDist)
            return 0
        if (listDist[2] == minDist)
            return 2
        if (listDist[3] == minDist)
            return 3
        
        return 4;
    }

	this.getAction = function ( state )		 
	{
	  console.log("Last Posistion = ", lastPos)
	  var mPos = 
	  {
	      x : state[0],
	      y : state[1]
	  }
	  var enPos = 
	  {
	      x : state[2],
	      y : state[3]
	  }
	  
	  dist = getDistance(mPos, enPos)
	  console.log("Distance = ", dist)
	  if (dist < 16.0)
	  {
            move = choseMovement(mPos, enPos, dist)
            lastMove = move
            lastPos = mPos
            console.log("Move = ", move)
            return move
	  }
	  return ( 4 );		 		
	};

		 
	this.endRun = function()                 
	{
	};

}