Code viewer for Mind: Mind v2.0

// Cloned by Lazarus on 8 Nov 2018 from Mind "Cloned Cloned New Mind" by Lazarus 
// Please leave this clone trail here.
 


// Cloned by Lazarus on 7 Nov 2018 from Mind "Cloned New Mind" by Lazarus 
// Please leave this clone trail here.
 


// Cloned by Lazarus on 6 Nov 2018 from Mind "New Mind" by Lazarus 
// Please leave this clone trail here.
 

function Mind() 
{ 
    var previousState = new Array(4);
    var badMoves = new Array(gridsize*gridsize);
    var badMovesIndex = 0;
     
	this.newRun = function()                  
	{
	};
	
    this.tilesOutOfPlace = function( state ) {
	   	var ai = state[0];
	    var ei = state[2];
	    var aj = state[1];
	    var ej = state[3];
	    
	    return Math.abs(ai - ei) + Math.abs(aj - ej) - 1;
	    
    };
    
    this.closestCornerMove = function(state){
        var ai = state[0];
	    var aj = state[1];
	    var ei = state[2];
	    var ej = state[3];
	    
	    const topLeft = {
	        
	        distance: 0,
	        i: 2,
	        j: 2,
	   };
	   
	    const topRight = {
	        
	        distance: 0,
	        i: 17,
	        j: 2,
	   };

	    const bottomLeft = {
	        
	        distance: 0,
	        i: 2,
	        j: 17,
	   };
	    

	    const bottomRight = {
	        
	        distance: 0,
	        i: 17,
	        j: 17,
	   };	    
	    
	    
	    topLeft.distance = this.tilesOutOfPlace([ai, aj, 2, 2]);
	    bottomLeft.distance = this.tilesOutOfPlace([ai, aj, 2, 17]);
	    topRight.distance = this.tilesOutOfPlace([ai, aj, 17, 2]);
	    bottomRight.distance = this.tilesOutOfPlace([ai, aj, 17, 17]);
	    
	    var corners = [topLeft, bottomLeft, topRight, bottomRight];
	    var distances = [topLeft.distance, bottomLeft.distance, topRight.distance, bottomRight.distance];
	    distances = distances.sort();
	    for (var i = 0; i < corners.length ; i++){
	        if (corners[i].distance == distances[1]){
	            return corners[i]
	        }
	    }
	    
    };

	this.getAction = function ( state )	{
	   
	    
	   	var ai = state[0];
	    var ei = state[2];
	    var aj = state[1];
	    var ej = state[3];
	    
	    var distance = this.tilesOutOfPlace(state);

	    if (previousState.toString() == state.toString()){
	        console.log(state);	       
	        console.log(previousState);
	        console.log("stuck-move");
	        return AB.randomIntAtoB(0,3);
	    }
	    else{
	        previousState = state;
	    }
	    
	    
	    
	    if (distance < (gridsize/2)){
	            console.log("dist-move");
	            if ((this.tilesOutOfPlace([ai, aj+1, ei, ej]) > distance) && (aj != 18)){
	                return ACTION_UP;
	            }
	            
	            else if ((this.tilesOutOfPlace([ai, aj-1, ei, ej]) > distance) && (aj != 1)){
	                return ACTION_DOWN;
	            }
	            
	            else if ((this.tilesOutOfPlace([ai+1, aj, ei, ej]) > distance) && (ai != 18)){
	                return ACTION_RIGHT;
	            }
	            
	            else {
	                return ACTION_LEFT;
	            }
	            
	    }        
	    
	    else {
	        var goal = this.closestCornerMove(state);
	            console.log("corner_move");
	        
	            if (this.tilesOutOfPlace([ai, aj+1, goal.i, goal.j]) < goal.distance){
	                return ACTION_UP;
	            }
	            
	            else if (this.tilesOutOfPlace([ai, aj-1, goal.i, goal.j]) < goal.distance){
	                return ACTION_DOWN;
	            }
	            
	            else if (this.tilesOutOfPlace([ai+1, aj, goal.i, goal.j]) < goal.distance){
	                return ACTION_RIGHT;
	            }
	            
	            else if (this.tilesOutOfPlace([ai-1, aj, goal.i, goal.j]) < goal.distance) {
	                return ACTION_LEFT;
	            }
	            else {
	                return ACTION_STAYSTILL;
	            }
	    }
	    
	    

	};
	

		 
	this.endRun = function()                 
	{
	};

}