当前位置:首页 > 通信资讯 > 正文

走迷宫小游戏(c++迷宫游戏程序设计)

介绍

本程序是根据广度优先遍历算法的思想设计的一款迷宫游戏,游戏设计了两种模式一种自动游戏模式,一种手动模式。因为项目在 Linux 开发,需要在 Windows 开发的,请查看源代码中需要修改地方的备注。

截图

走迷宫小游戏(c++迷宫游戏程序设计)

走迷宫小游戏(c++迷宫游戏程序设计)

走迷宫小游戏(c++迷宫游戏程序设计)

代码

  1. #include <iostream>
  2. #include <cstdlib> //标准库
  3. #include <unistd.h> //延时函数
  4. #include <stdio.h> //getchar
  5. #include <ctime>
  6. #include <termios.h> //终端设置
  7. #define MAX_X 20
  8. #define MAX_Y 30
  9. bool flag = false;
  10. bool slow = false;
  11. bool autogame = true;
  12. using namespace std;
  13. int maze[MAX_X][MAX_Y]; //迷宫
  14. //路线栈
  15. class stack_of_maze{
  16. private:
  17. //记录迷宫坐标
  18. struct node
  19. {
  20. int x;
  21. int y;
  22. char direction; //上一步路径(如何来的)
  23. node* next;
  24. };
  25. node* head;
  26. public:
  27. stack_of_maze(){
  28. head = NULL;
  29. }
  30. ~stack_of_maze(){
  31. node* p = head;
  32. while(head!=NULL){
  33. head = head->next;
  34. delete p;
  35. p = head;
  36. }
  37. }
  38. //压栈
  39. void push(int xx,int yy,char ddirection){
  40. node* new_node = new node;
  41. if(new_node!=NULL){
  42. new_node->x = xx;
  43. new_node->y = yy;
  44. new_node->direction = ddirection;
  45. new_node->next = NULL;
  46. if(head==NULL)
  47. head = new_node;
  48. else{
  49. new_node->next = head;
  50. head = new_node;
  51. }
  52. }
  53. else
  54. cout<<"内存分配失败"<<endl;
  55. }
  56. //出栈
  57. node* pop(int& xx,int& yy){
  58. if(head!=NULL){
  59. node* p = head;
  60. head = head->next;
  61. xx = p->x;
  62. yy = p->y;
  63. delete p;
  64. }
  65. return head;
  66. }
  67. void print(){
  68. if(head!=NULL){
  69. node* p = head;
  70. while(p!=NULL){
  71. cout<<" "<<p->x<<" "<<p->y<<" "<<p->direction<<endl;
  72. p = p->next;
  73. }
  74. }
  75. else
  76. cout<<"栈为空,打印失败"<<endl;
  77. }
  78. };
  79. //创建迷宫
  80. void createMaze(){
  81. int maxway = MAX_X * MAX_Y; //最大通路
  82. int x,y;
  83. for(x=0;x<MAX_X;x++)
  84. for(y=0;y<MAX_Y;y++)
  85. maze[x][y] = 1; //先填充迷宫
  86. srand((unsigned)time(NULL)); //随机函数种子,以时间为参数
  87. for(int i=0;i<maxway;i++) //随机构建迷宫通路
  88. {
  89. x = rand() % (MAX_X-2) + 1;
  90. y = rand() % (MAX_Y-2) + 1;
  91. maze[x][y] = 0;
  92. }
  93. maze[1][1] = 0; //入口
  94. maze[MAX_X-2][MAX_Y-2] = 0; //出口
  95. maze[0][1] = 3;
  96. maze[MAX_X-1][MAX_Y-2] = 0;
  97. }
  98. //输出迷宫
  99. void printMaze(){
  100. int x,y;
  101. system("clear"); //windows下使用system("cls")
  102. //cout<<endl;
  103. for(x=0;x<MAX_X;x++)
  104. {
  105. for(y=0;y<MAX_Y;y++)
  106. {
  107. if(maze[x][y]==0){cout<<" ";continue;} //通路
  108. if(maze[x][y]==1){cout<<"■";continue;} //墙
  109. if(maze[x][y]==2){cout<<"×";continue;} //死胡同
  110. if(maze[x][y]==3){cout<<"↓";continue;} //向下走
  111. if(maze[x][y]==4){cout<<"→";continue;}
  112. if(maze[x][y]==5){cout<<"←";continue;}
  113. if(maze[x][y]==6){cout<<"↑";continue;}
  114. if(maze[x][y]==7){cout<<"※";continue;} //当前站立位置
  115. }
  116. cout<<endl;
  117. }
  118. if(slow){
  119. sleep(1); //延时函数
  120. }
  121. }
  122. void check(stack_of_maze &s){
  123. int temp[MAX_X][MAX_Y];
  124. for(int x=0;x<MAX_X;x++)
  125. for(int y=0;y<MAX_Y;y++)
  126. temp[x][y] = maze[x][y];
  127. int x=1,y=1; //出发点
  128. while(1){
  129. temp[x][y] = 2;
  130. //向下
  131. if(temp[x+1][y]==0){
  132. s.push(x,y,'D');
  133. temp[x][y] = 3; //在当前位置做一个向下的标志
  134. x = x + 1;
  135. temp[x][y] = 7; //当前位置
  136. if((x==MAX_X-1)&&(y==MAX_Y-2)){
  137. flag = true;
  138. return;
  139. }
  140. else
  141. continue;
  142. }
  143. //向右
  144. if(temp[x][y+1]==0){
  145. s.push(x,y,'R');
  146. temp[x][y] = 4; //在当前位置做一个向右的标志
  147. y = y + 1;
  148. temp[x][y] = 7;
  149. if((x==MAX_X-1)&&(y==MAX_Y-2)){
  150. flag = true;
  151. return;
  152. }
  153. else
  154. continue;
  155. }
  156. //向上
  157. if(temp[x-1][y]==0){
  158. s.push(x,y,'U');
  159. temp[x][y] = 6; //在当前位置做一个向上的标志
  160. x = x - 1;
  161. temp[x][y] = 7;
  162. if((x==MAX_X-1)&&(y==MAX_Y-2)){
  163. flag = true;
  164. return;
  165. }
  166. else
  167. continue;
  168. }
  169. //向左
  170. if(temp[x][y-1]==0){
  171. s.push(x,y,'L');
  172. temp[x][y] = 5; //在当前位置做一个向右的标志
  173. y = y - 1;
  174. temp[x][y] = 7;
  175. if((x==MAX_X-1)&&(y==MAX_Y-2)){
  176. flag = true;
  177. return;
  178. }
  179. else
  180. continue;
  181. }
  182. //上下左右不通,则回退
  183. 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){
  184. temp[0][1] = 7;
  185. if(temp[1][1]!=1)
  186. temp[1][1] = 2;
  187. return;
  188. }
  189. }
  190. }
  191. //输入,windows下可以使用#incldue<conio.h>替代此函数
  192. char getch(){
  193. char ch;
  194. static struct termios oldt, newt; //保存原有终端属性和新设置的终端属性
  195. tcgetattr( STDIN_FILENO, &oldt); //获得终端原有属性并保存在结构体oldflag
  196. //设置新的终端属性
  197. newt = oldt;
  198. newt.c_lflag &= ~(ICANON);
  199. tcsetattr( STDIN_FILENO, TCSANOW, &newt);
  200. //取消回显
  201. system("stty -echo");
  202. ch = getchar();
  203. system("stty echo");
  204. tcsetattr( STDIN_FILENO, TCSANOW, &oldt); //让终端恢复为原有的属性
  205. return ch;
  206. }
  207. void move(){
  208. int x=1,y=1; //出发点
  209. while(1){
  210. switch(getch()){
  211. case 's':
  212. if(maze[x+1][y]==0){
  213. maze[x][y] = 0;
  214. x = x + 1;
  215. maze[x][y] = 7; //当前位置
  216. printMaze();
  217. if((x==MAX_X-1)&&(y==MAX_Y-2)){
  218. cout<<"\n\n 成功走出"<<endl;
  219. return;
  220. }
  221. }
  222. break;
  223. case 'd':
  224. if(maze[x][y+1]==0){
  225. if(maze[x][y+1]==0){
  226. maze[x][y] = 0;
  227. y = y + 1;
  228. maze[x][y] = 7;
  229. printMaze();
  230. if((x==MAX_X-1)&&(y==MAX_Y-2)){
  231. cout<<"\n\n 成功走出"<<endl;
  232. return;
  233. }
  234. }
  235. }
  236. break;
  237. case 'w':
  238. if(maze[x-1][y]==0){
  239. maze[x][y] = 0;
  240. x = x - 1;
  241. maze[x][y] = 7;
  242. printMaze();
  243. if((x==MAX_X-1)&&(y==MAX_Y-2)){
  244. cout<<"\n\n 成功走出"<<endl;
  245. return;
  246. }
  247. }
  248. break;
  249. case 'a':
  250. if(maze[x][y-1]==0){
  251. maze[x][y] = 0;
  252. y = y - 1;
  253. maze[x][y] = 7;
  254. printMaze();
  255. if((x==MAX_X-1)&&(y==MAX_Y-2)){
  256. cout<<"\n\n 成功走出"<<endl;
  257. return;
  258. }
  259. }
  260. break;
  261. }
  262. }
  263. }
  264. void autoMove(stack_of_maze &s){
  265. int x=1,y=1; //出发点
  266. while(1){
  267. maze[x][y] = 2;
  268. //向下
  269. if(maze[x+1][y]==0){
  270. s.push(x,y,'D');
  271. maze[x][y] = 3; //在当前位置做一个向下的标志
  272. x = x + 1;
  273. maze[x][y] = 7; //当前位置
  274. if(slow)
  275. printMaze();
  276. if((x==MAX_X-1)&&(y==MAX_Y-2)){
  277. s.push(x,y,'*');
  278. cout<<"\n\n 成功走出"<<endl;
  279. return;
  280. }
  281. else
  282. continue;
  283. }
  284. //向右
  285. if(maze[x][y+1]==0){
  286. s.push(x,y,'R');
  287. maze[x][y] = 4; //在当前位置做一个向右的标志
  288. y = y + 1;
  289. maze[x][y] = 7;
  290. if(slow)
  291. printMaze();
  292. if((x==MAX_X-1)&&(y==MAX_Y-2)){
  293. s.push(x,y,'*');
  294. cout<<"\n\n 成功走出"<<endl;
  295. return;
  296. }
  297. else
  298. continue;
  299. }
  300. //向上
  301. if(maze[x-1][y]==0){
  302. s.push(x,y,'U');
  303. maze[x][y] = 6; //在当前位置做一个向上的标志
  304. x = x - 1;
  305. maze[x][y] = 7;
  306. if(slow)
  307. printMaze();
  308. if((x==MAX_X-1)&&(y==MAX_Y-2)){
  309. s.push(x,y,'*');
  310. cout<<"\n\n 成功走出"<<endl;
  311. return;
  312. }
  313. else
  314. continue;
  315. }
  316. //向左
  317. if(maze[x][y-1]==0){
  318. s.push(x,y,'L');
  319. maze[x][y] = 5; //在当前位置做一个向右的标志
  320. y = y - 1;
  321. maze[x][y] = 7;
  322. if(slow)
  323. printMaze();
  324. if((x==MAX_X-1)&&(y==MAX_Y-2)){
  325. s.push(x,y,'*');
  326. cout<<"\n\n 成功走出"<<endl;
  327. return;
  328. }
  329. else
  330. continue;
  331. }
  332. //上下左右不通,则回退
  333. 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){
  334. cout<<"\n\n 没有找到合适的路径"<<endl;
  335. maze[0][1] = 7;
  336. if(maze[1][1]!=1)
  337. maze[1][1] = 2;
  338. return;
  339. }
  340. }
  341. }
  342. void menu();
  343. void gamestart(){
  344. flag = false;
  345. while(!flag){
  346. stack_of_maze stack; //定义一个栈的对象,用来记录行走路线
  347. createMaze();
  348. check(stack);
  349. system("clear");
  350. cout<<"\t* loading. *"<<endl;
  351. system("clear");
  352. cout<<"\t* loading.. *"<<endl;
  353. system("clear");
  354. cout<<"\t* loading... *"<<endl;
  355. }
  356. printMaze(); //输出当前迷宫的初始状态
  357. cout<<"\n\n 输入enter键继续"<<endl;
  358. getchar();
  359. if(!autogame){
  360. move();
  361. cout<<"\n\n 输入enter键继续"<<endl;
  362. getchar();
  363. menu();
  364. }
  365. else{
  366. stack_of_maze stack1;
  367. autoMove(stack1); //行走中……
  368. }
  369. printMaze(); //输出迷宫的最终状态
  370. cout<<"\n\n 输入enter键继续"<<endl;
  371. getchar();
  372. menu();
  373. }
  374. void menu(){
  375. system("clear");
  376. int num;
  377. cout<<"\t****************************************"<<endl;
  378. cout<<"\t* *"<<endl;
  379. cout<<"\t* 1.查看路径 *"<<endl;
  380. cout<<"\t* *"<<endl;
  381. cout<<"\t* 2.自动进行 *"<<endl;
  382. cout<<"\t* *"<<endl;
  383. cout<<"\t* 3.自行游戏 *"<<endl;
  384. cout<<"\t* *"<<endl;
  385. cout<<"\t* 4.退出游戏 *"<<endl;
  386. cout<<"\t* *"<<endl;
  387. cout<<"\t****************************************"<<endl;
  388. slow = false;
  389. switch(getch()){
  390. case '1':
  391. autogame = true;
  392. gamestart();break;
  393. case '2':
  394. autogame = true;
  395. slow = true;
  396. gamestart();
  397. break;
  398. case '3':
  399. autogame = false;
  400. gamestart();
  401. break;
  402. case '4':
  403. exit(1);break;
  404. default:
  405. cout<<"\n\n 错误操作,输入enter返回!"<<endl;
  406. getchar();
  407. menu();
  408. }
  409. getchar();
  410. }
  411. int main(int argc,char** argv){
  412. menu();
  413. return 0;
  414. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/weixin_43675051/article/details/85262289

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。