- 注册时间
- 2004-8-29
- 最后登录
- 1970-1-1
|
发表于 2006-12-22 14:34:18
|
显示全部楼层
找到源程序了,不过不太确定是不是最终版,因该不会有问题:- /**
- @迷宫生成程序
- @Author :Eastsun
- @Version:1.0
- @Date :6.7
- */
- #height 240
- #width 320
- #bigram
- #define VISIT 0x01 //该位为1表示还未访问
- #define VISIT1 0x20 //求解时用的
- #define NORTH 0x02 //该位为0表示无墙
- #define SOUTH 0x04
- #define WEST 0x08
- #define EAST 0x10
- #define MASK 0x15
- #define MWIDTH 160
- #define MHEIGHT 120
- char mazeMap[MHEIGHT+2][MWIDTH+2];
- int width,height;
- void drawMaze(int x,int y,int dest,int type){
- int w,h;
- y++;
- for(h =1;h<=height;h++)
- for(w =1;w<=width;w++){
- if(mazeMap[h][w]&EAST) Line(x+w*dest,y+(h-1)*dest,x+w*dest,y+h*dest,type);
- if(mazeMap[h][w]&SOUTH)Line(x+(w-1)*dest,y+h*dest,x+w*dest,y+h*dest,type);
- }
- Line(x,y,x+width*dest,y,type);
- Line(x,y+height*dest,x+width*dest,y+height*dest,type);
- Line(x,y,x,y+height*dest,type);
- Line(x+width*dest,y,x+width*dest,y+height*dest,type);
- y--;
- Line(x+2*dest,y,x+5*dest,y,type);
- Line(x+3*dest+1,y,x+4*dest-1,y,type^1);
- y++;
- Line(x+3*dest+1,y,x+4*dest-1,y,type^1);
- Line(x+width*dest,y+(height-1)*dest+1,x+width*dest,y+height*dest-1,type^1);
- }
- void init(int h,int w,long seed){
- int n,unVisited,listSize,neighbor,px,py,r,process;
- char pathList[MHEIGHT*MWIDTH][2];
- height =h;
- width =w;
- memset(mazeMap,MASK,(MWIDTH+2)*(MHEIGHT+2));
- for(n=0;n<=width+1;n++){ mazeMap[0][n] =0; mazeMap[height+1][n] =0;}
- for(n=0;n<=height+1;n++){mazeMap[n][0] =0; mazeMap[n][width+1] =0; }
-
- if(seed) srand(seed);
- else srand((Getms()<<8)+rand());
- unVisited =height*width;
- listSize =0;
- px =rand()%width+1; py =rand()%height+1; //pick a starting location in maze,at random
- Box(30,27,112,44,1,0);
- Box(30,27,112,44,0,1);
- TextOut(35,30,"迷宫生成中..",0x41);
- process =0;
- while(unVisited-1){
- mazeMap[py][px] =mazeMap[py][px]^VISIT;
- unVisited --;
- if(unVisited%width==1){
- process ++;
- Box(30,27,112,44,1,0);
- Box(30,27,112,44,0,1);
- TextOut(35,30,"迷宫生成中..",0x41);
- Box(35,30,35+72*process/height,41,1,2);
- }
- while(!(neighbor=(mazeMap[py][px-1]&VISIT)+(mazeMap[py][px+1]&VISIT)+
- (mazeMap[py-1][px]&VISIT)+(mazeMap[py+1][px]&VISIT))){
- py =pathList[--listSize][0];
- px =pathList[listSize][1];
- }
- if(neighbor>1){
- pathList[listSize][0] =py;
- pathList[listSize][1] =px;
- listSize ++;
- }
- r =rand()%neighbor;
- n =0;
- if(mazeMap[py-1][px]&VISIT){
- if(n ==r){
- mazeMap[py-1][px] =mazeMap[py-1][px]^SOUTH;
- py--;
- continue;
- }
- else n++;
- }
- if(mazeMap[py+1][px]&VISIT){
- if(n ==r){
- mazeMap[py][px] =mazeMap[py][px]^SOUTH;
- py++;
- continue;
- }
- else n++;
- }
- if((mazeMap[py][px-1]&VISIT)&&n ==r){
- mazeMap[py][px-1] =mazeMap[py][px-1]^EAST;
- px--;
- }
- else{
- mazeMap[py][px] =mazeMap[py][px]^EAST;
- px++;
- }
- }//while
- }
- void main(){
- char key,d;
- while(1){
- ClearScreen();
- TextOut(1,0, "*******迷宫生成程序*******",1);
- TextOut(1,24, " 1 小型迷宫(39×19) ",1);
- TextOut(1,40, " 2 大型迷宫(79×39) ",1);
- TextOut(1,67, "********By Eastsun********",1);
- Refresh();
- key =getchar();
- if(key =='b'){ init(59,79,0); d =4;}
- else if(key =='n'){ init(119,159,0); d =2;}
- else if(key ==0x1b) break;
- else continue;
- ClearScreen();
- Refresh();
- drawMaze(1,0,d,0x01);
- getchar();
- }
- }
-
-
复制代码 |
|