Code viewer for Mind: Complex Mind (clone by Kus...

// Cloned by Kushagra on 28 Nov 2020 from Mind "Complex Mind (clone by Kushagra)" by Kushagra 
// Please leave this clone trail here.
 


// Cloned by Kushagra on 26 Nov 2020 from Mind "Complex Mind" by Starter user 
// Please leave this clone trail here.
 



// =================================================================================================
// Sample Mind for more complex starter World  
// =================================================================================================

// World tells us agent position and enemy position
// World does not tell us of existence of walls
// if return invalid move (not empty square) World just ignores it and we miss a turn 

//---------------changes made by Kushagra-----------------------------

    var first_move_agent  = 0 ; // variable to track the first movement by the agent
    var old_position; // store the old position that the agent has moved

    function findBestPath2(grid,e,a){
    
    var path = [];
    var openSet = [];
    var closedSet = [];
    var return_spot;
    openSet.push(e);
    
    while(openSet.length > 0){
            
            var winner = 0;
            for (var i = 0; i < openSet.length; i++) {
                if (openSet[i].f < openSet[winner].f) {
                winner = i;
                }
             }
             
        var current = openSet[winner];
        
        if(current == a ){
            
            // reached enemy, return the first element in the path array
            var temp = current;
            path.push(temp);
            while(temp.previous){
                if(temp.previous == e)
                    return_spot = temp;
                path.push(temp.previous);
                temp = temp.previous;
            }
            displayPathAgent(path);
            break;
            
        }else{
            
            //continue search
            removeFromArray(openSet,current);
            closedSet.push(current);
            var neighbors = current.neighbors;
            for(var j = 0;j<neighbors.length;j++){
                var neighbor = neighbors[j];
                
                
                if(!closedSet.includes(neighbor) && !occupied(neighbor.i,neighbor.j)){
                    var tempG = current.g + 1;
                    
                    if(openSet.includes(neighbor)){
                        if(tempG< neighbor.g){
                            neighbor.g = tempG;
                        }
                    }else{
                        neighbor.g = tempG;
                        openSet.push(neighbor);
                    }
                    neighbor.h = heuristic(neighbor,a);
                    neighbor.f = neighbor.g + neighbor.f;
                    neighbor.previous = current;
                
                }
             }
        
        }
    }
    
    return return_spot;
    
}
 
//---------------end of changes made by Kushagra-----------------------------

	AB.mind.getAction = function ( x )		// x is an array of [ ai, aj, ei, ej ]
	{ 
		var ai = x.position[0];
		var aj = x.position[1];
		var ei = x.position[2];
		var ej = x.position[3];
		

//------------------------------BEGIN OF DEFAULT AGENT CODE BLOCK----------------------------------------------------------------		

		// if strictly move away, will get stuck at wall, so introduce randomness 
            
		/* if ( ej < aj ) 	return ( AB.randomPick ( ACTION_UP,		AB.randomPick(ACTION_RIGHT,ACTION_LEFT) 	)); 
		 if ( ej > aj ) 	return ( AB.randomPick ( ACTION_DOWN,	AB.randomPick(ACTION_RIGHT,ACTION_LEFT) 	)); 

		 if ( ei < ai ) 	return ( AB.randomPick ( ACTION_RIGHT,	AB.randomPick(ACTION_UP,ACTION_DOWN) 		)); 
		 if ( ei > ai ) 	return ( AB.randomPick ( ACTION_LEFT,	AB.randomPick(ACTION_UP,ACTION_DOWN) 		)); 

 		return  ( AB.randomIntAtoB (0,3) );*/
 		
//-------------------------ENDOF DEFAULT AGENT CODE BLOCK--------------------------------------------------------------



//------------------------BEGIN OF A* AGENT CODE BLOCK by Kushagra-----------------------------------------------------------------	


//set the target points for the Agent , it will try and move to the opposite quadrant that the enemy is

// set the target positions
        var target_i,target_j;
        var flag = 0;
        var target_position;
//if the enemy in the central region then do not do trigger a new A* path
//the agent will keep following the path that was set in the previous step
//also we need to make sure that if the agent has reached his target location then
//a new set of coordinates needs to be given
        if(ei>gridsize/3 && ei<2*(gridsize/3) && ej>gridsize/3 && ej<2*(gridsize/3) && first_move_agent== 1
        && ai!=old_target_position_i && aj!=old_target_position_j){
            
            target_i = old_target_position_i;
            target_j = old_target_position_j;
            target_position = search_grid[target_i][target_j];
        }
        else{
                if(ei>=gridsize/2 && ej<= gridsize/2){
                    //enemy is in first quandrant set the target position to some 
                    //unoccupied position in the opposite quadrant; 3 quandrant
                    flag = 0;
                    for(var i=AB.randomPick(1,2,3);i<=gridsize/2;i++){
                        for(var j=AB.randomPick(gridsize-2,gridsize-3,gridsize-4);j>gridsize/2;j--){
                            if(!occupied(i,j)){
                                target_i = i;
                                target_j= j;
                                flag = 1;
                                break;
                            }
                        }
                        if(flag==1)
                            break;
                    }
                }
        
                else if(ei<=gridsize/2 && ej<=gridsize/2){
                    //enemy is in the second quadrant then move the agent to the 4th quadrant
                    flag = 0;
                    for(var i = AB.randomPick(gridsize-2,gridsize-3,gridsize-4);i>gridsize/2;i--){
                        for(var j = AB.randomPick(gridsize-2,gridsize-3,gridsize-4);j>=gridsize/2;j--){
                            if(!occupied(i,j)){
                                target_i = i;
                                target_j= j;
                                flag=1;
                                break;
                            }
                        }
                        if(flag==1)
                            break;
                    }
                }
        
                else if(ei<=gridsize/2 && ej>=gridsize/2)  {
                    
                    //if enemy in the third quandrant then the target point is somewhere in the
                    //first quandrant
                    flag=0
                    for(var i=AB.randomPick(gridsize-2,gridsize-3,gridsize-4); i>gridsize/2;i--){
                        for(var j=AB.randomPick(1,2,3);j<=gridsize/2;j++){
                            if(!occupied(i,j)){
                                target_i = i;
                                target_j= j;
                                flag=1;
                                break;
                            }
                        }
                        if(flag==1)
                            break;
                    }
                }  
        
                else if(ei>=gridsize/2 && ej>=gridsize/2){
                    //is enemy is in the fourth quandrant then the target point
                    //is somewhere in the 2nd quadrant
                    flag = 0;
                    for(var i=AB.randomPick(1,2,3);i<=gridsize/2;i++){
                        for(var j=AB.randomPick(1,2,3);j<=gridsize/2;j++){
                            if(!occupied(i,j)){
                                target_i = i;
                                target_j= j;
                                flag=1;
                                break;
                            }
                        }
                        if(flag==1)
                            break;
                    }
                }
                
            target_position = search_grid[target_i][target_j];
        }
        
        
 		

	
	var agent_position = search_grid[ai][aj];
    //var target_position = search_grid[target_i][target_j];
    
    var clear;

    var new_position = findBestPath2(search_grid,agent_position,target_position)
    old_target_position_i = target_i;
    old_target_position_j = target_j;
        first_move_agent = 1;

    
    
    for (i = 0; i < gridsize; i++) {
        for (j = 0; j < gridsize; j++) {
            clear = search_grid[i][j];
            clear.f = 0;
            clear.h = 0;
            clear.g = 0;
            clear.previous = undefined;
        }
    }
    
    return new_position;
    
	};