Code viewer for Mind: New Mind
 
function Mind() 
{ 
	var old_state = {};
	var map = {};
	var old_action;
	
	this.newRun = function()                  
	{
	    for (var x = 0; x < 20; x++) {
	        map[x] = {};
    	    for (var y = 0; y < 20; y++)
        	    map[x][y] = 0;
	    }
	    old_state[0] = 0;
	    old_state[1] = 0;
	    old_state[2] = 0;
	    old_state[3] = 0;
	};


	this.getAction = function (state)		 
	{
	var vec = {};
	var bloqued = false;

	vec[0] = (state[0] - state[2]);
	vec[1] = (state[1] - state[3]);
	if (vec[0] > vec[1] && vec[0] > 0)
    	  action = ACTION_RIGHT;
	else if (vec[0] > vec[1]  && vec[0] < 0)
    	  action = ACTION_LEFT;
	else if (vec[0] < vec[1]  && vec[1] > 0)
    	  action = ACTION_UP;
    else if (vec[0] < vec[1]  && vec[1] < 0)
    	  action = ACTION_DOWN;
    if (old_state[0] == state[0] && old_state[1] == state[1]) {
        bloqued = true;
        if (old_action == ACTION_UP && state[3] != state[1] + 1) {
            map[old_state[0]][old_state[1] + 1] = 1;
        }
        if (old_action == ACTION_DOWN && state[3] != state[1] - 1) {
            map[old_state[0]][old_state[1] - 1] = 1;
        }
        if (old_action == ACTION_LEFT && state[2] != state[0] - 1) {
            map[old_state[0] - 1][old_state[1]] = 1;
        }
        if (old_action == ACTION_RIGHT && state[2] != state[0] + 1) {
            map[old_state[0] + 1][old_state[1]] = 1;
        }
    }
    else
        bloqued = false;
        
    if  (bloqued === true && map[state[0] - 1][state[1]] != 1)
        	  action = ACTION_LEFT;
    else if (bloqued === true && map[state[0]][state[1] + 1] != 1)
        	  action = ACTION_DOWN;
    else if (bloqued === true && map[state[0] + 1][state[1]] != 1)
        	  action = ACTION_RIGHT;
    else if (bloqued === true == state[1] && map[state[0]][state[1] + 1] != 1)
        	  action = ACTION_UP;
        	  
	old_state[0] = state[0];
	old_state[1] = state[1];
	old_state[2] = state[2];
	old_state[3] = state[3];
	old_action = action;
	return (action);
	};

		 
	this.endRun = function()                 
	{
	};

}