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

// Cloned by Pranay Manjunath on 12 Nov 2021 from Mind "Complex Mind (clone by Dheeraj Putta)" by Dheeraj Putta 
// Please leave this clone trail here.
 
const depth=3;function Queue(){this.items=[],this.enqueue=function(e){this.items.push(e)},this.dequeue=function(){var e=this.items[0];return this.items.shift(),e}}function Node(e){this.data=e,this.parent=null,this.children=[],this.score=null}function Tree(e){this.root=new Node(e),this.populateTree=function(){var e=new Queue;this.addChildren(this.root),e.enqueue(this.root),currentNode=e.dequeue();for(var r=0;currentNode&&r<depth-1;){for(var t=0;t<currentNode.children.length;t++)this.addChildren(currentNode.children[t]),e.enqueue(currentNode.children[t]);currentNode=e.dequeue(),r++}},this.addChildren=function(e){for(var r=0;r<e.data.neighbors.length;r++){var t=new Node(e.data.neighbors[r]);t.parent=e,e.children.push(t)}},this.miniMax=function(e,r){!function t(n,i){for(var o=0;o<n.children.length;o++)t(n.children[o],i+1);0==n.children.length?n.score=heuristicMind(n.data.i,n.data.j,e,r):n.score=i%2==0?Math.max(...n.children.map(e=>e.score)):Math.min(...n.children.map(e=>e.score))}(this.root,0)},this.getBest=function(){for(var e=-1,r=null,t=0;t<this.root.children.length;t++)e<this.root.children[t].score&&(r=this.root.children[t],e=this.root.children[t].score);return r}}function heuristicMind(e,r,t,n){if(trapMove(e,r))return 0;var i=new THREE.Vector3(e,0,r),o=new THREE.Vector3(t,0,n);return i.manhattanDistanceTo(o)}function removeFromArray(e,r){e.splice(e.indexOf(r),1)}function occupiedMind(e,r){return GRID[e][r].type==GRID_WALL||GRID[e][r].type==GRID_MAZE}function trapMove(e,r){var t=[];return t.push(e<gridsize-1&&occupiedMind(e+1,r)),t.push(e>0&&occupiedMind(e-1,r)),t.push(r<gridsize-1&&occupiedMind(e,r+1)),t.push(r>0&&occupiedMind(e,r-1)),t.reduce((e,r)=>e+r,0)>=3}AB.mind.getAction=function(e){var r=e[0],t=e[1],n=e[2],i=e[3],o=new Tree(GRID[r][t]);o.populateTree(),o.miniMax(n,i);var u=o.getBest().data;if(console.log(o),console.log("Agent move: ",u),!(trapMove(u.i,u.j)||AB.step>0&&agentLastVisited.reduce((e,r)=>e&&[u.i,u.j].includes(r),!0)))return u;for(var c=[...Array(GRID[r][t].neighbors.length).keys()];;){var h=AB.randomElementOfArray(c);if(!trapMove((u=GRID[r][t].neighbors[h]).i,u.j))return u;removeFromArray(c,h)}};