Skip to content

Instantly share code, notes, and snippets.

@Mobilpadde
Created October 20, 2018 13:51
Show Gist options
  • Save Mobilpadde/c7df1a0b778eb6e06aa7dd6df19f3ee5 to your computer and use it in GitHub Desktop.
Save Mobilpadde/c7df1a0b778eb6e06aa7dd6df19f3ee5 to your computer and use it in GitHub Desktop.
({
Next(snake: Snake, snakeApi: SnakeApi): Action {
return function(views) {
const directions = ['up', 'down', 'right', 'left'];
const treatMatch = views.find(v => v.contains == 'treat');
if (treatMatch) {
return treatMatch;
}
let possibleActions = [...directions];
const wallMatch = views.findIndex(v => v.contains === 'wall');
if (wallMatch !== -1) {
possibleActions.splice(wallMatch, 1);
}
const body = snake.getBody();
const sViews = snake.views();
const dirLeft = Object.entries(sViews)
.filter((e) => {
return body.filter((v) => v.toString() === e[1].toString()).length > 0;
})
.map((e) => e[0]);
possibleActions = possibleActions.filter((p) => dirLeft.indexOf(p) < 0);
const rnd = ~~(Math.random() * possibleActions.length);
return views.find(v => v.direction == possibleActions[rnd]);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment