Skip to content

Instantly share code, notes, and snippets.

@Guria
Created June 13, 2017 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Guria/3da812d19ad121cc240c7b9cd2c187f8 to your computer and use it in GitHub Desktop.
Save Guria/3da812d19ad121cc240c7b9cd2c187f8 to your computer and use it in GitHub Desktop.
const REVISIT_PENALTY = 1.5
const DEADEND_PENALTY = 100000
const directions = ['left', 'right', 'up', 'down']
const visitedScore = {}
const getNextCoords = (x, y) => ({ left: [x-1, y], right: [x+1, y], up: [x, y-1], down: [x, y+1] })
const getScore = (x, y) => visitedScore[`${x}:${y}`] || 0
const increaseScore = (x, y, n = 1) => visitedScore[`${x}:${y}`] = getScore(x, y) * REVISIT_PENALTY + n
function strategy(move, canMove, x, y, maze) {
const nextCoords = getNextCoords(x, y)
const availableDirections = directions
.sort(() => .5 - Math.random()) // ВАЖНО: рандомизуем исходный список
.filter(canMove)
.filter(dir => getScore(...nextCoords[dir]) < DEADEND_PENALTY) // отсекаем посещённые тупики
.sort((a, b) => getScore(...nextCoords[a]) - getScore(...nextCoords[b]))
increaseScore(x, y, availableDirections.length === 1 ? DEADEND_PENALTY : 1)
move(availableDirections[0])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment