Code viewer for Mind: Mind v3.0

// Cloned by Lazarus on 10 Nov 2018 from Mind "Mind v2.0" by Lazarus 
// Please leave this clone trail here.
 


// 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.

//const ACTION_LEFT 			= 0;		   
//const ACTION_RIGHT 			= 1;
//const ACTION_UP 			    = 2;		 
//const ACTION_DOWN 			= 3;
//const ACTION_STAYSTILL 		= 4;
 

function Mind() 
{ 
    var previousAction = 4;
    var previousState = new Array(4);
    var wallCoords = new Array(gridsize*gridsize);
    var wallCoordsIndex = 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];
	    
	    console.log("Printing Walls");
	    console.log(JSON.stringify(wallCoords));
	    
	    var distance = this.tilesOutOfPlace(state);

	    if (previousState.toString() == state.toString()){
	        //add the wall into the badCoords array
	        //wall to the left
	        if (previousAction ===0 ) {
	            if (JSON.stringify(wallCoords).indexOf(JSON.stringify([ai-1, aj])) == -1) {
	                wallCoords[wallCoordsIndex] = [ai-1, aj];
	                wallCoordsIndex += 1;
	            }
	        }
	        
	        //wall to right
	        if (previousAction == 1) {
	            if (JSON.stringify(wallCoords).indexOf(JSON.stringify([ai+1, aj])) == -1) {
	                wallCoords[wallCoordsIndex] = [ai+1, aj];
	                wallCoordsIndex += 1;
	            }
	        }
	        
	        //wall up
	        if (previousAction == 2) {
	            if (JSON.stringify(wallCoords).indexOf(JSON.stringify([ai, aj+1])) == -1) {
	                wallCoords[wallCoordsIndex] = [ai, aj+1];
	                wallCoordsIndex += 1;
	            }
	        }
	        //wall down
	        if (previousAction == 3) {
	            if (JSON.stringify(wallCoords).indexOf(JSON.stringify([ai, aj-1])) == -1) {
	                wallCoords[wallCoordsIndex] = [ai, aj-1];
	                wallCoordsIndex += 1;
	            }	            
	        }
	        
	        console.log(state);	       
	        console.log(previousState);
	        console.log("stuck-move");
	        return AB.randomIntAtoB(0,3);
	    }
	    else{
	        previousState = state;
	    }
	    
	    
	    
	    if (distance < 8){
	            console.log("dist-move");
	            if ((this.tilesOutOfPlace([ai, aj+1, ei, ej]) > distance) && (aj != 18) && (JSON.stringify(wallCoords).indexOf(JSON.stringify([ai, aj+1])) == -1)){
	                previousAction = 2;
	                return ACTION_UP;
	            }
	            
	            else if ((this.tilesOutOfPlace([ai, aj-1, ei, ej]) > distance) && (aj != 1) && (JSON.stringify(wallCoords).indexOf(JSON.stringify([ai, aj-1])) == -1)){
	                previousAction = 3;
	                return ACTION_DOWN;
	            }
	            
	            else if ((this.tilesOutOfPlace([ai+1, aj, ei, ej]) > distance) && (ai != 18) && (JSON.stringify(wallCoords).indexOf(JSON.stringify([ai+1, aj])) == -1)){
	                previousAction = 1;
	                return ACTION_RIGHT;
	            }
	            
	            else if ((this.tilesOutOfPlace([ai-1, aj, ei, ej]) > distance && (ai != 1) && (JSON.stringify(wallCoords).indexOf(JSON.stringify([ai-1, aj])) == -1))){
	                previousAction = 0;
	                return ACTION_LEFT;
	            }
	            else {
	                previousAction = 5;
	                return ACTION_STAYSTILL;
	            }
	            
	    }        
	    
	    else {
	        var goal = this.closestCornerMove(state);
	            console.log("corner_move");
	        
	            if ((this.tilesOutOfPlace([ai, aj+1, goal.i, goal.j]) < goal.distance) && (JSON.stringify(wallCoords).indexOf(JSON.stringify([ai, aj+1])) == -1)){
	                previousAction = 2;
	                return ACTION_UP;
	            }
	            
	            else if ((this.tilesOutOfPlace([ai, aj-1, goal.i, goal.j]) < goal.distance) && (JSON.stringify(wallCoords).indexOf(JSON.stringify([ai, aj-1])) == -1)){
	                previousAction = 3;
	                return ACTION_DOWN;
	            }
	            
	            else if ((this.tilesOutOfPlace([ai+1, aj, goal.i, goal.j]) < goal.distance) && (JSON.stringify(wallCoords).indexOf(JSON.stringify([ai+1, aj])) == -1)){
	                previousAction = 1;
	                return ACTION_RIGHT;
	            }
	            
	            else if ((this.tilesOutOfPlace([ai-1, aj, goal.i, goal.j]) < goal.distance) && (JSON.stringify(wallCoords).indexOf(JSON.stringify([ai-1, aj])) == -1)){
	                previousAction = 0;
	                return ACTION_LEFT;
	            }
	            else {
	                previousAction = 4;
	                return ACTION_STAYSTILL;
	            }
	    }
	    
	    

	};
	

		 
	this.endRun = function()                 
	{
	};

}