zoukankan      html  css  js  c++  java
  • 迷宫游戏_自动生成地图

    (更新过后的代码效果)

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<windows.h>          //Sleep()头文件 
      4 #include<cstring>
      5 #include<mmsystem.h>         //播放MP3文件的头文件,工具编译选项编译器在连接器命令加入-lwinmm  
      6 #include<conio.h>
      7 #include<ctime>
      8 
      9 using namespace std;
     10 
     11 void welcome();             //欢迎模块 
     12 void play_mp3(char *ps);    //播放音乐 
     13 void Pos(int x, int y) {     //设置放置位置 
     14     COORD p;
     15     p.X = y;p.Y = x;
     16     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), p);
     17 }
     18 
     19 void welcome() {
     20     char pause;
     21     HANDLE console;
     22     console = GetStdHandle(STD_OUTPUT_HANDLE);
     23     char mp[] = "F:\KuGou\薛之谦_绅士.mp3";
     24     play_mp3(mp);
     25     Pos(7,33);
     26     SetConsoleTextAttribute(console, 15);
     27     cout << " Welcome To 
    "; 
     28     Pos(9,31);
     29     cout << "↖RAT IN A MAZE↗
    ";
     30     Pos(11,31);
     31     cout << "by 一念永恒, 2016
    ";
     32     Pos(13, 0);
     33     SetConsoleTextAttribute(console, 11);
     34     cout << "加载ing...	";
     35     for(int i = 0; i<101; ++i)
     36     {
     37         printf("%2.0f%%", i/100.0 * 100 ); 
     38         Sleep(10);
     39         printf("");
     40     }
     41     SetConsoleTextAttribute(console, 11);
     42     cout << endl; 
     43     cout << "===============================按回车键进入游戏=========================================
    ";
     44     cin.get(pause);
     45     system("cls");
     46 }
     47 
     48 void play_mp3(char *ps)  // 歌曲的名字 
     49 {
     50     char str[100] = "play ";
     51     strcat(str,ps);
     52     mciSendString(str,NULL,0,NULL);
     53 }
     54 
     55 char **NewDArray(int col, int row)         //设置迷宫的大小 
     56 {
     57     char **m = new char*[col];
     58     if (!m) {
     59         return nullptr;
     60     }
     61     for (int i = 0; i < col; i++)
     62     {
     63         m[i] = new char[row];
     64         if (!m[i]) {
     65             for (int k = 0; k < i; k++)
     66                 delete []m[i];
     67             delete []m;
     68             return nullptr;
     69         } 
     70     }
     71     return m;
     72 }
     73 
     74 void DeleteDArray(char **m, int col)        //可改成引用吗 char **&m ????? 
     75 {
     76     for (int i = 0; i < col; i++)
     77         delete []m[i];
     78     delete []m;
     79 }
     80 
     81 void MazeGenerator(char **m, int col, int row)  //随机产生迷宫
     82 {
     83     int i, j;
     84     time_t t;
     85     
     86     srand((unsigned)time(&t));                 //设置随机种子
     87     for (i = 0; i < col; i++) 
     88         for (j = 0; j < row; j++)
     89             if (rand()%2)                      //随机数为1,2输出#,为0输出  . 
     90                 m[i][j] = '#';
     91             else
     92                 m[i][j] = '.';                
     93 } 
     94 
     95 void MazeDisplay(char **m, int col, int row)   //显示迷宫 
     96 {
     97     system("cls");
     98     
     99     int i, j;
    100     
    101     HANDLE console;
    102     console = GetStdHandle(STD_OUTPUT_HANDLE);
    103     SetConsoleTextAttribute(console, 7);
    104     cout << endl;
    105     Pos(6, 32);
    106     for (int i = 0; i < col; i++) {
    107         cout << i; 
    108         if (i < 10) cout << ' ';
    109     }
    110     
    111     for (i = 0; i < row; i++)
    112     {   
    113         Pos(7+i, 29);
    114         cout << i << " ";
    115         for (j = 0; j < col; j++) {
    116             if (m[i][j] == 'x') {
    117                 SetConsoleTextAttribute(console, 13);
    118                 cout << ' ' << m[i][j];
    119             }
    120             else if (m[i][j] == 'E') {
    121                 SetConsoleTextAttribute(console, 12);
    122                 cout << ' ' << m[i][j];
    123             }
    124             else {
    125                 SetConsoleTextAttribute(console, 7);
    126                 cout << ' ' << m[i][j];
    127             }
    128         }
    129         cout << endl;
    130     }
    131     Pos(7+row, 0);
    132     cout << endl;
    133 }
    134 
    135 void MazeEntrance(  //功能: 输入迷宫入口 
    136         char **m, int col, int row,   //迷宫 
    137         int *x0, int *y0              //迷宫入口 
    138                 )
    139 {
    140     int x, y;                         //用来输入迷宫入口位置 
    141     do {
    142         HANDLE console;
    143         console = GetStdHandle(STD_OUTPUT_HANDLE);
    144         SetConsoleTextAttribute(console, 13);
    145         cout << "Enter the maze entrance position:";
    146         cin >> x >> y;
    147     } while ( (x < 0 || x > col || y < 0 || y > row) || (m[x][y] == '#'));  //检测输入数据合理性 
    148     *x0 = x;                                                                //行小于0,大于迷宫的行。列。。。。重输 
    149     *y0 = y;
    150 }
    151 
    152 int MazeTraverse(        //功能: 走迷宫 
    153         char **m, int col, int row, //迷宫 
    154         int x0, int y0,             //迷宫入口 
    155         int x, int y                //进入迷宫后当前所处位置
    156         )                           //返回值为0--迷宫无出口;1--迷宫有出口 
    157 {
    158     int r;
    159     
    160     
    161     m[x][y] = 'x';                  //迷宫当前为设为 x ---将显示他的移动
    162     MazeDisplay(m, col, row);       //显示迷宫
    163     
    164     if ((x == 0 || x == col-1 || y == 0 || y == row-1) && (x != x0 || y != y0)) //找到出口--且该位置不等于入口 
    165     {
    166         cout << "The exit is (" << x << ',' << y << ")
    
    ";
    167         DeleteDArray(m, col);       //L: 释放迷宫所占动态内存     
    168         exit(1);                    //不再找其他出口,直接结束;否则用return 1;
    169                                     //可找到所有的出口,但此时L行省略 
    170     }
    171     HANDLE console;
    172     console = GetStdHandle(STD_OUTPUT_HANDLE);
    173     SetConsoleTextAttribute(console, 13);
    174     cout << "Press any key to go next step! 
    ";
    175     cin.get();
    176     
    177     if (x+1 < col && m[x+1][y] == '.')    //向下寻找出口
    178     {
    179         r = MazeTraverse(m, col, row, x0, y0, x+1, y);
    180         if (r == 0)
    181         {
    182             m[x+1][y] = 'E';              //做一标志,此路不通
    183             MazeDisplay(m, col, row);      
    184         }
    185         cout << "Press any key to go next step! 
    ";
    186         cin.get();
    187     } 
    188     
    189     if (x-1 >= 0 && m[x-1][y] == '.')     //向上寻找出口 
    190     {
    191         r = MazeTraverse(m, col, row, x0, y0, x-1, y);
    192         if (r == 0)
    193         {
    194             m[x-1][y] = 'E';              //做一标志,此路不通
    195             MazeDisplay(m, col, row);      
    196         }
    197         cout << "Press any key to go next step! 
    ";
    198         cin.get();
    199     }
    200     if (y+1 < row && m[x][y+1] == '.')     //向右寻找出口 
    201     {
    202         r = MazeTraverse(m, col, row, x0, y0, x, y+1);
    203         if (r == 0)
    204         {
    205             m[x][y+1] = 'E';              //做一标志,此路不通
    206             MazeDisplay(m, col, row);      
    207         }
    208         cout << "Press any key to go next step! 
    ";
    209         cin.get();
    210     }
    211     if (y-1 >= 0 && m[x][y-1] == '.')     //向左寻找出口 
    212     {
    213         r = MazeTraverse(m, col, row, x0, y0, x, y-1);
    214         if (r == 0)
    215         {
    216             m[x][y-1] = 'E';              //做一标志,此路不通
    217             MazeDisplay(m, col, row);      
    218         }
    219         cout << "Press any key to go next step! 
    ";
    220         cin.get();
    221     }
    222     return 0;
    223 } 
    224 
    225 int main()
    226 {
    227     welcome();
    228     char **m;
    229     int x0, y0,    //迷宫入口位置
    230         col, row;  //the maze size;
    231     cout << "Enter the maze size(col and row): ";
    232     cin >> col >> row; 
    233     m = NewDArray(col,row);           //返回动态申请的二维数组
    234     if (!m) {
    235         cout << "out of Memory! 
    ";
    236         return 1;
    237     }
    238     MazeGenerator(m, col, row);         //随机生成迷宫 
    239     MazeDisplay(m,col,row);             //显示迷宫 
    240     MazeEntrance(m, col, row, &x0, &y0);//输入迷宫入口位置----迷宫当前位置仍然需要,所以用指针 
    241     
    242     if (MazeTraverse(m,col,row,x0,y0,x0,y0) == 0) //走迷宫 
    243     {
    244         cout << "Not exit! 
    ";
    245     }        
    246     system("pause");
    247     DeleteDArray(m,col); 
    248     return 0;
    249 }
  • 相关阅读:
    python笔记之re模块学习
    python笔记之面向对象
    C# 静态类和非静态类(实例类)
    占位符的使用
    数据类型和数据类型转换
    win7 安装 memcached
    php 汉字转换成拼音
    apache window环境下本地配置虚拟主机
    在浏览器输入一个网址到得到页面的过程
    浅谈线程池ThreadPoolExecutor核心参数
  • 原文地址:https://www.cnblogs.com/douzujun/p/5939230.html
Copyright © 2011-2022 走看看