#include <Windows.h>
#include <conio.h>
int map[21 * 21] = { 0 }, x = 0, y = 1;
void dig(int v) {
int a[4] = { 2,-2,2 * 21,-2 * 21 }, i = 0, j;
for (; j = rand() % 4, i < 4; i++) i - j && (a[i] ^= a[j] ^= a[i] ^= a[j]);
for (map[v] = 1, i = 0; i < 4; i++) {
if (v + a[i] < 0 || 21 * 21 < v + a[i] || map[v + a[i]])continue;
if ((a[i] == 2 || a[i] == -2) && v / 21 - (a[i] + v) / 21)continue;
map[v + a[i] / 2] = 1, dig(v + a[i]);
}
}
int main() {
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &(CONSOLE_CURSOR_INFO) { 25, 0 });
srand((unsigned)malloc(!system("mode con:cols=42 lines=21")));
dig(21 + 1), map[21] = map[21 * 21 - 21 - 1] = 1;
for (char c; c = _getch(), x<20;) {
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), (COORD) { 0, 0 });
x += (c == 'd' && map[y * 21 - (21 - x) + 21 + 1] == 1 ? 1 : (c == 'a' && map[y * 21 - (21 - x) + 21 - 1] == 1 ? -1 : 0));
y += (c == 'w' && map[y * 21 - (21 - x) + 21 - 21] == 1 ? -1 : (c == 's' && map[y * 21 - (21 - x) + 21*2] == 1 ? 1 : 0));
for (int i = 0; i < 21 * 21; i++)
_cputs((map[i] ? " " : "[]") == " " && ((y*21)-(21-x)+21) == i ? "<>" : (map[i] ? " " : "[]"));
}
}
(未经过同意,禁止转载)