Code viewer for Mind: Complex Mind (clone by Rad...

// Cloned by Radwan Duadu on 13 Nov 2020 from Mind "Complex Mind" by Starter user 
// Please leave this clone trail here.
 



// =================================================================================================
// Sample Mind for more complex 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 


 

	AB.mind.getAction = function ( x )		// x is an array of [ ai, aj, ei, ej ]
	{ 
		var ai = x[0];
		var aj = x[1];
		var ei = x[2];
		var ej = x[3];
		
		var tmp = x[4].length;
		var tmp2 = x[4];
		var GRID = new Array(tmp);
		for ( i = 0; i < tmp ; i++ ) {
		    GRID[i] = new Array(tmp);}	
		for(var i =0;i<tmp;i++){
		    for(var j =0;j<tmp;j++){
		        GRID[i][j]=tmp2[i][j];
		    }
		}
		
		var Dis = 0;
		
		//possible future positions of agent
		var pos = new Array(4);
		var IP = GRID[ai+1][aj];var IM = GRID[ai-1][aj];
        var JP = GRID[ai][aj+1];var JM = GRID[ai][aj-1];
        pos[0] = IP;pos[1]=IM;pos[2]=JP;pos[3]=JM;
        
        
        var NextStep;
        for(i =0;i<pos.length;i++){
            
            var neighbors = pos[i].neighbors;
            
            var count =0;
            for (var ss = 0; ss < neighbors.length; ss++) 
                {
                  var neighbor = neighbors[ss];
                  
                  if(neighbor.wall){
                      count++;
                  }
                }
                
            if(count<3 && !pos[i].wall ){
                var tmp = Math.abs(pos[i].i - ei)+ Math.abs(pos[i].j-ej);
                if(tmp>Dis){
                    Dis = tmp;
                    NextStep = pos[i];
                }
            }
        }
        
        var ci = NextStep.i; var cj = NextStep.j;
     
		// if strictly move away, will get stuck at wall, so introduce randomness 

		 if ( cj < aj ) {	return ( ACTION_DOWN); }
		 if ( cj > aj ) {	return ( ACTION_UP); }

		 if ( ci < ai ) {	return (  ACTION_LEFT);} 
		 if ( ci > ai ) {	return (  ACTION_RIGHT); }

	};