Skip to content

Instantly share code, notes, and snippets.

@lessandro
Last active December 11, 2015 01:19
Show Gist options
  • Select an option

  • Save lessandro/4522500 to your computer and use it in GitHub Desktop.

Select an option

Save lessandro/4522500 to your computer and use it in GitHub Desktop.
GridC bot - resource seeker
// GridC to GridLang compiler
// https://github.com/lessandro/gridc
void eat(dir) {
if (ffi(@LOOK, dir) == @CELL_ROBOT) ffi(@PUNCH, dir);
if (ffi(@LOOK, dir) == @CELL_RESOURCE) ffi(@PULL, dir);
}
void eat_all() {
eat(@NORTH);
eat(@SOUTH);
eat(@WEST);
eat(@EAST);
}
void move_randomly() {
n = rand(3);
dir = @NORTH;
if (n == 0) { dir = @SOUTH; }
if (n == 1) { dir = @WEST; }
if (n == 2) { dir = @EAST; }
ffi(@MOVE, dir);
}
int seek(dx, dy, dir) {
if (ffi(@SCAN, dx, dy) == @CELL_RESOURCE) {
ffi(@MOVE, dir);
return 1;
}
return 0;
}
void move_bot() {
if (rand(6) == 0) {
move_randomly();
return;
}
d = 2;
while (d < 5) {
n = 0;
while (n < d) {
m = d - n;
if (seek(n, 0 - m, @NORTH)) { return; }
if (seek(m, n, @EAST)) { return; }
if (seek(0 - n, m, @SOUTH)) { return; }
if (seek(0 - m, 0 - n, @WEST)) { return; }
n = n + 1;
}
d = d + 1;
}
move_randomly();
}
void main() {
while (1) {
eat_all();
move_bot();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment