Code viewer for Mind: stall_mind

// Cloned by Eoin McLaughlin on 26 Nov 2018 from Mind "block_dict_mind" by Eoin McLaughlin 
// Please leave this clone trail here.
 
 
function Mind() 
{ 
    this.compare_Lists = function(a1, a2)
    {
        return (a1[0] === a2[0]) && (a1[1] === a2[1])
    }

    
    this.in_array = function(big, small)
    {
        i = 0
        while (i < big.length)
        {
            if (this.compare_Lists(big[i], small))
                {return true}
            i += 1
        }
        return false
    }
    
    this.distance = function(object1, object2)
    {   
        xdistance = object2[0]-object1[0]
        if (xdistance < 0)
            xdistance = -xdistance
        
        ydistance = object2[1]-object1[1]
        if (ydistance < 0)
            ydistance = -ydistance
        
        return xdistance+ydistance
            
    };  
    
    this.sort_Potential_Destinations = function(destinations)
    {
        i = 0
        while (i < destinations.length)
        {
            maxi = i
            j = i
            while (j < destinations.length)
            {
                if (destinations[j][0] > destinations[maxi][0])
                    maxi = j;
                j += 1
            }
            tmp = destinations[maxi]
            destinations[maxi] = destinations[i]
            destinations[i] = tmp
            i += 1
        }
    }
	
	this.newRun = function()                  
	{
	    console.log("newRun")
	    this.previous_Agent_Position = [0, 0]
	    this.previous_Enemy_Position = [0, 0]
	    this.intended_Position = [0, 0]
	    this.indended_move = -1
	    this.stall_index = 0
	};


	this.getAction = function ( state )		 
	{ 
	  console.log("######### GET ACTION #########")
	  agent_x = state[0]
	  agent_y = state[1]
	  enemy_x = state[2]
	  enemy_y = state[3]
	  current_Agent_Position = [state[0], state[1]]
	  current_Enemy_Position = [state[2], state[3]]
	  console.log("Current Agent Position = " + current_Agent_Position)
	  console.log("Current Enemy Position = " + current_Enemy_Position)
	  
	  console.log(this.intended_Position)
	  console.log(current_Agent_Position)
	  if (this.compare_Lists(this.previous_Agent_Position, current_Agent_Position) && this.intended_move !== 4)
	    {
	        this.stall_index += 1
	        if (this.stall_index > 5)
	        {
	            this.stall_index = 0
	        }
	    }
	    else this.stall_index = 0

	  potential_Moves_To_Destinations = 
	  {
	      0:[agent_x-1, agent_y],
	      1:[agent_x+1, agent_y],
	      2:[agent_x, agent_y+1],
	      3:[agent_x, agent_y-1],
	      4:[agent_x, agent_y]
	  }


	  a = []
	  i = 0
	  
	  while (i < 5)
	  {
	      move = potential_Moves_To_Destinations[i]
	      {
    	      d = this.distance(move, current_Enemy_Position)
    	      //Index 0 of each move is the distance between the Agent and Enemy at that position. Index 1 is the move (0...4) to bring the Agent to the position 
    	      a.push([d, i])
	      }
	      i += 1
	  }
      
	  this.sort_Potential_Destinations(a)
	  
	  console.log(a)

	  
	  this.previous_Agent_Position = current_Agent_Position
	  this.previous_Enemy_Position = current_Enemy_Position
	  this.intended_position = potential_Moves_To_Destinations[a[this.stall_index][1]]
	  console.log("chosen move = " + a[this.stall_index][1])
	  this.intended_move = a[this.stall_index][1]
	  return a[this.stall_index][1]
	};

		 
	this.endRun = function()                 
	{
	    console.log("endRun")
	};

}