Code viewer for Mind: CA318 Grid Q

// 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 movepattern = [[-1,0],[1,0],[0,1],[0,-1]]
var grid_width = 20;
var grid_height = 20;
var grid = new Array(grid_height);
var enemyX = 0;
var enemyY = 0;

var lastPlannedTurn = [0,0]
var lastLocation = [0,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];
	    
	    if(compare_coords(lastLocation, [myX,myY])){
	        grid[lastPlannedTurn[1]][lastPlannedTurn[0]] = 0;
	    }
	    
	    

	    
        var next = consider_options();

        lastPlannedTurn = calculate_next_space(next);
        lastLocation = [myX, myY];
        return(next);
	};

		 
	this.endRun = function()                 
	{
	    console.log(grid)
	};
	
	

}


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 i = 1; i < grid.length - 1; i++){
        for (var j = 1; j < grid[i].length - 1; j++){
            if(grid[i][j] !== 0){
                grid[i][j] = value([j, i], enemyX,enemyY);
            }
            
            var mult = consider_around_space(j,i);
            grid[i][j] = grid[i][j] * mult;
        }
    }
    
    /*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);
            }
            
            
            
            
            //maybe delete this bit
            var multiplier = 1;
            if(index > 0 && grid[index - 1][item_index] === 0){
                multiplier -= 0.25;
            }
            //if(index < grid.length - 1 && grid[index + 1][item_index] === 0){
            //    multiplier -= 0.25;
            //}
            if(item_index > 0 && grid[index][item_index - 1] === 0){
                multiplier -= 0.25;
            }
            if(item_index < (grid_width - 1) && grid[index][item_index + 1] === 0){
                multiplier -= 0.25;
            }
            grid[index][item_index] = (grid[index][item_index]) * multiplier;

        }
    }*/
}

function value(coords, eX, eY){
    var val = Math.pow((coords[0] - eX), 2) + Math.pow((coords[1] - eY), 2);
    val =  Math.sqrt(val);
    if(val === 0){
    return -1;
    }
    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];
}

function consider_around_space(x,y){
    var surrounding = [];
    
    surrounding.push(grid[x-1][y]);
    surrounding.push(grid[x+1][y]);
    surrounding.push(grid[x][y-1]);
    surrounding.push(grid[x[y+1]]);
    
    var multiplier = 1;
    
    for(var index in surrounding){
        if(surrounding[index] === 0){
            multiplier -= 0.25;
        }
    }
    if (multiplier <= 0){
        multiplier = 0.0001;
    }
    return multiplier;
}

function calculate_next_space(n){
    var tobeadded = movepattern[n]
    return [myX + tobeadded[0], myY + tobeadded[1]]
}

function compare_coords(coord1,coord2){
    return ((coord1[0] == coord2[0]) && (coord1[1] == coord2[1]));
}