// I wrote wander.fs but then realized that I had no way to compile it // to a standalone executable. #include #include #include #include enum { width = 80, height = 23 }; char board[height][width]; // Write to standard out, without using stdio void ws0(char *s) { write(1, s, strlen(s)); } void print_board() { ws0("\033[H"); // home cursor char *p = board[0]; for (int i = 0; i != height; i++) { write(1, p, width); ws0("\n"); p += width; } ws0("[hjklq]"); } int dude_x = 40, dude_y = 12; void init_board() { srand(time(0)); memset(board[0], ' ', width * height); for (int i = 0; i != 512; i++) board[0][rand() % (width * height)] = '#'; board[dude_y][dude_x] = '@'; } int clip(int min, int val, int max) { return val < min ? min : val > max ? max : val; } void try_move(int dx, int dy) { int x = clip(0, dude_x + dx, width - 1), y = clip(0, dude_y + dy, height - 1); if (' ' == board[y][x]) { board[dude_y][dude_x] = ' '; dude_x = x; dude_y = y; board[y][x] = '@'; } } int act(int c) { switch(c) { case 'h': try_move(-1, 0); break; case 'l': try_move(1, 0); break; case 'j': try_move(0, 1); break; case 'k': try_move(0, -1); break; case 'q': case -1: return 0; } return 1; } int rchar() { char buf; if (!read(0, &buf, 1)) return -1; // EOF return buf; } void wander() { ws0("\033[2J"); // clear screen init_board(); for (;;) { print_board(); if (!act(rchar())) return; } } int main() { system("stty cbreak -echo"); wander(); system("stty -cbreak echo"); return 0; }