- // SimpleGenerateMaze.cpp : Defines the entry point for the console application.
- //
- /************************** A* Algorithm Copyright**************************/
- /*You can distributed this algorithm as you like only saving this declaration above*/
- /********************Date: 2004.7.18 Author: caucy*************************/
- #include "stdafx.h"
- #include "stdlib.h"
- #include "iostream.h"
- #include "time.h"
- #define SIZEX_MAZE 30
- #define SIZEY_MAZE 40
- int maze[SIZEX_MAZE][SIZEY_MAZE];
- //出口点
- #define EXIT_ROW SIZEX_MAZE-2
- #define EXIT_COL SIZEY_MAZE-2
- void GenerateMaze()
- {
- //设置种子
- srand(time(NULL));
- int i,j;
- int k;
- //Step1: 初始设置迷宫为不可走通
- for(i=0;i<SIZEX_MAZE;i++)
- for(j=0;j<SIZEY_MAZE;j++)
- maze[i][j]=1;
- i=1;j=1;
- //Step2: 设置迷宫入口点在(1,1)位置
- maze[i][j]=0;
- //Step3: 探索一条通路出来
- while(i!=EXIT_ROW||j!=EXIT_COL)
- {
- //Step3.1: 如果到了行墙边,则沿着墙边打通墙
- if(i==EXIT_ROW)
- {
- while(j!=EXIT_COL)
- maze[i][++j]=0;
- break;
- }
- //Step3.2: 如果到了列墙边,则沿着墙边打通墙
- if(j==EXIT_COL)
- {
- while(i!=EXIT_ROW)
- maze[++i][j]=0;
- break;
- }
- //Step3.3: 产生一个随机运动的方向
- k=rand()%3;
- //打通向下方向的墙
- if(k==0)
- {
- maze[++i][j]=0;
- continue;
- }
- //打通向右方向的墙
- if(k==1)
- {
- maze[i][++j]=0;
- continue;
- }
- /*如果采用8连通的迷宫,则还需要加入右下方的通路
- //打通向右下方向的墙
- if(k==2)
- {
- maze[++i][++j]=0;
- continue;
- }
- */
- }
- //Step4: 加入随机通路
- k=0;
- while(k<SIZEX_MAZE)
- {
- i=rand()%EXIT_ROW;
- j=rand()%EXIT_COL;
- maze[i][j]=0;
- k++;
- }
- }
- void ShowMaze()
- {
- int i,j;
- for(i=0;i<SIZEX_MAZE;i++)
- {
- for(j=0;j<SIZEY_MAZE;j++)
- {
- cout<<maze[i][j];
- }
- cout<<endl;
- }
- }
- int main(int argc, char* argv[])
- {
- GenerateMaze();
- ShowMaze();
- return 0;
- }