Skip to content

Instantly share code, notes, and snippets.

@kurokikaze
Created December 2, 2011 13:38
Show Gist options
  • Save kurokikaze/1423268 to your computer and use it in GitHub Desktop.
Save kurokikaze/1423268 to your computer and use it in GitHub Desktop.
Алгоритм Тезея для черепашки: http://www.kurilo.su/programmers/
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