// Cloned by Walter on 18 Nov 2018 from Mind "Walter's Mind" by Walter
// Please leave this clone trail here.
const GRID_SIZE = 20;
var stateHistory = [];
var moveHistory = [];
var mapLayout = [];
var currentState = null;
var prevState = null;
function getMoves(state) {
var ax = state[0];
var ay = state[1];
return {
up: {
x: ax,
y: ay - 1
},
down: {
x: ax,
y: ay + 1
},
left: {
x: ax - 1,
y: ay
},
right: {
x: ax + 1,
y: ay
}
};
}
function getAgentPosition(state) {
var ax = state[0];
var ay = state[1];
return {
x: ax,
y: ay
};
}
function getEnemyPosition(state) {
var ex = state[2];
var ey = state[3];
return {
x: ex,
y: ey
};
}
function updateMap() { // check if we were able to move
if (moveHistory.length < 2) return;
var lastMove = moveHistory[moveHistory.length - 1];
var currentPos = getAgentPosition(currentState);
var prevPos = getAgentPosition(prevState);
if (lastMove === ACTION_UP) { // ensure we moved up
var moveBlocked = currentPos.y !== (prevPos.y - 1);
if (moveBlocked) { // check was enemy blocking square
enemyPos = getEnemyPosition(prevState);
if (enemyPos.y === (currentPos.y - 1) && enemyPos.x === currentPos.x) return;
// enemy not blocking square, should update map
mapLayout[currentPos.y - 1][currentPos.x] = 1;
}
} else if (lastMove === ACTION_DOWN) { // ensure we moved down
var moveBlocked = currentPos.y !== (prevPos.y + 1);
if (moveBlocked) { // check was enemy blocking square
enemyPos = getEnemyPosition(prevState);
if (enemyPos.y === currentPos.y + 1 && enemyPos.x === currentPos.x) return;
// enemy not blocking square, should update map
mapLayout[currentPos.y + 1][currentPos.x] = 1;
}
} else if (lastMove === ACTION_LEFT) { // ensure we moved left
var moveBlocked = currentPos.x !== (prevPos.x - 1);
if (moveBlocked) { // check was enemy blocking square
enemyPos = getEnemyPosition(prevState);
if (enemyPos.y === currentPos.y && enemyPos.x === currentPos.x - 1) return;
// enemy not blocking square, should update map
mapLayout[currentPos.y][currentPos.x - 1] = 1;
}
} else if (lastMove === ACTION_RIGHT) { // ensure we moved up
var moveBlocked = currentPos.x !== (prevPos.x + 1);
if (moveBlocked) { // check was enemy blocking square
enemyPos = getEnemyPosition(prevState);
if (enemyPos.y === currentPos.y && enemyPos.x === currentPos.x + 1) return;
// enemy not blocking square, should update map
mapLayout[currentPos.y][currentPos.x + 1] = 1;
}
}
}
function getMove(state) {
var ax = state[0];
var ay = state[1];
var ex = state[2];
var ey = state[3];
if (ex < ax) return (AB.randomPick(ACTION_UP, AB.randomPick(ACTION_RIGHT, ACTION_LEFT)));
if (ex > ax) return (AB.randomPick(ACTION_DOWN, AB.randomPick(ACTION_RIGHT, ACTION_LEFT)));
if (ey < ay) return (AB.randomPick(ACTION_RIGHT, AB.randomPick(ACTION_UP, ACTION_DOWN)));
if (ey > ay) return (AB.randomPick(ACTION_LEFT, AB.randomPick(ACTION_UP, ACTION_DOWN)));
}
function Mind() {
this.newRun = function () {
// init mapLayout
// 0 for free square, 1 for blocked
for (i = 0; i < GRID_SIZE; i++) {
mapLayout.push([]);
for (j = 0; j < GRID_SIZE; j++) {
if (i === 0 || i === 19 || j === 0 || j === 19) mapLayout[i].push(1); // wall
else mapLayout[i].push(0);
}
}
};
this.getAction = function (state) { // state: [ax, ay, ex, ey]
prevState = currentState;
currentState = state;
stateHistory.push(state);
updateMap();
var move = getMove(state);
console.log(move, mapLayout);
moveHistory.push(move);
return (move);
};
this.endRun = function () {};
}