// Personal notes
//
// const ACTION_LEFT = 0;
// const ACTION_RIGHT = 1;
// const ACTION_UP = 2;
// const ACTION_DOWN = 3;
// const ACTION_STAYSTILL = 4;
//
// Grid = 20 * 20, but only 18 * 18 is playable
// state = (x1, y1, x2, y2)
function Mind() {
// Creating an x, y grid with each position having a counter
// Example: x, y : 0
// 0 being the counter for the number of moves
// moves starts at (1, 1) and ends at (18, 18)
var moves = {};
var x = 1;
var y = 1;
while (y < 19) {
while (x < 19) {
let tmp_xy = [x, y];
moves[tmp_xy] = 0;
x++;
}
x = 1; // Reset x
y++;
}
// moves is now properly initialized
function newMove(agent) {
// Generate new move
var new_move = Math.floor(Math.random() * 5);
var original_agent = agent;
// Check whether new move (coordinate) is valid
if (new_move === 0) { // LEFT: (x-1, y)
agent = [agent[0]-1, agent[1]];
} else if (new_move === 1) { // RIGHT: (x+1, y)
agent = [agent[0]+1, agent[1]];
} else if(new_move === 2) { // UP: (x, y+1)
agent = [agent[0], agent[1]+1];
} else if (new_move === 3) { // DOWN: (x, y-1)
agent = [agent[0], agent[1-1]];
}
try {
xy_move_count = moves[agent]; // moves[agent] is the counter of agent coordinate
} catch(e) {
if (e instanceof TypeError) {
return newMove(original_agent); // If coordinate out of range, call newMove again
}
}
if (xy_move_count < 3) { // This integer specifies maximum allowed duplicate moves
moves[agent]++;
return new_move;
} else {
return newMove(original_agent);
}
}
this.newRun = function() {
};
this.getAction = function (state) {
let agent = [state[0], state[1]];
let [x1, y1] = agent; // agent = (x1, y1)
let enemy = [state[2], state[3]];
let [x2, y2] = enemy; // enemy = (x2, y2)
return newMove(agent);
};
this.endRun = function() {
};
}