找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4353|回复: 0
打印 上一主题 下一主题
收起左侧

用C语言写的走迷宫的代码

[复制链接]
跳转到指定楼层
楼主
ID:598748 发表于 2019-8-10 11:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. //走迷宫 普通走法
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <windows.h>
  5. #include <time.h>
  6. #define Height 25 //迷宫的高度,必须为奇数
  7. #define Width 25 //迷宫的宽度,必须为奇数
  8. #define Wall 1
  9. #define Road 0
  10. #define Start 2
  11. #define End 3
  12. #define Esc 5
  13. #define Up 1
  14. #define Down 2
  15. #define Left 3
  16. #define Right 4
  17. int map[Height+2][Width+2];
  18. void gotoxy(int x,int y) //移动坐标
  19. {
  20.     COORD coord;
  21.     coord.X=x;
  22.     coord.Y=y;
  23.     SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
  24. }
  25. void hidden()//隐藏光标
  26. {
  27.     HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  28.     CONSOLE_CURSOR_INFO cci;
  29.     GetConsoleCursorInfo(hOut,&cci);
  30.     cci.bVisible=0;//赋1为显示,赋0为隐藏
  31.     SetConsoleCursorInfo(hOut,&cci);
  32. }
  33. void create(int x,int y) //随机生成迷宫
  34. {
  35.     int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向
  36.     int i,j,t;
  37.         //将方向打乱
  38.     for(i=0;i<4;i++)
  39.     {
  40.         j=rand()%4;
  41.         t=c[i][0];c[i][0]=c[j][0];c[j][0]=t;
  42.         t=c[i][1];c[i][1]=c[j][1];c[j][1]=t;
  43.     }
  44.     map[x][y]=Road;
  45.     for(i=0;i<4;i++)
  46.     if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall)
  47.     {
  48.         map[x+c[i][0]][y+c[i][1]]=Road;
  49.         create(x+2*c[i][0],y+2*c[i][1]);
  50.     }
  51. }
  52. int get_key() //接收按键
  53. {
  54.     char c;
  55.     while(c=getch())
  56.     {
  57.         if(c==27) return Esc; //Esc
  58.         if(c!=-32)continue;
  59.         c=getch();
  60.         if(c==72) return Up; //上
  61.         if(c==80) return Down; //下
  62.         if(c==75) return Left; //左
  63.         if(c==77) return Right; //右
  64.     }
  65.     return 0;
  66. }
  67. void paint(int x,int y) //画迷宫
  68. {
  69.     gotoxy(2*y-2,x-1);
  70.     switch(map[x][y])
  71.     {
  72.         case Start:
  73.         printf("入");break; //画入口
  74.         case End:
  75.         printf("出");break; //画出口
  76.         case Wall:
  77.         printf("▇");break; //画墙
  78.         case Road:
  79.         printf(" ");break; //画路
  80.     }
  81. }
  82. void game()
  83. {
  84.     int x=2,y=1; //玩家当前位置,刚开始在入口处
  85.     int c; //用来接收按键
  86.     while(1)
  87.     {
  88.         gotoxy(2*y-2,x-1);
  89.         printf("●"); //画出玩家当前位置  这里画出光标的位置也就是个圈圈
  90.         if(map[x][y]==End) //判断是否到达出口
  91.         {
  92.             gotoxy(30,24);
  93.             printf("到达终点,按任意键结束");
  94.             getch();
  95.             break;
  96.         }
  97.         c=get_key();
  98.         if(c==Esc)
  99.         {
  100.             gotoxy(0,24);
  101.             break;
  102.         }
  103.         switch(c)
  104.         {
  105.                 case Up: //向上走
  106.                 if(map[x-1][y]!=Wall)
  107.                 {
  108.                     paint(x,y);
  109.                     x--;
  110.                 }
  111.             break;
  112.             case Down: //向下走
  113.             if(map[x+1][y]!=Wall)
  114.             {
  115.                 paint(x,y);
  116.                 x++;
  117.             }
  118.             break;
  119.             case Left: //向左走
  120.             if(map[x][y-1]!=Wall)
  121.             {
  122.                 paint(x,y);
  123.                 y--;
  124.             }
  125.             break;
  126.             case Right: //向右走
  127.             if(map[x][y+1]!=Wall)
  128.             {
  129.                 paint(x,y);
  130.                 y++;
  131.             }
  132.             break;
  133.         }
  134.     }
  135. }
  136. int main()
  137. {
  138.     system("title yourname");
  139.     int i,j;
  140.     srand((unsigned)time(NULL)); //初始化随即种子
  141.     hidden(); //隐藏光标
  142.     for(i=0;i<=Height+1;i++)
  143.     for(j=0;j<=Width+1;j++)
  144.     if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫
  145.     map[i][j]=Road;
  146.     else map[i][j]=Wall;

  147.     create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数
  148.     for(i=0;i<=Height+1;i++) //边界处理
  149.     {
  150.         map[i][0]=Wall;
  151.         map[i][Width+1]=Wall;
  152.     }

  153.     for(j=0;j<=Width+1;j++) //边界处理
  154.     {
  155.         map[0][j]=Wall;
  156.         map[Height+1][j]=Wall;
  157.     }
  158.     map[2][1]=Start; //给定入口
  159.     map[Height-1][Width]=End; //给定出口
  160.     for(i=1;i<=Height;i++)
  161.     for(j=1;j<=Width;j++) //画出迷宫
  162.     paint(i,j);
  163.     game(); //开始游戏
  164.     getch();
  165.     return 0;
  166. }
复制代码

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表