Code viewer for Mind: oap mind
var starter = [];
var laststeps = [];
const mindistance = 4;
const stepcount = 5;
var nextstep;
var randomflag = 0;
var discount;


function isBelowThreshold(currentValue) {
  return currentValue < mindistance;
}

// function cost(ai, aj, ei, ej){
//     var distance = AB.distance2D (ai,aj,ei,ej);
//     if(distance > 1){
//         console.log(1);
//     }
    
// }

function distance(ai, aj, ei, ej){
    return Math.sqrt((Math.pow(ei, 2) - Math.pow(ai, 2)) + (Math.pow(ej, 2) - Math.pow(aj, 2)))
}



function Mind() 
{ 
    
        
        this.getAction = function ( x )         // x is an array of [ ai, aj, ei, ej ]
        { 

		if (randomflag === 0){
                var ai = x[0];
                var aj = x[1];
                var ei = x[2];
                var ej = x[3];
				
				
        		if (randomflag == 1){
        			//check distance from agent to starter
        			
        			if (distance(ai, aj, ei, ej) > mindistance){  //distance between points
        				randomflag = 0;
        				starter = [ai,aj];
        			}
        		}
				
		
				if (starter === []){
					starter = [ai,aj];
				}

                // if strictly move away, will get stuck at wall, so introduce randomness 

                 if ( ej < aj ){
                     nextstep = ( AB.randomPick ( ACTION_UP,             AB.randomPick(ACTION_RIGHT,ACTION_LEFT) )); 
                     
				 }
				 
                 if ( ej > aj ){
					nextstep = ( AB.randomPick ( ACTION_DOWN,   AB.randomPick(ACTION_RIGHT,ACTION_LEFT)         )); 
				 }

                 if ( ei < ai ){         
					nextstep = ( AB.randomPick ( ACTION_RIGHT,  AB.randomPick(ACTION_UP,ACTION_DOWN)            ));
				 }
				 
				 
                 if ( ei > ai ){
					nextstep = ( AB.randomPick ( ACTION_LEFT,   AB.randomPick(ACTION_UP,ACTION_DOWN)            )); 
				 }

                
				else{
				    var allmoves = [ACTION_LEFT,ACTION_RIGHT,ACTION_UP,ACTION_DOWN];
				    nextstep =  ( AB.randomElementOfArray ( allmoves ));
				}
				
				laststeps.push(nextstep);
				
				if (laststeps.length == stepcount){
					
					if (laststeps.every(isBelowThreshold)){
						randomflag = 1;
					}
					
					laststeps = [];
				}
				return nextstep;
        }
		
		if (randomflag == 1){
			return ( AB.randomPick ( AB.randomPick(ACTION_LEFT,ACTION_RIGHT),   AB.randomPick(ACTION_UP,ACTION_DOWN)    ));
		}
		};

}