123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- #include <stdio.h> //getchar()
- #include <termios.h> //终端设置
- #include <unistd.h> //延时函数
- #include <cstdlib> //标准库
- #include <ctime> //时间函数
- #include <iostream> //输入输出流
- using namespace std; //命名空间
- #define MAX_X 20
- #define MAX_Y 30
- bool flag = false;
- bool slow = false;
- bool autogame = true;
- int maze[MAX_X][MAX_Y]; //迷宫地图
- class stack_of_maze {
- private:
- //记录迷宫坐标
- struct node {
- int x;
- int y;
- char direction; //上一步路径(如何来的)
- node* next;
- };
- node* head;
- public:
- stack_of_maze() {
- //初始化
- head = NULL;
- }
- ~stack_of_maze() {
- node* p = head;
- //逐个删除
- while (head != NULL) {
- head = head->next;
- delete p;
- p = head;
- }
- }
- void push(int xx, int yy, char ddirection) {
- //定义一个新节点
- node* new_node = new node;
- //赋值
- if (new_node != NULL) {
- new_node->x = xx;
- new_node->y = yy;
- new_node->direction = ddirection;
- new_node->next = NULL;
- //判断栈是否为空,如果为空则直接把新节点赋值给栈,否则添加到栈顶
- if (head == NULL)
- head = new_node;
- else {
- new_node->next = head;
- head = new_node;
- }
- } else
- cout << "内存分配失败" << endl;
- }
- node* pop(int& xx, int& yy) {
- if (head != NULL) {
- node* p = head;
- head = head->next;
- xx = p->x;
- yy = p->y;
- delete p;
- }
- return head;
- }
- void print() {
- if (head != NULL) {
- node* p = head;
- while (p != NULL) {
- cout << " " << p->x << " " << p->y << " " << p->direction << endl;
- p = p->next;
- }
- } else
- cout << "栈为空,打印失败" << endl;
- }
- };
- void createMaze() {
- int maxway = MAX_X * MAX_Y; //最大通路
- int x, y;
- //先填充迷宫
- for (x = 0; x < MAX_X; x++)
- for (y = 0; y < MAX_Y; y++) maze[x][y] = 1;
- //随机函数种子,以时间为参数
- srand((unsigned)time(NULL));
- //随机构建迷宫通路
- for (int i = 0; i < maxway; i++) {
- x = rand() % (MAX_X - 2) + 1;
- y = rand() % (MAX_Y - 2) + 1;
- maze[x][y] = 0;
- }
- maze[1][1] = 0; //入口
- maze[MAX_X - 2][MAX_Y - 2] = 0; //出口
- maze[0][1] = 3;
- maze[MAX_X - 1][MAX_Y - 2] = 0;
- }
- void printMaze() {
- int x, y;
- //清屏,如果是windows环境使用system("cls")
- system("clear");
- //打印地图
- for (x = 0; x < MAX_X; x++) {
- for (y = 0; y < MAX_Y; y++) {
- if (maze[x][y] == 0) {
- cout << " ";
- continue;
- } //通路
- if (maze[x][y] == 1) {
- cout << "■ ";
- continue;
- } //墙
- if (maze[x][y] == 2) {
- cout << "× ";
- continue;
- } //死胡同
- if (maze[x][y] == 3) {
- cout << "↓ ";
- continue;
- } //向下走
- if (maze[x][y] == 4) {
- cout << "→ ";
- continue;
- }
- if (maze[x][y] == 5) {
- cout << "← ";
- continue;
- }
- if (maze[x][y] == 6) {
- cout << "↑ ";
- continue;
- }
- if (maze[x][y] == 7) {
- cout << "※ ";
- continue;
- } //当前站立位置
- }
- cout << endl;
- }
- //是否慢速游戏
- if (slow) {
- sleep(1); //延时函数
- }
- }
- void check(stack_of_maze& s) {
- //备份地图
- int temp[MAX_X][MAX_Y];
- for (int x = 0; x < MAX_X; x++)
- for (int y = 0; y < MAX_Y; y++) temp[x][y] = maze[x][y];
- int x = 1, y = 1; //出发点
- while (1) {
- temp[x][y] = 2;
- //向下
- if (temp[x + 1][y] == 0) {
- s.push(x, y, 'D');
- //在当前位置做一个向下的标志
- temp[x][y] = 3;
- x = x + 1;
- temp[x][y] = 7; //当前位置
- //判断是否到达出口,如果到达出口则flag标记为true,下同
- if ((x == MAX_X - 1) && (y == MAX_Y - 2)) {
- flag = true;
- return;
- } else
- continue;
- }
- //向右
- if (temp[x][y + 1] == 0) {
- s.push(x, y, 'R');
- //在当前位置做一个向右的标志
- temp[x][y] = 4;
- y = y + 1;
- temp[x][y] = 7;
- if ((x == MAX_X - 1) && (y == MAX_Y - 2)) {
- flag = true;
- return;
- } else
- continue;
- }
- //向上
- if (temp[x - 1][y] == 0) {
- s.push(x, y, 'U');
- //在当前位置做一个向上的标志
- temp[x][y] = 6;
- x = x - 1;
- temp[x][y] = 7;
- if ((x == MAX_X - 1) && (y == MAX_Y - 2)) {
- flag = true;
- return;
- } else
- continue;
- }
- //向左
- if (temp[x][y - 1] == 0) {
- s.push(x, y, 'L');
- //在当前位置做一个向右的标志
- temp[x][y] = 5;
- y = y - 1;
- temp[x][y] = 7;
- if ((x == MAX_X - 1) && (y == MAX_Y - 2)) {
- flag = true;
- return;
- } else
- continue;
- }
- //上下左右不通,则回退到起点
- if (s.pop(x, y) == NULL && temp[x - 1][y] != 0 && temp[x][y - 1] != 0 &&
- temp[x][y + 1] != 0 && temp[x + 1][y] != 0) {
- temp[0][1] = 7;
- if (temp[1][1] != 1) temp[1][1] = 2;
- return;
- }
- }
- }
- char getch() {
- char ch;
- //保存原有终端属性和新设置的终端属性
- static struct termios oldt, newt;
- //获得终端原有属性并保存在结构体oldt
- tcgetattr(STDIN_FILENO, &oldt);
- //设置新的终端属性
- newt = oldt;
- newt.c_lflag &= ~(ICANON);
- tcsetattr(STDIN_FILENO, TCSANOW, &newt);
- //取消回显
- system("stty -echo");
- ch = getchar();
- system("stty echo");
- //让终端恢复为原有的属性
- tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
- return ch;
- }
- void move() {
- int x = 1, y = 1; //出发点
- //一直游戏,直到走出
- while (1) {
- //判断输入的命令
- switch (getch()) {
- case 's':
- if (maze[x + 1][y] == 0) {
- maze[x][y] = 0;
- x = x + 1;
- maze[x][y] = 7; //当前位置
- printMaze();
- if ((x == MAX_X - 1) && (y == MAX_Y - 2)) {
- cout << "\n\n 成功走出" << endl;
- return;
- }
- }
- break;
- case 'd':
- if (maze[x][y + 1] == 0) {
- if (maze[x][y + 1] == 0) {
- maze[x][y] = 0;
- y = y + 1;
- maze[x][y] = 7;
- printMaze();
- if ((x == MAX_X - 1) && (y == MAX_Y - 2)) {
- cout << "\n\n 成功走出" << endl;
- return;
- }
- }
- }
- break;
- case 'w':
- if (maze[x - 1][y] == 0) {
- maze[x][y] = 0;
- x = x - 1;
- maze[x][y] = 7;
- printMaze();
- if ((x == MAX_X - 1) && (y == MAX_Y - 2)) {
- cout << "\n\n 成功走出" << endl;
- return;
- }
- }
- break;
- case 'a':
- if (maze[x][y - 1] == 0) {
- maze[x][y] = 0;
- y = y - 1;
- maze[x][y] = 7;
- printMaze();
- if ((x == MAX_X - 1) && (y == MAX_Y - 2)) {
- cout << "\n\n 成功走出" << endl;
- return;
- }
- }
- break;
- }
- }
- }
- void autoMove(stack_of_maze& s) {
- int x = 1, y = 1; //出发点
- while (1) {
- maze[x][y] = 2;
- //向下
- if (maze[x + 1][y] == 0) {
- s.push(x, y, 'D');
- maze[x][y] = 3; //在当前位置做一个向下的标志
- x = x + 1;
- maze[x][y] = 7; //当前位置
- if (slow) printMaze();
- if ((x == MAX_X - 1) && (y == MAX_Y - 2)) {
- s.push(x, y, '*');
- cout << "\n\n 成功走出" << endl;
- return;
- } else
- continue;
- }
- //向右
- if (maze[x][y + 1] == 0) {
- s.push(x, y, 'R');
- maze[x][y] = 4; //在当前位置做一个向右的标志
- y = y + 1;
- maze[x][y] = 7;
- if (slow) printMaze();
- if ((x == MAX_X - 1) && (y == MAX_Y - 2)) {
- s.push(x, y, '*');
- cout << "\n\n 成功走出" << endl;
- return;
- } else
- continue;
- }
- //向上
- if (maze[x - 1][y] == 0) {
- s.push(x, y, 'U');
- maze[x][y] = 6; //在当前位置做一个向上的标志
- x = x - 1;
- maze[x][y] = 7;
- if (slow) printMaze();
- if ((x == MAX_X - 1) && (y == MAX_Y - 2)) {
- s.push(x, y, '*');
- cout << "\n\n 成功走出" << endl;
- return;
- } else
- continue;
- }
- //向左
- if (maze[x][y - 1] == 0) {
- s.push(x, y, 'L');
- maze[x][y] = 5; //在当前位置做一个向右的标志
- y = y - 1;
- maze[x][y] = 7;
- if (slow) printMaze();
- if ((x == MAX_X - 1) && (y == MAX_Y - 2)) {
- s.push(x, y, '*');
- cout << "\n\n 成功走出" << endl;
- return;
- } else
- continue;
- }
- //上下左右不通,则回退
- if (s.pop(x, y) == NULL && maze[x - 1][y] != 0 && maze[x][y - 1] != 0 &&
- maze[x][y + 1] != 0 && maze[x + 1][y] != 0) {
- cout << "\n\n 没有找到合适的路径" << endl;
- maze[0][1] = 7;
- if (maze[1][1] != 1) maze[1][1] = 2;
- return;
- }
- }
- }
- void menu();
- void gamestart() {
- //初始化地图
- flag = false;
- while (!flag) {
- stack_of_maze stack;
- //创建地图
- createMaze();
- //检查地图是否创建成功
- check(stack);
- //模仿进度条
- system("clear");
- cout << "\t* loading. *" << endl;
- system("clear");
- cout << "\t* loading.. *" << endl;
- system("clear");
- cout << "\t* loading... *" << endl;
- }
- //输出当前迷宫的初始状态
- printMaze();
- cout << "\n\n 输入enter键继续" << endl;
- getchar();
- //自行游戏
- if (!autogame) {
- move();
- cout << "\n\n 输入enter键继续" << endl;
- getchar();
- menu();
- }
- //自动游戏
- else {
- stack_of_maze stack1;
- autoMove(stack1); //行走中……
- }
- printMaze(); //输出迷宫的最终状态
- cout << "\n\n 输入enter键继续" << endl;
- getchar();
- menu();
- }
- void menu() {
- system("clear");
- int num;
- cout << "\t****************************************" << endl;
- cout << "\t* *" << endl;
- cout << "\t* 1.查看路径 *" << endl;
- cout << "\t* *" << endl;
- cout << "\t* 2.自动进行 *" << endl;
- cout << "\t* *" << endl;
- cout << "\t* 3.自行游戏 *" << endl;
- cout << "\t* *" << endl;
- cout << "\t* 4.退出游戏 *" << endl;
- cout << "\t* *" << endl;
- cout << "\t****************************************" << endl;
- slow = false;
- //选择模式
- switch (getch()) {
- case '1':
- autogame = true;
- gamestart();
- break;
- case '2':
- autogame = true;
- slow = true;
- gamestart();
- break;
- case '3':
- autogame = false;
- gamestart();
- break;
- case '4':
- exit(1);
- break;
- default:
- cout << "\n\n 错误操作,输入enter返回!" << endl;
- getchar();
- menu();
- }
- getchar();
- }
- int main(int argc, char** argv) {
- menu();
- return 0;
- }
|