时间:2021-06-01 08:52:41 | 栏目:C代码 | 点击:次
本程序是在控制台下随机生成迷宫路径的一个C++程序,可以通过修改宏定义 M 和 N 的值来修改迷宫的长度和宽度,运行程序后 按1开始游戏 按2退出游戏,游戏入口在左上角,出口在右下角,人物(星星)到达右下角出口提示成功闯关。
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> #include<iostream.h> #include<ctime> #include <windows.h> #define UP 72 #define DOWN 80 #define LEFT 75 #define RIGHT 77 #define M 40 //迷宫长度 #define N 82 //迷宫宽度 char maze[M/2][N/2]; //定义迷宫数组 char path[M-1][N-1]; //定义路径数组 void setview(void); //设置控制台窗口信息 int menu_maze(void); //主目录 void startgame(void); //开始游戏 void init_maze(void); //初始化迷宫 void gotoxy(int x, int y); //移动光标 void path_up(int *x, int *y); //上构路径 void path_down(int *x, int *y); //下构路径 void path_left(int *x, int *y); //左构路径 void path_right(int *x, int *y); //右构路径 void setxy(int x, int y); //指定位打通路径 void path_local(int x, int y); //本置路径 void go_up(int *x,int *y); //向上移动 void go_down(int *x,int *y); //向下移动 void go_left(int *x,int *y); //向左移动 void go_right(int *x,int *y); //向右移动 void HideCursor(void); //隐藏光标 void win(void); int T; int F; int m; int n; int x; int target; int flag; int local_x; int local_y; void main() { setview(); while(1) { switch(menu_maze()) { case 49: system("cls"); startgame(); continue; case 50:exit(0); } } } void setview() { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出设备句柄 COORD size = {N*2+167, M*2+43}; SetConsoleScreenBufferSize(hOut,size); //设置控制台窗口缓冲区大小 SMALL_RECT rc = {0,0,167,43}; SetConsoleWindowInfo(hOut,true ,&rc); //设置窗口位置和大小 SetConsoleTitle("迷宫"); //设置窗口标题 HideCursor(); //隐藏光标 } int menu_maze(void) { char c; while(!(c>48&&c<51)) { system("cls"); printf("\n\n\n\n\n\n\n\n"); printf(" ………………^欢迎使用DOS迷宫游戏^……………\n"); printf(" *******************************************\n"); printf(" **************** 1.开始游戏****************\n"); printf(" **************** 2.退出游戏****************\n"); printf(" *******************************************\n"); c=getch(); } return c; } void startgame() { char key; local_x=0; local_y=0; system("cls"); init_maze(); gotoxy(2,2); printf("★"); while(path[M-2][N-2]!='o') { key=getch(); if(key==-32) { key=getch(); switch(key) { case UP: if(path[local_x-1][local_y]!='t'&&path[local_x-1][local_y]!='o'||local_x-1<0) break; //路径不通或越界 go_up(&local_x,&local_y); break; case DOWN: if(path[local_x+1][local_y]!='t'&&path[local_x+1][local_y]!='o'||local_x+1>M-2) break; go_down(&local_x,&local_y); break; case LEFT: if(path[local_x][local_y-1]!='t'&&path[local_x][local_y-1]!='o'||local_y-1<0) break; go_left(&local_x,&local_y); break; case RIGHT: if(path[local_x][local_y+1]!='t'&&path[local_x][local_y+1]!='o'||local_y+1>N-2) break; go_right(&local_x,&local_y); break; } } } system("cls"); win(); } void init_maze() { int i,j; T=1; F=1; m=0; n=0; x=0; flag=0; srand((unsigned)time(NULL)); for(i=0;i<M/2;i++) //初始化迷宫数组 { for(j=0;j<N/2;j++) maze[i][j]='f'; } for(i=0;i<M-1;i++) //初始化路径数组 { for(j=0;j<N-1;j++) path[i][j]='f'; } path[0][0]='t'; for(i=0;i<N+1;i++) //边框 cout<<"**"; cout<<endl; for(i=0;i<M+1;i++) { for(j=0;j<N+1;j++) { cout<<"■"; } cout<<endl; } for(i=0;i<N+1;i++) cout<<"**"; cout<<endl; while(F)//构建迷宫 { if(T==0) { for(j=0;j<N/2;j++) { for(i=0;i<M/2;i++) { if(maze[i][j]=='f') { m=i; n=j; maze[m][n]='t'; path_local(m,n); if(maze[m-1][n]==maze[0][0]) //向上有未打通路径 { path_up(&m,&n); m=i; n=j; flag--; break; } if(maze[m+1][n]==maze[0][0]) //向下有未打通路径 { path_down(&m,&n); m=i; n=j; flag--; break; } if(maze[m][n-1]==maze[0][0]) //向左有未打通路径 { path_left(&m,&n); m=i; n=j; flag--; break; } if(maze[m][n+1]==maze[0][0]) //向右有未打通路径 { path_right(&m,&n); m=i; n=j; flag--; break; } } } if(m==i&&n==j) break; } } T=1; while(T) { x++; if(m==0&&n==0)//光标在起始位置 { maze[m][n]='t'; path_local(m,n); switch(rand()%2) { case 0://向下 path_down(&m,&n); break; case 1://向右 path_right(&m,&n); } } if(m==M/2-1&&n==0)//光标在左下角 { switch(rand()%2) { case 0://向上 if(maze[m-1][n]==maze[0][0]) break; //已打通路径 path_up(&m,&n); break; case 1://向右 if(maze[m][n+1]==maze[0][0]) break; path_right(&m,&n); } } if(m==0&&n==N/2-1)//光标在右上角 { switch(rand()%2) { case 0://向下 if(maze[m+1][n]==maze[0][0]) break; path_down(&m,&n); break; case 1://向左 if(maze[m][n-1]==maze[0][0]) break; path_left(&m,&n); break; } } if(m==M/2-1&&n==N/2-1)//光标在右下角 { switch(rand()%2) { case 0://向上 if(maze[m-1][n]==maze[0][0]) break; path_up(&m,&n); break; case 1://向左 if(maze[m][n-1]==maze[0][0]) break; path_left(&m,&n); break; } } if(m==0&&n!=0&&n!=N/2-1)//光标在第一行 { switch(rand()%3) { case 0://向下 if(maze[m+1][n]==maze[0][0]) break; path_down(&m,&n); break; case 1://向左 if(maze[m][n-1]==maze[0][0]) break; path_left(&m,&n); break; case 2://向右 if(maze[m][n+1]==maze[0][0]) break; path_right(&m,&n); } } if(m!=0&&m!=M/2-1&&n==0)//光标在第一列 { switch(rand()%3) { case 0://向上 if(maze[m-1][n]==maze[0][0]) break; path_up(&m,&n); break; case 1://向下 if(maze[m+1][n]==maze[0][0]) break; path_down(&m,&n); break; case 2://向右 if(maze[m][n+1]==maze[0][0]) break; path_right(&m,&n); } } if(m==M/2-1&&n!=0&&n!=N/2-1)//光标在最后一行 { switch(rand()%3) { case 0://向上 if(maze[m-1][n]==maze[0][0]) break; path_up(&m,&n); break; case 1://向左 if(maze[m][n-1]==maze[0][0]) break; path_left(&m,&n); break; case 2://向右 if(maze[m][n+1]==maze[0][0]) break; path_right(&m,&n); } } if(m!=0&&m!=M/2-1&&n==N/2-1)//光标在最后一列 { switch(rand()%3) { case 0://向上 if(maze[m-1][n]==maze[0][0]) break; path_up(&m,&n); break; case 1://向下 if(maze[m+1][n]==maze[0][0]) break; path_down(&m,&n); break; case 2://向左 if(maze[m][n-1]==maze[0][0]) break; path_left(&m,&n); } } if(m!=0&&m!=M/2-1&&n!=0&&n!=N/2-1)//光标在中间部分 { switch(rand()%4) { case 0://向上 if(maze[m-1][n]==maze[0][0]) break; path_up(&m,&n); break; case 1://向下 if(maze[m+1][n]==maze[0][0]) break; path_down(&m,&n); break; case 2://向左 if(maze[m][n-1]==maze[0][0]) break; path_left(&m,&n); break; case 3://向右 if(maze[m][n+1]==maze[0][0]) break; path_right(&m,&n); } } if(x>M*N/4) { x=0; if(m==0&&n==0&&maze[m][n+1]==maze[0][0]&&maze[m+1][n]==maze[0][0]) T=0;//初始位置死路 if(m==0&&n==N/2-1&&maze[m][n-1]==maze[0][0]&&maze[m+1][n]==maze[0][0]) T=0;//右上角死路 if(m==M/2-1&&n==0&&maze[m][n+1]==maze[0][0]&&maze[m-1][n]==maze[0][0]) T=0;//左下角死路 if(m==M/2-1&&n==N/2-1&&maze[m][n-1]==maze[0][0]&&maze[m-1][n]==maze[0][0]) T=0;//终点死路 if(m==0&&n!=0&&n!=N/2-1&&maze[m][n-1]==maze[0][0]&&maze[m][n+1]==maze[0][0]&&maze[m+1][n]==maze[0][0]) T=0;//第一行死路 if(m!=0&&m!=M/2-1&&n==0&&maze[m-1][n]==maze[0][0]&&maze[m][n+1]==maze[0][0]&&maze[m+1][n]==maze[0][0]) T=0;//第一列死路 if(m!=0&&m!=M/2-1&&n==N/2-1&&maze[m-1][n]==maze[0][0]&&maze[m][n-1]==maze[0][0]&&maze[m+1][n]==maze[0][0]) T=0;//最后一列死路 if(m==M/2-1&&n!=0&&n!=N/2-1&&maze[m-1][n]==maze[0][0]&&maze[m][n+1]==maze[0][0]&&maze[m][n-1]==maze[0][0]) T=0;//最后一行死路 if(m>0&&m<M/2-1&&n>0&&n<N/2-1&&maze[m+1][n]==maze[0][0]&&maze[m-1][n]==maze[0][0]&&maze[m][n+1]==maze[0][0]&&maze[m][n-1]==maze[0][0]) T=0;//中间部分死路 } } if(flag==M*N/4) F=0; } /* i=M+3; gotoxy(0,i); for(i=0;i<M-1;i++) { for(j=0;j<N-1;j++) { if(path[i][j]=='f') printf("1"); if(path[i][j]=='t') printf("0"); } printf("\n"); } getch();*/ } void gotoxy(int x, int y) { COORD pos = {x,y}; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOut, pos); } void path_up(int *x, int *y) { int i,j; maze[--(*x)][*y]=maze[0][0]; path[2*(*x+1)-1][2*(*y)]=path[0][0]; path_local(*x,*y); i=4*(*y)+2; j=2*(*x)+3; gotoxy(i,j); printf(" "); } void path_down(int *x, int *y) { int i,j; maze[++(*x)][*y]=maze[0][0]; path[2*(*x-1)+1][2*(*y)]=path[0][0]; path_local(*x,*y); i=4*(*y)+2; j=2*(*x)+1; gotoxy(i,j); printf(" "); } void path_left(int *x, int *y) { int i,j; maze[*x][--(*y)]=maze[0][0]; path[2*(*x)][2*(*y+1)-1]=path[0][0]; path_local(*x,*y); i=4*(*y)+4; j=2*(*x)+2; gotoxy(i,j); printf(" "); } void path_right(int *x, int *y) { int i,j; maze[*x][++(*y)]=maze[0][0]; path[2*(*x)][2*(*y-1)+1]=path[0][0]; path_local(*x,*y); i=4*(*y); j=2*(*x)+2; gotoxy(i,j); printf(" "); } void setxy(int x, int y) { gotoxy(x,y); printf(" "); } void path_local(int x, int y) { int i,j; i=4*y+2; j=2*x+2; gotoxy(i,j); printf(" "); path[2*x][2*y]=path[0][0]; flag++; } void go_up(int *x,int *y) { int i,j; i=2*(*y)+2; j=(*x)+2; gotoxy(i,j); printf(" "); j-=1; gotoxy(i,j); printf("★"); (*x)--; path[*x][*y]='o'; } void go_down(int *x,int *y) { int i,j; i=2*(*y)+2; j=(*x)+2; gotoxy(i,j); printf(" "); j+=1; gotoxy(i,j); printf("★"); (*x)++; path[*x][*y]='o'; } void go_left(int *x,int *y) { int i,j; i=2*(*y)+2; j=(*x)+2; gotoxy(i,j); printf(" "); i-=2; gotoxy(i,j); printf("★"); (*y)--; path[*x][*y]='o'; } void go_right(int *x,int *y) { int i,j; i=2*(*y)+2; j=(*x)+2; gotoxy(i,j); printf(" "); i+=2; gotoxy(i,j); printf("★"); (*y)++; path[*x][*y]='o'; } void HideCursor() { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void win() { printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" " 恭喜你,成功了!"); getch(); }