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

// Cloned by Rakesh Reddy Soma on 23 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 neighbors=[];
        //console.log("neighbors",neighbors);
		var path1=0;
		var path2=0;
		var path3=0;
		var count=0;
		var count1=0;
		

		// if strictly move away, will get stuck at wall, so introduce randomness 
			 

		 if ( ej < aj ) 
		 {
		     //(GRID[ei][ej+1]);
		     if(!occupied(ei,ej+1))
		     {
		     var path1=(Math.abs(ei - ai) + Math.abs((ej+1) - aj));
		     //console.log("path1:",path1)
		     neighbors.push(path1)
		         
		     }
		     
		     //(GRID[ei-1][ej]);
		      if(!occupied(ei-1,ej))
		     {
		     var path2=(Math.abs((ei-1) - ai) + Math.abs(ej - aj));
		     //console.log("path2:",path2)
		     neighbors.push(path2)
		     }
		     
		     //(GRID[ei+1][ej]);
		     if(!occupied(ei+1,ej))
		     {
		     var path3=(Math.abs((ei+1) - ai) + Math.abs(ej - aj));
		     //console.log("path3:",path3)
		     neighbors.push(path3)
		     }
		     
		     var min = Math.min.apply(Math, neighbors)

		     //console.log("The neighbor values",neighbors);
		     //console.log("The min value",min);
		     if(min==path1)
		     {
		         if(!occupied(ei,ej+1))
		         {
		         count=count+1;
		         ei=ei;
		         ej=ej+1;
		       //return (GRID[ei][ej+1]);  
		     }
		     }
		     if(min==path2 && count==0)
		     {
		         if(!occupied(ei-1,ej))
		         {
		         count1=count1+1;
		         ei=ei-1;
		         ej=ej;
		        //return (GRID[ei-1][ej]);
		     }
		     }
		     if(min==path3 && count1==0 && count==0)
		     {
		         if(!occupied(ei+1,ej))
		         {
		         ei=ei+1;
		         ej=ej;
		       //return (GRID[ei+1][ej]);  
		     }
		     }
		     
		     
		     if((GRID[ei][ej+1]==GRID_MAZE || GRID[ei][ej+1]==GRID_WALL ) && ( GRID[ei-1][ej]==GRID_MAZE || GRID[ei-1][ej]==GRID_WALL) && (GRID[ei+1][ej]==GRID_MAZE ||GRID[ei+1][ej]==GRID_WALL))
		     
		     {
		         ei=ei;
		         ej=ej-1;
		     }
		     
		 
		 //return ( AB.randomPick ( ACTION_UP,AB.randomPick(ACTION_RIGHT,ACTION_LEFT) 	));
		     
		 }
		 if ( ej > aj ) {
		     
		     //(GRID[ei][ej-1]);
		     if(!occupied(ei,ej-1))
		     {
		     var path1=(Math.abs(ei - ai) + Math.abs((ej-1) - aj));
		     //console.log("path1:",path1)
		     neighbors.push(path1)
		     }
		     
		    // (GRID[ei+1][ej]);
		    if(!occupied(ei+1,ej))
		    {
		     var path2=(Math.abs((ei+1) - ai) + Math.abs(ej - aj));
		     //console.log("path1:",path2)
		     neighbors.push(path2)
		    }
		    
		     //(GRID[ei-1][ej]);
		     if(!occupied(ei-1,ej))
		     {
		     var path3=(Math.abs((ei-1) - ai) + Math.abs(ej - aj));
		     //console.log("path1:",path3)
		     neighbors.push(path3)
		     }
		     
		     var min = Math.min.apply(Math, neighbors)

		     //console.log("The neighbor values",neighbors);
		     //console.log("The min value",min);
		     if(min==path1)
		     {
		         if(!occupied(ei,ej-1))
		         {
		         count=count+1;
		         ei=ei;
		         ej=ej-1;
		       //return (GRID[ei][ej-1]);  
		     }
		     }
		     if(min==path2 && count==0)
		     {
		          if(!occupied(ei+1,ej))
		          {
		         count1=count1+1;
		         ei=ei+1;
		         ej=ej;
		        //return (GRID[ei+1][ej]);
		     }
		     }
		     if(min==path3 && count==0 && count1==0)
		     {
		         if(!occupied(ei-1,ej))
		         {
		       ei=ei-1;
		       ej=ej;
		       //return (GRID[ei-1][ej]);  
		     }
		     }
		       if((GRID[ei][ej-1]==GRID_MAZE || GRID[ei][ej-1]==GRID_WALL ) && ( GRID[ei-1][ej]==GRID_MAZE || GRID[ei-1][ej]==GRID_WALL) && (GRID[ei+1][ej]==GRID_MAZE ||GRID[ei+1][ej]==GRID_WALL))
		     
		     {
		         ei=ei;
		         ej=ej+1;
		     }
		     
		 //return ( AB.randomPick ( ACTION_DOWN,	AB.randomPick(ACTION_RIGHT,ACTION_LEFT) 	)); 
            }
		 if ( ei < ai ) 
		 {
		     //(GRID[ei+1][ej]);
		     if(!occupied(ei+1,ej))
		     {
		     var path1=(Math.abs((ei+1) - ai) + Math.abs(ej - aj));
		     //console.log("path1:",path1)
		     neighbors.push(path1)
		     }
		     //(GRID[ei][ej-1]);
		     if(!occupied(ei,ej-1))
		     {
		     var path2=(Math.abs(ei - ai) + Math.abs((ej-1) - aj));
		     //console.log("path1:",path2)
		     neighbors.push(path2)
		     }
		     
		     //(GRID[ei][ej+1]);
		     if(!occupied(ei,ej+1))
		     {
		     var path3=(Math.abs(ei - ai) + Math.abs((ej+1) - aj));
		     //console.log("path1:",path3)
		     neighbors.push(path3)
		     }
		     
		     var min = Math.min.apply(Math, neighbors)

		     //console.log("The neighbor values",neighbors);
		     //console.log("The min value",min);
		     if(min==path1)
		     {
		         if(!occupied(ei+1,ej))
		         {
		         count=count+1;
		       ei=ei+1;
		       ej=ej;
		       //return (GRID[ei+1][ej]);  
		     }
		     }
		     if(min==path2 && count==0)
		     {
		         if(!occupied(ei,ej-1))
		         {
		         count1=count1+1;
		         ei=ei;
		         ej=ej-1;
		        //return (GRID[ei][ej-1]);
		     }
		     }
		     if(min==path3 && count1==0 && count==0)
		     {
		      if(!occupied(ei,ej+1))
		      {
		         ei=ei;
		         ej=ej+1;
		       //return (GRID[ei][ej+1]);  
		     }
		     }
		       if((GRID[ei][ej+1]==GRID_MAZE || GRID[ei][ej+1]==GRID_WALL ) && ( GRID[ei][ej-1]==GRID_MAZE || GRID[ei][ej-1]==GRID_WALL) && (GRID[ei+1][ej]==GRID_MAZE ||GRID[ei+1][ej]==GRID_WALL))
		     
		     {
		         ei=ei-1;
		         ej=ej;
		     }
		     
		     
		     
		     
		 //return ( AB.randomPick ( ACTION_RIGHT,	AB.randomPick(ACTION_UP,ACTION_DOWN) 		)); 
		 }
		 if ( ei > ai ) 
		 {
		     //(GRID[ei-1][ej]);
		     if(!occupied(ei-1,ej))
		     {
		     var path1=(Math.abs((ei-1) - ai) + Math.abs(ej - aj));
		     //console.log("path1:",path1)
		     neighbors.push(path1)
		     }
		     //(GRID[ei][ej+1]);
		     if(!occupied(ei,ej+1))
		     {
		     var path2=(Math.abs(ei - ai) + Math.abs((ej+1) - aj));
		     //console.log("path1:",path2)
		     neighbors.push(path2)
		     }
		     
		     //(GRID[ei][ej-1]);
		     if(!occupied(ei,ej-1))
		     {
		     var path3=(Math.abs(ei - ai) + Math.abs((ej-1) - aj));
		     //console.log("path1:",path3)
		     neighbors.push(path3)
		     }
		      var min = Math.min.apply(Math, neighbors)

		     //console.log("The neighbor values",neighbors);
		     //console.log("The min value",min);
		     if(min==path1)
		     {
		         if(!occupied(ei-1,ej))
		         {
		         count=count+1;
		         ei=ei-1;
		         ej=ej;
		       //return (GRID[ei-1][ej]);  
		     }
		     }
		     if(min==path2 && count==0)
		     {
		          if(!occupied(ei,ej+1))
		          {
		         count1=count1+1;
		         ei=ei;
		         ej=ej+1;
		        //return (GRID[ei][ej+1]);
		     }
		     }
		     if(min==path3 && count==0 && count1==0)
		     {
		      if(!occupied(ei,ej-1))
		      {
		          
		         
		         ei=ei;
		         ej=ej-1;
		       //return (GRID[ei][ej-1]);   
		     }
		     }
		      if((GRID[ei][ej+1]==GRID_MAZE || GRID[ei][ej+1]==GRID_WALL ) && ( GRID[ei][ej-1]==GRID_MAZE || GRID[ei][ej-1]==GRID_WALL) && (GRID[ei-1][ej]==GRID_MAZE ||GRID[ei-1][ej]==GRID_WALL))
		     
		     {
		         ei=ei+1;
		         ej=ej;
		     }
		     
		// return ( AB.randomPick ( ACTION_LEFT,	AB.randomPick(ACTION_UP,ACTION_DOWN) 		)); 
		 }
		 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////		 
	// The agen move away from the enemy;
	
	
		    
		 
		 
		 

 		//return  ( AB.randomIntAtoB (0,3) );
	};