Code viewer for Mind: ca318 Mind
// a mind can pick one of the following..

// mind must return one of these values..
//const ACTION_LEFT                       = 0;               
//const ACTION_RIGHT                      = 1;
//const ACTION_UP                         = 2;             
//const ACTION_DOWN                       = 3;
//const ACTION_STAYSTILL          = 4;


// algorithm of a World
// var x = world.getState(); -- 
// var a = mind.getAction(x);
// world.takeAction(a); -- returns the value to world and moves the agent accordingly


function randomItem(a,b,c){
    var items = [a,b,c];
    return items[Math.floor(Math.random()* items.length)];
}


function Mind(){
    
    // to start
    

    this.newRun = function()
        {
        };

    this.endRun = function()
        {
        };

    
  this.getAction = function ( x )	    // x is list of ai,aj,ei,ej, world tells us agent and enemy position on sq  	 
  { 
	    // agent position x and y on grid - 
	    var ai = x[0];
	    var aj = x[1];
	    
	    // enemy variables x and y and on grid - 
	    
	    var ei = x[2];
	    var ej = x[3];
	    
	    // when stuck at wall and trying to get away from enemy you must return one of the values 0,1,2,3 randomly, depending on the 
	    // suggested actions given by world (x)
	    
	    // j is aligned with (away from you - towards you) - UP, DOWN (2,3)
	    // i is aligned with left, right (0,1)
        //up -> j++ , down -> i--
        //right -> i++ , left --

	    if ( ej < aj ){
	      // move in the direction of up on j, right or left on i
	      return randomItem(0,1,2); } 
	    
		if ( ej > aj ){
		  // move down on j or right or left on i 
		  return randomItem(0,1,3);
		 
		}	
		if ( ei < ai ){
		    //move right, up or down
		    return randomItem(1,2,3);
		}
		
		if ( ei > ai ) 	{
		  //move left, up or down
		  return randomItem(0,2,3);
		}
		
		// if at same position move in any direction
		if ((ei == ej) || (ej == ai)){
		    return  (randomintAtoB (0,3));
		}
	
};
}