Code viewer for Mind: Cloned Cloned Complex Mind

// Cloned by mik on 19 Nov 2018 from Mind "Cloned Complex Mind" by Joe Ninety One 
// Please leave this clone trail here.
 


// Cloned by Joe Ninety One on 18 Nov 2018 from Mind "Complex Mind" by Starter user 
// Please leave this clone trail here.
 



// ==== Starter Mind ===============================================================================================
// (c) Ancient Brain Ltd. All rights reserved.
// This code is only for use on the Ancient Brain site.
// This code may be freely copied and edited by anyone on the Ancient Brain site.
// This code may not be copied, re-published or used on any other website.
// To include a run of this code on another website, see the "Embed code" links provided on the Ancient Brain site.
// ==================================================================================================================




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


NO_GO_VALUE = -10;
NOT_KNWOWN_VALUE = 1;
function Mind() 
{ 
    this.world = [];
    let border = [];
    for (let i = 0; i < 19; i++) {
        border.push(NO_GO_VALUE);
    }
    this.world.push(border)
    for (let i =0; i < 18; i++){
        let tmp = [NO_GO_VALUE];
        for (let i =0; i < 18; i++){
            tmp.push(NOT_KNWOWN_VALUE);
        }
        tmp.push(NO_GO_VALUE);
        this.world.push(tmp);
    }
    this.world.push(border);
    this.last_pos = undefined;
	this.getAction = function ( x )		// x is an array of [ ai, aj, ei, ej ]
	{ 
	    console.log(x)
	    /*console.log(ACTION_LEFT, ACTION_RIGHT, ACTION_UP, ACTION_DOWN)
	    return*/
	    //return ACTION_LEFT;
		var ai = x[0];
		var aj = x[1];
		var ei = x[2];
		var ej = x[3];
        this.world[ai][aj] = 1;
        this.world[ei][ej] = 1;
        let current_pos = [ai, aj];
        let enemy_pos = [ei, ej];
        //console.log(this.last_pos, current_pos)
        if (this.last_pos != undefined) {
            if (JSON.stringify(current_pos) == JSON.stringify(this.last_pos)) {
                console.log('locked')
                console.log(current_pos);
                console.log(this.world);
                this.cellDecision(current_pos, this.last_decision, NO_GO_VALUE);
                this.world[ei][ej] = 1;
                console.log(this.world);
                console.log('#######');
            }
        }
        this.last_pos = [ai, aj];
        let decision = undefined;
		// if strictly move away, will get stuck at wall, so introduce randomness 
		
        let possibilities = [ this.cellDecision(current_pos, ACTION_LEFT), this.cellDecision(current_pos, ACTION_RIGHT), this.cellDecision(current_pos, ACTION_UP), this.cellDecision(current_pos, ACTION_DOWN)]


		 if ( ej < aj) {
		     possibilities[ACTION_UP] += 1;
		     possibilities[ACTION_DOWN] -= 1;
		     if (JSON.stringify(this.cellDecision(current_pos, ACTION_UP, 0, true)) == JSON.stringify(enemy_pos)) {
		         possibilities[ACTION_UP] -= 20;
		     }
		     //decision =  ( AB.randomPick ( ACTION_UP,		AB.randomPick(ACTION_RIGHT,ACTION_LEFT) 	));
		 }
		 if ( ej > aj) {
		     possibilities[ACTION_DOWN] += 1;
		     possibilities[ACTION_UP] -= 1;
		     if (JSON.stringify(this.cellDecision(current_pos, ACTION_DOWN, 0, true)) == JSON.stringify(enemy_pos)) {
		         possibilities[ACTION_DOwN] -= 20;
		     }
		     //decision =  ( AB.randomPick ( ACTION_DOWN,	AB.randomPick(ACTION_RIGHT,ACTION_LEFT) 	)); 
        }
		 if ( ei < ai ) {
		     possibilities[ACTION_RIGHT] += 1;
		     possibilities[ACTION_LEFT] -= 1;
		     if (JSON.stringify(this.cellDecision(current_pos, ACTION_DOWN, 0, true)) == JSON.stringify(enemy_pos)) {
		         possibilities[ACTION_DOWN] -= 20;
		     }
		     //decision =  ( AB.randomPick ( ACTION_RIGHT,	AB.randomPick(ACTION_UP,ACTION_DOWN) 		));
		 }
		 if ( ei > ai ) {
		     possibilities[ACTION_LEFT] += 1;
		     possibilities[ACTION_RIGHT] -= 1;
		     if (JSON.stringify(this.cellDecision(current_pos, ACTION_DOWN, 0, true)) == JSON.stringify(enemy_pos)) {
		         possibilities[ACTION_DOWN] -= 20;
		     }
		     //decision =  ( AB.randomPick ( ACTION_LEFT,	AB.randomPick(ACTION_UP,ACTION_DOWN) 		));
		 }
		 console.log("LRUD::", possibilities);
		 let max = Math.max(...possibilities);
		 decisions = [0, 1, 2, 3].filter((e, i) => possibilities[e] == max);
		 decision = AB.randomPick(decisions)
		 decision = possibilities.indexOf(Math.max(...possibilities));
		 if (decision === undefined) {
		    decision = ( AB.randomIntAtoB (0,3) );
		 }
		 this.last_decision = decision;
		 return decision;
	};
	
	this.cellDecision = function (pos, action, override = undefined, pos_ol= false) {
	       let x = pos[0];
	       let y = pos[1];
	       
	       if (action == ACTION_UP) {
	           y++;
	       }
	       else if (action == ACTION_DOWN) {
	           y--;
	       }
	       else if (action == ACTION_RIGHT) {
	           x++
	       }
	       else if (action == ACTION_LEFT) {
	           x--;
	       }
	       if (pos_ol) {
	           return [x, y];
	       }
	       //console.log(':::', x, y, action, override)
	       if (override !== undefined) {
	           this.world[x][y] = override
	       }
	       return this.world[x][y];
	}

}