Code viewer for Mind: Cloned New Mind
// Cloned by Canille on 23 Nov 2018 from Mind "New Mind" by Canille 
// Please leave this clone trail here.
//import World from '../Worlds/5108602103.js';
const sizegrid = 20;
const BLANK = 0;
const WALL = 1;
const BLOCK = 2;
const CHAISPAS = 3;
const IS_ENNEMY = 1;
const IS_ME = 2;
const IS_WALL = 3;
const IS_BLOCK = 4;
const IS_EMPTY = 0;
const L = 0;
const R = 1;
const U = 2;
const D = 3;


 
function Mind() 
{ 
	var GRID = new Array(sizegrid);
	var tab = new Array(4);
	var my_x, my_y, his_x, his_y;
	var step = 0;
	var previousMove;
    var previousState;
	var changed = 0;
	var newMove;
	
	for ( i = 0; i < gridsize ; i++ )
	{
	    GRID[i] = new Array(sizegrid);
	}
	
	for ( i = 0; i < 4 ; i++ )
	{
	    tab[i] = new Array(2);
	}
	
	 for ( i = 0; i < sizegrid ; i++ )
	 {
    	  for ( j = 0; j < sizegrid ; j++ )
    	  {
        		if ( ( i==0 ) || ( i==sizegrid-1 ) || ( j==0 ) || ( j==sizegrid-1 ) )
        		{
        			GRID[i][j] = WALL;
        		}
        		else
        		{
           			GRID[i][j] = CHAISPAS;
        		}
    	  }
	 }
	
	function occupied ( i, j )		// is this square occupied
    {
         if ( ( his_x == i ) && ( his_y == j ) ) return IS_ENNEMY;		// variable objects 
         if ( ( my_x == i ) && ( my_x == j ) ) return IS_ME;
        
         if ( GRID[i][j] == WALL ) return IS_WALL;		// fixed objects	 
         if ( GRID[i][j] == BLOCK ) return IS_BLOCK;		 
        	 
         return IS_EMPTY;
    }
    
    function carto (i, j, nature)
    {   
        if(GRID[i][j]==CHAISPAS && nature == BLOCK) GRID[i][j] = 0.5
        if(GRID[i][j]==0.5 && nature == BLOCK) GRID[i][j] = BLOCK
        if(GRID[i][j]==0.5 && nature == BLANK) GRID[i][j] = CHAISPAS
        else GRID[i][j] = nature;
    }
    
    function distance(ai, aj, ei, ej)
    {
        var dist = Math.sqrt(Math.abs(ai-aj)*Math.abs(ai-aj) + Math.abs(ei - ej)*Math.abs(ei - ej));
        console.log("distance : ",dist);
        return dist;
    }
    
    function compareSecondColumn(a, b) {
        if (a[1] === b[1]) {
            return 0;
        }
        else {
            return (a[1] < b[1]) ? -1 : 1;
        }
    } 
    
    function isok(mouv, pMove)
    {
        if(mouv == L)
        {
            if((occupied (my_x,my_y-1)==WALL)||(occupied (my_x,my_y-1)==BLOCK)||(mouv==pMove))
            {
                return false;
            }
            return true;
        }
        else if(mouv == R)
        {
            if((occupied (my_x,my_y+1)==WALL)||(occupied (my_x,my_y+1)==BLOCK)||(mouv==pMove))
            {
                return false;
            }
            return true;
        }
        else if(mouv == U)
        {
            if((occupied (my_x-1,my_y)==WALL)||(occupied (my_x-1,my_y)==BLOCK)||(mouv==pMove))
            {
                return false;
            }
            return true;
        }
        else
        {
            if((occupied (my_x+1,my_y)==WALL)||(occupied (my_x+1,my_y)==BLOCK)||(mouv==pMove))
            {
                return false;
            }
            return true;
        }
        return true;
    }
    
    function move(state,pMove,achange)
    {
        var nMove;
        var t=0;
        console.log("hello2");
        for ( i = 0; i < 4 ; i++ )
    	{
    	    tab[i][0] = i;
    	    tab[i][1] = 0;
    	}
    	/*tab[0][1] = distance(state[0], state[1]-1, state[2], state[3]); //L
    	console.log("trucs : ",state[0], state[1]-1, state[2], state[3]);
    	console.log("trucs 2 : ",tab[0][1]);
    	tab[1][1] = distance(state[0], state[1]+1, state[2], state[3]); //R
    	console.log("trucs 12 : ",tab[1][1]);
    	tab[2][1] = distance(state[0]-1, state[1], state[2], state[3]); //U
    	tab[3][1] = distance(state[0]+1, state[1], state[2], state[3]); //D
    	console.log("trucs 44 : ",tab[0][1]);
    	console.log("move : ", tab);
    	console.log("trucs 45 : ",tab[0][1]);
    	tab.sort(compareSecondColumn)
    	for ( i = 3; i >= 0 ; i-- )
        	{
        	    if(isok(tab[i][0], pMove))
        	    {
        	        t = i;
        	        break;
        	    }
        	}
        	
        nMove = tab[t][0];
        return nMove;*/
        	
        	
    	tab[0][1] = distance(state[0]-1, state[1], state[2], state[3]); //L
    	tab[1][1] = distance(state[0]+1, state[1], state[2], state[3]); //R
    	tab[2][1] = distance(state[0], state[1]-1, state[2], state[3]); //U
    	tab[3][1] = distance(state[0], state[1]+1, state[2], state[3]); //D
    	var maxDist=0;
    	var bestIndX=-1
    	for(i=0 ; i<4 ; i++){
    	    if(tab[i][1] > maxDist && isok(tab[i][0], pMove)){
    	        maxDist=tab[i][1]
    	        bestIndX=i
    	    }
    	}
    	return bestIndX;
    }
    
    function check(pState, state, pMove)
    {
        if((pState[0] == state[0])&&(pState[1] == state[1]))
        {
            if(pMove == L)
            {
                carto (my_x, my_y-1, BLOCK);
            }
            else if(pMove == R)
            {
                carto (my_x, my_y+1, BLOCK);
            }
            else if(pMove == U)
            {
                carto (my_x-1, my_y, BLOCK);
            }
            else
            {
                carto (my_x+1, my_y, BLOCK);
            }
            return 1;
            
        }
        return 0;
    }
	
	this.newRun = function()                  
	{
	    
	};


	this.getAction = function (state)		 
	{ 
	  my_x = state[0];
	  my_y = state[1];
	  his_x = state[2];
	  his_y = state[3];
	  
	  carto(my_x, my_y, BLANK);
	  carto(his_x, his_y, BLANK);
	  
	  if(step === 0)
	  {
	      carto(my_x, my_y, BLANK);
	      carto(his_x, his_y, BLANK);
	  }
	  else
	  {
	      console.log("hello1");
	      changed = check(previousState, state, previousMove);
	      
	      
	  }
	  
	  newMove = move(state, previousMove, changed);
	  console.log(GRID);
	  console.log(step);
	  console.log("state : ",state);

	  previousState = state;
	  previousMove = newMove;
	  changed = 0;
	  step ++;
	  return (newMove);		 		
	};

		 
	this.endRun = function()                 
	{
	    
	};
}