Created
October 20, 2018 13:51
-
-
Save Mobilpadde/c7df1a0b778eb6e06aa7dd6df19f3ee5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
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