Code viewer for Mind: New Mind
function Mind() 
{ 
    var previousState = new Array(4);
    var previousMove = 6;
    var p1 = 0;
    var p2 = 0;
    var blocked_coordinates = [];
    var blockedIndex = 0;

    
    this.checkMove = function( state ){
        
	   	var ai = state[0];
	    var ei = state[2];
	    var aj = state[1];
	    var ej = state[3];
        
        if (previousMove === 0){
            p1 = ai-1;
            p2 = aj;
        }
        else if (previousMove == 1){
            p1 = ai + 1;
            p2 = aj
        }
        
        else if (previousMove == 2){
            p1 = ai;
            p2 = aj + 1;
        }
        
        else {
            p1 = ai;
            p2 = aj - 1;
        }
        return
    }
    
    
    
    
    this.blockedPath = function(pos1, pos2, a) {
    var positions = [pos1, pos2];
    
    if (a == "c"){
        console.log("positions " + positions);
        console.log("block coordinate result " + blocked_coordinates.indexOf(positions));
        console.log("blocked_coordinates " + blocked_coordinates);
        return blocked_coordinates.indexOf(positions);

    }
    else{
        blocked_coordinates.push(positions);
        console.log("current blocked_path " + blocked_coordinates);
        console.log("Added Block");
        blockedIndex++;
        return;
    }
    }
     
	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");
	        this.checkMove( state );
	        this.blockedPath(p1, p2, "u");
	        }
	        
	   else {
	        previousState = state;
	        }
	    
	    
	    
	    if (distance < 8){
        	    console.log("dist-move");
	            if ((this.tilesOutOfPlace([ai, aj+1, ei, ej]) > distance) && (aj != 18) && (this.blockedPath(ai, aj+1, "c") < 0)){
	                previousMove = ACTION_UP;
	                return ACTION_UP;
	            }
	            
	            else if ((this.tilesOutOfPlace([ai, aj-1, ei, ej]) > distance) && (aj != 1) && (this.blockedPath(ai, aj-1, "c") < 0)){
	                previousMove = ACTION_DOWN;
	                return ACTION_DOWN;
	            }
	            
	            else if ((this.tilesOutOfPlace([ai+1, aj, ei, ej]) > distance) && (ai != 18) && (this.blockedPath(ai+1, aj, "c") < 0)){
	                previousMove = ACTION_RIGHT;
	                return ACTION_RIGHT;
	            }
	            
	            else if ((this.tilesOutOfPlace([ai-1, aj, ei, ej]) > distance) && (ai != 18) && (this.blockedPath(ai-1, aj, "c") < 0)){
	                previousMove = ACTION_LEFT
	                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 && (this.blockedPath(ai, aj+1, "c") < 0)){
	                previousMove = ACTION_UP;
	                return ACTION_UP;
	            }
	            
	            else if (this.tilesOutOfPlace([ai, aj-1, goal.i, goal.j]) < goal.distance && (this.blockedPath(ai, aj-1, "c") < 0)){
	                previousMove =  ACTION_DOWN;
	                return ACTION_DOWN;
	            }
	            
	            else if (this.tilesOutOfPlace([ai+1, aj, goal.i, goal.j]) < goal.distance && (this.blockedPath(ai+1, aj, "c") < 0)){
	                previousMove = ACTION_RIGHT;
	                return ACTION_RIGHT;
	            }
	            
	            else if (this.tilesOutOfPlace([ai-1, aj, goal.i, goal.j]) < goal.distance && (this.blockedPath(ai-1, aj, "c")< 0)){
	                previousMove = ACTION_LEFT;
	                return ACTION_LEFT;
	            }
	    }
	    
	    

	};
	

		 
	this.endRun = function()                 
	{
	};

}