//迷宫 #include <iostream> #include <cstdlib> #include <ctime> #include <windows.h> #include <conio.h> using namespace std; /* ================= 参数 ================= */ const int ROWS = 25; // 奇数(必须) const int COLS = 61; // 奇数(必须) char maze[ROWS][COLS]; int playerX = 1, playerY = 1; int exitX = COLS - 2, exitY = ROWS - 2; bool gameOver = false; bool win = false; int steps = 0; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); /* ================= 颜色 ================= */ #define WALL_COLOR FOREGROUND_BLUE #define ROAD_COLOR FOREGROUND_GREEN #define PLAYER_COLOR (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY) #define EXIT_COLOR (FOREGROUND_RED | FOREGROUND_INTENSITY) /* ================= 方向 ================= */ const int dirs[4][2] = { {-2, 0}, {2, 0}, {0, -2}, {0, 2} }; /* ================= 工具 ================= */ void swap(int &a, int &b) { int t = a; a = b; b = t; } void randomOrder(int a[4]) { for (int i = 0; i < 4; i++) { int r = rand() % 4; swap(a[i], a[r]); } } /* ================= DFS 迷宫(100% 有解) ================= */ void dfs(int y, int x) { maze[y][x] = ' '; int order[4] = {0, 1, 2, 3}; randomOrder(order); for (int i = 0; i < 4; i++) { int ny = y + dirs[order[i]][0]; int nx = x + dirs[order[i]][1]; if (ny > 0 && ny < ROWS - 1 && nx > 0 && nx < COLS - 1 && maze[ny][nx] == '#') { maze[y + dirs[order[i]][0] / 2] [x + dirs[order[i]][1] / 2] = ' '; dfs(ny, nx); } } } void generateMaze() { srand((unsigned)time(NULL)); /* 全部围墙 */ for (int y = 0; y < ROWS; y++) for (int x = 0; x < COLS; x++) maze[y][x] = '#'; /* DFS 生成(保证连通) */ dfs(1, 1); /* 出口 */ maze[exitY][exitX] = 'E'; /* 玩家 */ playerY = 1; playerX = 1; maze[playerY][playerX] = 'P'; } /* ================= 隐藏光标 ================= */ void hideCursor() { CONSOLE_CURSOR_INFO ci; GetConsoleCursorInfo(hOut, &ci); ci.bVisible = FALSE; SetConsoleCursorInfo(hOut, &ci); } /* ================= 绘制 ================= */ void draw() { COORD pos = {0, 0}; SetConsoleCursorPosition(hOut, pos); for (int y = 0; y < ROWS; y++) { for (int x = 0; x < COLS; x++) { char c = maze[y][x]; if (c == '#') SetConsoleTextAttribute(hOut, WALL_COLOR); else if (c == 'P') SetConsoleTextAttribute(hOut, PLAYER_COLOR); else if (c == 'E') SetConsoleTextAttribute(hOut, EXIT_COLOR); else SetConsoleTextAttribute(hOut, ROAD_COLOR); putchar(c); } putchar('\n'); } SetConsoleTextAttribute(hOut, ROAD_COLOR); cout << "\n步数:" << steps << endl; cout << "W/A/S/D 移动 | Q 退出\n"; } /* ================= 输入 ================= */ void input() { if (!_kbhit()) return; char ch = _getch(); /* Q 退出(不算通关) */ if (ch == 'q' || ch == 'Q') { gameOver = true; win = false; return; } int nx = playerX, ny = playerY; if (ch == 'w' || ch == 'W') ny--; if (ch == 's' || ch == 'S') ny++; if (ch == 'a' || ch == 'A') nx--; if (ch == 'd' || ch == 'D') nx++; if (nx >= 0 && ny >= 0 && nx < COLS && ny < ROWS && maze[ny][nx] != '#') { maze[playerY][playerX] = ' '; playerX = nx; playerY = ny; maze[playerY][playerX] = 'P'; steps++; if (playerX == exitX && playerY == exitY) { win = true; gameOver = true; } } } /* ================= 主函数 ================= */ int main() { hideCursor(); generateMaze(); /* 操作说明 */ system("cls"); cout << "====== 迷宫游戏 ======\n"; cout << "W/A/S/D :移动\n"; cout << "Q :退出\n"; cout << "E :终点\n"; system("pause"); while (!gameOver) { draw(); input(); Sleep(30); } system("cls"); if (win) cout << "恭喜通关!\n"; else cout << "游戏结束(未通关)\n"; cout << "总步数:" << steps << endl; system("pause"); return 0; }
Note.ms
/WJCforgame