Created
December 2, 2011 13:38
-
-
Save kurokikaze/1423268 to your computer and use it in GitHub Desktop.
Алгоритм Тезея для черепашки: http://www.kurilo.su/programmers/
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 dirs = {'north':0,'east':1,'south':2,'west':3}; | |
var names = ['north','east','south','west']; | |
var obs = {'north':north,'south':south,'west':west,'east':east}; | |
result='east'; | |
if (!direction) direction = 'east'; | |
var left = function() { | |
return names[(dirs[direction] + 3) % 4]; | |
}; | |
var right = function() { | |
return names[(dirs[direction] + 1) % 4]; | |
}; | |
var back = function() { | |
return names[(dirs[direction] + 2) % 4]; | |
}; | |
var forward = function() { return direction }; | |
if (obs[left()] == 1 && obs[forward()] != 1) { // путь свободен | |
result = forward(); | |
} else { | |
if (obs[left()] != 1) { // потеряли стену | |
result = left(); | |
} else { // впереди препятствие | |
if (obs[right()] != 1) { // справа есть куда ехать | |
result = right(); | |
} else { | |
result = back(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment