Created
December 10, 2014 14:40
-
-
Save axfcampos/764eeaa650c43d27c9b5 to your computer and use it in GitHub Desktop.
This file contains 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
var prev_distance = -1; | |
var prev_direction = -1; | |
function tick(distance) { | |
var directions = ['up', 'down', 'left', 'right']; | |
var direction; | |
if (prev_distance === -1) prev_distance = distance; | |
if (prev_direction === -1) prev_direction = 'up'; | |
if (distance === 1){ | |
direction = 'up'; | |
}else{ | |
if (prev_distance < distance) { | |
if (prev_direction === 'up') direction = 'left'; | |
if (prev_direction === 'down') direction = 'right'; | |
if (prev_direction === 'left') direction = 'down'; | |
if (prev_direction === 'right') direction = 'up'; | |
}else{ | |
direction = prev_direction; | |
} | |
} | |
prev_distance = distance; | |
prev_direction = direction; | |
return direction; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment