Code viewer for Mind: Complex Mind (clone by OmO)

// Cloned by OmO on 4 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 
// Implementatin of a* node
class AstarNode {

	// Constructor for node, each node has a value f which is h (distance from start) + g (distance to goal)
	// Parent node to be able to track back
	// Position on the grid
	// If the node was visited by the algorithm or not
	constructor(f, g, h, x, y, parent) {
		this.f = f;
		this.h = g;
		this.g = h;

		this.x = x;
		this.y = y;

		this.parent = parent;
		this.visited = false;
	};
};

function Mind() {
    
	this.turn = 1;
	this.searchSpace = 10;
	this.knownGrid = [];
	
	this.posMoves = [{x:1,y:0},{x:-1,y:0},{x:0,y:1},{x:0,y:-1}];
	this.prevMove = null;
	
	this.x = 0;
	this.y = 0;

	this.getAction = function ( x )		// x is an array of [ ai, aj, ei, ej ]
	{
		var ai = x[0];
		var aj = x[1];
		var ei = x[2];
		var ej = x[3];
		
// 		// Infinite structure
// 		if (this.turn === 1) {
// 		    x = Math.max(ai, ei);
// 		    y = Math.max(aj, ej);
		    
// 		    for (var i = 0; i < x + 1; i++) {
// 		        row = new Array(y+1).fill(undefined)
		        
// 		        if (i == 0) row.fill(1);
		        
// 		        row[0] = 1
// 		        this.knownGrid.push(row)
// 		    }   
// 		} else {
//             if ((Math.max(aj,ej) + 1) == this.knownGrid[0].length) {
//                 for (var i = 0; i < this.knownGrid.length; i++){
//                     if (i == 0) this.knownGrid[i].push(1);
//                     else this.knownGrid[i].push(undefined);
//                 }
//             }
//             if ((Math.max(ai,ei) + 1) == this.knownGrid.length) {
//                 this.knownGrid.push(new Array(this.knownGrid.length).fill(undefined));
//             }
//         }
        
//         // Marking positions
//         if (this.x != ai && this.y != aj) {
//     		this.knownGrid[ai][aj] = 0;
//         } else {
//             //mark this.prevMove as 1
//             this.knownGrid[ai + this.prevMove.x][aj + this.prevMove.y ] = 1
//         }
        
//         this.knownGrid[ei][ej] = 0;
        
//         // Debugging
// 	    console.log(ai,aj,ei,ej, this.turn)
// 		console.log(this.knownGrid)  
	    
// 	    this.x = ai;
// 	    this.y = aj;
//         this.turn++;
// 		// if strictly move away, will get stuck at wall, so introduce randomness 


            
//         var openList = []
//         var toRemove = 0
//         var added = 0
        
//         for (var dis = 1; dis <= this.searchSpace; dis++) {
            
//             if (dis == 1) {
//                 for(var i = 0; i < this.posMoves.length; i++) {
                    
//                     try  {
//                             var newPos = this.knownGrid[ai + this.posMoves[i].x][aj + this.posMoves[i].y];
//                             if(newPos == undefined || newPos == 0) {
//                                 var pos = new AstarNode()
//                                 pos.g = Math.max(Math.abs((ai + this.posMoves[i].x)-ei), Math.abs((aj + this.posMoves[i].y)-ej))
//                                 pos.f = dis + pos.g
//                                 pos.h = dis
//                                 pos.x = ai + this.posMoves[i].x
//                                 pos.y = aj + this.posMoves[i].y
//                                 pos.parent = null
                                
//                                 openList.push(pos)
//                                 console.log('dun')
//                                 added++;
//                             }
//                     } catch(e) {  }
//                 }
//             } else {
//                 while (toRemove !== 0) {
//                     parent = openList.shift();
                    
//                     for(var i = 0; i < this.posMoves.length; i++) {
                        
//                         try  {
                            
//                             search = true
                            
//                             for (var j = 0; j < openList.length; j++) {
//                                 if (parent.x + this.posMoves[j].x === openList[j].x && parent.y + this.posMoves[j].y === openList[j].y) {
//                                     search = false
//                                     break
//                                 }
//                             }
//                             if (search) {
//                                 var newPos = this.knownGrid[parent.x + this.posMoves[i].x][parent.y + this.posMoves[i].y];
//                                 if(newPos == undefined || newPos == 0) {
//                                     var pos = new AstarNode()
//                                     pos.g = Math.max(Math.abs((parent.x + this.posMoves[i].x)-ei), Math.abs((parent.y + this.posMoves[i].y)-ej))
//                                     pos.h = parent.h + 1
//                                     pos.f = pos.h + pos.g
//                                     pos.x = parent.x + this.posMoves[i].x
//                                     pos.y = parent.y + this.posMoves[i].y
//                                     pos.parent = parent
                                    
//                                     openList.push(pos)
//                                     console.log('dun')
//                                     added++;
//                                 }
//                             }
//                         } catch(e) {  }
//                     }
//                     toRemove--;
//                 }
//             }
//             toRemove = added;
//         }
//         console.log(openList, 'openList')
        
//         openList.sort(function(a, b) {
// 			var keyA = a.f;
// 			var keyB = b.f;

// 			// Compare the 2 dates
// 			return ((keyA < keyB) ? 1 : (keyA > keyB) ? -1 : 0);
// 		});
//         console.log(openList, 'openList2')
		
// 		bestChild = openList.shift()
		
// 		while(bestChild.parent != null) {
// 		    bestChild = bestChild.parent
// 		}
		
// 		this.x = ai
// 		this.y = aj
		
// 		this.prevMove = {x:bestChild.x - ai, y:bestChild.y - aj}
// 		console.log(this.prevMove, ai, aj, bestChild)
// 		if ( this.prevMove.x === 0 && this.prevMove.y === -1 ) return ACTION_UP
// 		if ( this.prevMove.x === 0 && this.prevMove.y === 1 ) return ACTION_DOWN
// 		if ( this.prevMove.x === -1 && this.prevMove.y === 0 ) return ACTION_LEFT
// 		if ( this.prevMove.x === 1 && this.prevMove.y === 0 ) return ACTION_RIGHT
            
		 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) );
	};
}