Code viewer for Mind: Complex Mind mine

// Cloned by Mohamed Hafez on 13 Nov 2020 from Mind "Complex Mind (clone by Mohamed Hafez)" by Mohamed Hafez 
// Please leave this clone trail here.
 


// Cloned by Mohamed Hafez on 8 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 agentalgorithm2= x[4];
		var m1 = x[5];
		var m2=  x[6];
		var m3=  x[7];
		var m4=  x[8];
		var gridsize =x[9];

        var openSet = [];
        var closedSet = [];
        var start;
        var end2;
        var GRIDMIND=x[10];
        var heuristicdistance =[];
        var heuristicx =[];
        var heuristicy =[];
        var max2=[];
        var reach=0;
        var si;
        var sj;
        var Q;


        function heuristic(a, b) 
        {

        return ( Math.sqrt(Math.pow((a.i - b.i),2) + Math.pow((a.j - b.j),2)) );
    
        }

        function removeFromArray(arr, elt) 
        {
    // Could use indexOf here instead to be more efficient
  for (var i = arr.length - 1; i >= 0; i--) 
    if (arr[i] == elt) 
      arr.splice(i, 1);
      
      function occupied ( i, j )		// is this square occupied
{
 if ( ( ei == i ) && ( ej == j ) ) return true;		// variable objects 
 if ( ( ai == i ) && ( aj == j ) ) return true;

 if ( GRIDMIND[i][j].value == 1 ) return true;		// fixed objects //[MH] Adding .value since we are dealing with object now	 
 if ( GRIDMIND[i][j].value == 2 ) return true;		//[MH] Adding .value since we are dealing with object now
	 
 return false;
}
      
}


        if (agentalgorithm2==1)
        {
		// Random Algorithm

        
		 if ( ej < aj ) 	return ( AB.randomPick ( ACTION_UP,		AB.randomPick(ACTION_RIGHT,ACTION_LEFT) 	)); 
		 if ( ej > aj ) 	return ( AB.randomPick ( ACTION_DOWN,	AB.randomPick(ACTION_RIGHT,ACTION_LEFT) 	)); 

		 if ( ei < ai ) 	return ( AB.randomPick ( ACTION_RIGHT,	AB.randomPick(ACTION_UP,ACTION_DOWN) 		)); 
		 if ( ei > ai ) 	return ( AB.randomPick ( ACTION_LEFT,	AB.randomPick(ACTION_UP,ACTION_DOWN) 		)); 

 		return  ( AB.randomIntAtoB (0,3) );
 		
        }
 		
 		if (agentalgorithm2==3)
 		{
 	//	Run away with Heuristic and stucking avoidance
 		
 		var bfsm=[];
 	
 	

    bfsm[0]=Math.sqrt(Math.pow(ai-ei,2 )  + Math.pow(aj+1-ej,2)) ;
    bfsm[1]=Math.sqrt(Math.pow(ai+1-ei,2 )  + Math.pow(aj-ej,2)) ;
    bfsm[2]=Math.sqrt(Math.pow(ai-ei,2 )  + Math.pow(aj-1-ej,2)) ;
    bfsm[3]=Math.sqrt(Math.pow(ai-1-ei,2 )  + Math.pow(aj-ej,2)) ;
    
    if(m1>0) bfsm[2]=0;
    if(m2>0) bfsm[1]=0;
    if(m3>0) bfsm[3]=0;
    if(m4>0) bfsm[0]=0;
    
    var max = Math.max.apply(null, bfsm);
        for (var f = 0; f < bfsm.length; f++ )
        {
        if (bfsm[f]==max)
        {
        max2.push(f);
        }
        }
 		
    return AB.randomElementOfArray ( max2 );
 		}
 		
 		if (agentalgorithm2==2)
 		{
 	openSet=[];
    closedSet=[];
    start = GRIDMIND[ai][aj];
/*     for ( i = 0; i < gridsize ; i++ )
{
    for ( j = 0; j < gridsize ; j++ )
    {  
     if (GRID[i][j].value===0 )
     {
     console.log('hello');
     heuristicdistance.push(heuristic(GRID[i][j],GRID [ei][ej]));
     heuristicx.push(i);
     heuristicy.push(j);
     }
    }
}
    

    var max = Math.max.apply(null,heuristicdistance);
    for (var f = 0; f < heuristicdistance.length; f++ )
        {
        if (heuristicdistance[f]==max)
        {
        max2.push(f);
        }
        }
 		
    var index= AB.randomElementOfArray ( max2 );

    end= GRIDMIND[heuristicx[index]][heuristicy[index]];
    //end = GRIDMIND[20][1];
  
  */
  
  function quarter(i,j)
  {
  if ( (i>=0) && (i<=gridsize/3) && (j>=0) && (j<=gridsize/3 )) return 1; 
  if ( (i>=gridsize/3) && (i<=2*gridsize/3) && (j>=0) && (j<gridsize/3 )) return 2;
  if ( (i>=2*gridsize/3) && (i<=gridsize) && (j>=0) && (j<=gridsize/3 )) return 3;
  if ( (i>=0) && (i<gridsize/3) && (j>=gridsize/3) && (j<=2*gridsize/3 )) return 4;
  if ( (i>=gridsize/3) && (i<=2*gridsize/3) && (j>=gridsize/3) && (j<=2*gridsize/3 )) return 5;
  if ( (i>2*gridsize/3) && (i<=gridsize) && (j>=gridsize/3) && (j<=2*gridsize/3 )) return 6;
  if ( (i>=0) && (i<=gridsize/3) && (j>=2*gridsize/3) && (j<=gridsize)) return 7;
  if ( (i>=gridsize/3) && (i<=2*gridsize/3) && (j>2*gridsize/3) && (j<=gridsize)) return 8;
  if ( (i>=2*gridsize/3) && (i<=gridsize) && (j>=2*gridsize/3) && (j<=gridsize)) return 9;
  else return Q=10;
  }
var Q1=[0,gridsize/3,0,gridsize/3];
var Q2=[gridsize/3,2*gridsize/3,0,gridsize/3];
var Q3=[2*gridsize/3,gridsize,0,gridsize/3];
var Q4=[0,gridsize/3,gridsize/3,2*gridsize/3];
var Q5=[gridsize/3,2*gridsize/3,gridsize/3,2*gridsize/3];
var Q6=[2*gridsize/3,gridsize,gridsize/3,2*gridsize/3];
var Q7=[0,gridsize/3,2*gridsize/3,gridsize];
var Q8=[gridsize/3,2*gridsize/3,2*gridsize/3,gridsize];
var Q9=[2*gridsize/3,gridsize,2*gridsize/3,gridsize];


while (1<2)
{
var Q11=AB.randomIntAtoB (Q1[0],Q1[1] );
var Q12=AB.randomIntAtoB (Q1[2],Q1[3] );
if (!occupied(Q11,Q12)) break; 
}

while (1<2)
{
var Q21=AB.randomIntAtoB (Q2[0],Q2[1] );
var Q22=AB.randomIntAtoB (Q2[2],Q2[3] );
if (!occupied(Q21,Q22)) break; 
}

while(1<2)
{
var Q31=AB.randomIntAtoB (Q3[0],Q3[1] );
var Q32=AB.randomIntAtoB (Q3[2],Q3[3] );
if (!occupied(Q31,Q32)) break; 
}

while(1<2)
{
var Q41=AB.randomIntAtoB (Q4[0],Q4[1] );
var Q42=AB.randomIntAtoB (Q4[2],Q4[3] );
if (!occupied(Q41,Q42)) break; 
}

while(1<2)
{
var Q51=AB.randomIntAtoB (Q5[0],Q5[1] );
var Q52=AB.randomIntAtoB (Q5[2],Q5[3] );
if (!occupied(Q51,Q52)) break; 
}

while(1<2)
{
var Q61=AB.randomIntAtoB (Q6[0],Q6[1] );
var Q62=AB.randomIntAtoB (Q6[2],Q6[3] );
if (!occupied(Q61,Q62)) break; 
}

while(1<2)
{
var Q71=AB.randomIntAtoB (Q7[0],Q7[1] );
var Q72=AB.randomIntAtoB (Q7[2],Q7[3] );
if (!occupied(Q71,Q72)) break; 
}

while(1<2)
{
var Q81=AB.randomIntAtoB (Q8[0],Q8[1] );
var Q82=AB.randomIntAtoB (Q8[2],Q8[3] );
if (!occupied(Q81,Q82)) break; 
}

while(1<2)
{
var Q91=AB.randomIntAtoB (Q9[0],Q9[1] );
var Q92=AB.randomIntAtoB (Q9[2],Q9[3] );
if (!occupied(Q91,Q92)) break; 
}

console.log(Q11,Q12,Q21,Q22,Q31,Q32,Q41,Q42,Q51,Q52,Q61,Q62,Q71,Q72,Q81,Q82,Q91,Q92);
console.log(GRIDMIND);
 /* 
  if ( (ei>0) && (ei<gridsize/3) && (ej>0) && (ej<gridsize/3 )) Q=1; 
  if ( (ei>gridsize/3) && (ei<2*gridsize/3) && (ej>0) && (ej<gridsize/3 )) Q=2;
  if ( (ei>2*gridsize/3) && (ei<gridsize) && (ej>0) && (ej<gridsize/3 )) Q=3;
  if ( (ei>0) && (ei<gridsize/3) && (ej>gridsize/3) && (ej<2*gridsize/3 )) Q=4;
  if ( (ei>gridsize/3) && (ei<2*gridsize/3) && (ej>gridsize/3) && (ej<2*gridsize/3 )) Q=5;
  if ( (ei>2*gridsize/3) && (ei<gridsize) && (ej>gridsize/3) && (ej<2*gridsize/3 )) Q=6;
  if ( (ei>0) && (ei<gridsize/3) && (ej>2*gridsize/3) && (ej<gridsize)) Q=7;
  if ( (ei>gridsize/3) && (ei<2*gridsize/3) && (ej>2*gridsize/3) && (ej<gridsize)) Q=8;
  if ( (ei>2*gridsize/3) && (ei<gridsize) && (ej>2*gridsize/3) && (ej<gridsize)) return Q=9;
*/
if (quarter(ei,ej)==1) end2=GRIDMIND[Q91][Q92];
if (quarter(ei,ej)==3) end2=GRIDMIND[Q71][Q72];
if (quarter(ei,ej)==7) end2=GRIDMIND[Q31][Q32];
if (quarter(ei,ej)==9) end2=GRIDMIND[Q11][Q12];

if (quarter(ei,ej)==2)
{
   if (quarter(ai,aj)==1 || quarter(ai,aj)==4 || quarter(ai,aj)==7) end2=GRIDMIND[Q71][Q72]; 
   if (quarter(ai,aj)==3 || quarter(ai,aj)==6 || quarter(ai,aj)==9) end2=GRIDMIND[Q91][Q92];
   if (quarter(ai,aj)==2 || quarter(ai,aj)==5 || quarter(ai,aj)==8) end2=GRIDMIND[Q71][Q72]; 
}
    
if (quarter(ei,ej)==4)
{
   if (quarter(ai,aj)==1 || quarter(ai,aj)==2 || quarter(ai,aj)==3) end2=GRIDMIND[Q31][Q32];
   if (quarter(ai,aj)==7 || quarter(ai,aj)==8 || quarter(ai,aj)==9) end2=GRIDMIND[Q91][Q92];
   if (quarter(ai,aj)==4 || quarter(ai,aj)==5 || quarter(ai,aj)==6) end2=GRIDMIND[Q31][Q32];

}
 
 
if (quarter(ei,ej)==6)
{
   if (quarter(ai,aj)==1 || quarter(ai,aj)==2 || quarter(ai,aj)==3) end2=GRIDMIND[Q11][Q12];
   if (quarter(ai,aj)==7 || quarter(ai,aj)==8 || quarter(ai,aj)==9) end2=GRIDMIND[Q71][Q72]; 
   if (quarter(ai,aj)==4 || quarter(ai,aj)==5 || quarter(ai,aj)==6) end2=GRIDMIND[Q11][Q12];

} 



if (quarter(ei,ej)==8)
{
   if (quarter(ai,aj)==1 || quarter(ai,aj)==4 || quarter(ai,aj)==7) end2=GRIDMIND[Q11][Q12];
   if (quarter(ai,aj)==3 || quarter(ai,aj)==6 || quarter(ai,aj)==9) end2=GRIDMIND[Q31][Q32];
   if (quarter(ai,aj)==2 || quarter(ai,aj)==5 || quarter(ai,aj)==8) end2=GRIDMIND[Q11][Q12];

}
    
if (quarter(ei,ej)==5)
{
      if (quarter(ai,aj)==1) end2=GRIDMIND[Q21][Q22];
      if (quarter(ai,aj)==2) end2=GRIDMIND[Q31][Q32];
      if (quarter(ai,aj)==3) end2=GRIDMIND[Q61][Q62];
      if (quarter(ai,aj)==4) end2=GRIDMIND[Q71][Q72];
      if (quarter(ai,aj)==6) end2=GRIDMIND[Q91][Q92];
      if (quarter(ai,aj)==7) end2=GRIDMIND[Q81][Q82];
      if (quarter(ai,aj)==8) end2=GRIDMIND[Q91][Q92];
      if (quarter(ai,aj)==9) end2=GRIDMIND[Q61][Q62];
      if (quarter(ai,aj)==5) end2=GRIDMIND[Q91][Q92];

    
}
    console.log(ai,aj,ei,ej);
    console.log(end2);
    openSet.push(start);


for ( i = 0; i < gridsize ; i++ )
{
    for ( j = 0; j < gridsize ; j++ )
    {
      GRIDMIND[i][j].g=0;
	  GRIDMIND[i][j].f=0;
	  GRIDMIND[i][j].h=0;
	  GRIDMIND[i][j].previou=undefined;
    }
}	  

//[MH] A* Algorithm start
while (openSet.length > 0) 
  {
    // Best next option
    var winner = 0;
    
   for (var i = 0; i < openSet.length; i++) 
      if (openSet[i].f < openSet[winner].f) 
        winner = i;
        
    var current = openSet[winner];
    // Did I finish?
    if (current === end2) 
    {
     console.log("success - found path2");
    break;
    }

    // Best option moves from openSet to closedSet
    removeFromArray(openSet, current);
    closedSet.push(current);
    // Check all the neighbors
    var neighbors = current.neighbors;

    //--- start of for loop -----------
    for (var i = 0; i < neighbors.length; i++) 
    {
      var neighbor = neighbors[i];
      // Valid next spot?
        if (!closedSet.includes(neighbor) && (neighbor.value)!==2 && (neighbor.value)!==1)  // [MH] Discard walls and mazes
        {
        var tempG = current.g + heuristic(neighbor, current);
        // Is this a better path than before?
        var newPath = false;
        if (openSet.includes(neighbor)) 
        {
          if (tempG < neighbor.g) 
          {
            neighbor.g = tempG;
            newPath = true;
          }
        }
        else 
        {
          neighbor.g = tempG;
          newPath = true;
          openSet.push(neighbor);
        }

        // Yes, it's a better path
        if (newPath) 
        {
          neighbor.h = heuristic(neighbor, end2);
          neighbor.f = neighbor.g + neighbor.h;
          neighbor.previou = current;
        }
        }
    }
  }
 
 //[MH] Get Path 
  var path = [];
  var temp = current;
  path.push(temp);
  while (temp.previou) 
  {
    path.push(temp.previou);
    temp = temp.previou;
  }


  si=path[path.length-2].i;   //[MH] Lenght-2 to discard myself otherwise will not move
  sj=path[path.length-2].j;   //[MH] Lenght-2 to discard myself otherwise will not move

  if (si<ai) return 0;
  if (si>ai) return 1;
  if (sj<aj) return 3;
  if (sj>aj) return 2;
  else return AB.randomIntAtoB (0,3);
 
 	}
 		
 		
 		
	};