迷宮游戲_自動生成地圖


(更新過后的代碼效果)

 

  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 \n"; 
 28     Pos(9,31);
 29     cout << "↖RAT IN A MAZE↗\n";
 30     Pos(11,31);
 31     cout << "by 一念永恆, 2016\n";
 32     Pos(13, 0);
 33     SetConsoleTextAttribute(console, 11);
 34     cout << "加載ing...\t";
 35     for(int i = 0; i<101; ++i)
 36     {
 37         printf("%2.0f%%", i/100.0 * 100 ); 
 38         Sleep(10);
 39         printf("\b\b\b");
 40     }
 41     SetConsoleTextAttribute(console, 11);
 42     cout << endl; 
 43     cout << "===============================按回車鍵進入游戲=========================================\n";
 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 << ")\n\n";
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! \n";
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! \n";
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! \n";
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! \n";
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! \n";
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! \n";
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! \n";
245     }        
246     system("pause");
247     DeleteDArray(m,col); 
248     return 0;
249 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM