Code viewer for Mind: Grid Attempt

// Cloned by Eoghan Murphy on 7 Nov 2018 from Mind "Cloned Simple Mind" by Eoghan Murphy 
// Please leave this clone trail here.
 


// Cloned by Eoghan Murphy on 5 Nov 2018 from Mind "Simple Mind" by Starter user 
// Please leave this clone trail here.
 



// ==== Starter Mind ===============================================================================================
// (c) Ancient Brain Ltd. All rights reserved.
// This code is only for use on the Ancient Brain site.
// This code may be freely copied and edited by anyone on the Ancient Brain site.
// This code may not be copied, re-published or used on any other website.
// To include a run of this code on another website, see the "Embed code" links provided on the Ancient Brain site.
// ==================================================================================================================



// =================================================================================================
// Sample Mind for simple starter World  
// =================================================================================================

// World tells us agent position and enemy position
// World does not tell us of existence of walls
// if return invalid move (not empty square) World just ignores it and we miss a turn 

// 0 -x
// 1 +x
// 2 +y
// 3 -y

 
var grid_width = 20;
var grid_height = 20;
var grid = new Array(grid_height);
var enemyX = 0;
var enemyY = 0;

function Mind() 
{ 


    
	this.newRun = function()                  
	{
	    initialise_grid();

	    
	};


	this.getAction = function ( x )		 
	{ 
	    enemyX = x[2]
	    enemyY = x[3]
	    update_grid();
	    myX = x[0];
	    myY = x[1];

	    
        return(consider_options());
	};

		 
	this.endRun = function()                 
	{
	};
	
	

}


function initialise_grid(){
    var row = new Array(grid_width);
    for (i=0;i<grid_width;i++){
        row[i] = 0
    }
    
    for (i=0; i<grid_height;i++){
        grid[i] = row.slice(0);
    }
    
    for (i = 1; i < grid_height - 1; i++){
        for(j = 1; j < grid_width - 1; j++){
            grid[i][j] = 1;
        }
    }
}

function update_grid(){
    for(var index in grid){
        for(var item_index in grid[index]){
            if(grid[index][item_index] !== 0){
                grid[index][item_index] = value([item_index, index], enemyX,enemyY);
            }
        }
    }
}

function value(coords, eX, eY){
    var val = Math.pow((coords[0] - eX), 2) + Math.pow((coords[1] - eY), 2);
    val =  Math.sqrt(val);
    return val;
}

function consider_options(){
    possible = []
    
    possible.push([grid[myY][myX - 1],0]);
    possible.push([grid[myY][myX + 1],1]);
    possible.push([grid[myY + 1][myX],2]);
    possible.push([grid[myY - 1][myX],3]);
    
    var best = [0,-1];
    
    for(var index in possible){
        if (possible[index][0] > best[0]){
            best = possible[index];
        }
    }
    console.log(possible);
    return best[1];
}