This program is a C++ program that randomly generates the maze path under the console. You can modify the length and width of the maze by modifying the values of the macro definitions M and N. After running the program, press 1 to start the game and press 2 to exit the game. The game entrance is in the upper left corner, the exit is in the lower right corner, and the character (star) reaches the exit at the lower right corner and prompts to successfully pass the level.
#include<> #include<> #include<> #include<> #include<> #include<ctime> #include <> #define UP 72 #define DOWN 80 #define LEFT 75 #define RIGHT 77 #define M 40 //Labyrinth length#define N 82 //Labyrinth width char maze[M/2][N/2]; //Define the maze arraychar path[M-1][N-1]; //Define path array void setview(void); //Set the console window informationint menu_maze(void); //Home directoryvoid startgame(void); //Start the gamevoid init_maze(void); //Initialize the mazevoid gotoxy(int x, int y); //Move the cursorvoid path_up(int *x, int *y); //Upload pathvoid path_down(int *x, int *y); //Substructure pathvoid path_left(int *x, int *y); //Left pathvoid path_right(int *x, int *y); //Right-structured pathvoid setxy(int x, int y); //Specify the pathvoid path_local(int x, int y); //This setting pathvoid go_up(int *x,int *y); //Move upvoid go_down(int *x,int *y); //Move downvoid go_left(int *x,int *y); //Move leftvoid go_right(int *x,int *y); //Move to the rightvoid HideCursor(void); //Hide the cursorvoid 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); // Get the standard output device handle COORD size = {N*2+167, M*2+43}; SetConsoleScreenBufferSize(hOut,size); //Set the console window buffer size SMALL_RECT rc = {0,0,167,43}; SetConsoleWindowInfo(hOut,true ,&rc); //Set window position and size SetConsoleTitle("maze"); //Set window title HideCursor(); //Hide the cursor} int menu_maze(void) { char c; while(!(c>48&&c<51)) { system("cls"); printf("\n\n\n\n\n\n\n\n"); printf(" ………………^Welcome to DOS Maze Game^……………\n"); printf(" *******************************************\n"); printf(" *************** 1. Start the game******************\n"); printf(" *************** 2. Exit the game******************\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; //The path is blocked or crosses the boundary 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++) //Initialize the maze array { for(j=0;j<N/2;j++) maze[i][j]='f'; } for(i=0;i<M-1;i++) //Initialize the path array { for(j=0;j<N-1;j++) path[i][j]='f'; } path[0][0]='t'; for(i=0;i<N+1;i++) //frame 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)//Build a maze { 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]) //There is no path upward { path_up(&m,&n); m=i; n=j; flag--; break; } if(maze[m+1][n]==maze[0][0]) //There is no path downward { path_down(&m,&n); m=i; n=j; flag--; break; } if(maze[m][n-1]==maze[0][0]) //There is no path to the left { path_left(&m,&n); m=i; n=j; flag--; break; } if(maze[m][n+1]==maze[0][0]) //There is no path to the right { 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)//The cursor is in the starting position { maze[m][n]='t'; path_local(m,n); switch(rand()%2) { case 0://down path_down(&m,&n); break; case 1://To the right path_right(&m,&n); } } if(m==M/2-1&&n==0)//The cursor is in the lower left corner { switch(rand()%2) { case 0://up if(maze[m-1][n]==maze[0][0]) break; //Path has been opened path_up(&m,&n); break; case 1://To the right if(maze[m][n+1]==maze[0][0]) break; path_right(&m,&n); } } if(m==0&&n==N/2-1)//The cursor is in the upper right corner { switch(rand()%2) { case 0://down if(maze[m+1][n]==maze[0][0]) break; path_down(&m,&n); break; case 1://left if(maze[m][n-1]==maze[0][0]) break; path_left(&m,&n); break; } } if(m==M/2-1&&n==N/2-1)//The cursor is in the lower right corner { switch(rand()%2) { case 0://up if(maze[m-1][n]==maze[0][0]) break; path_up(&m,&n); break; case 1://left if(maze[m][n-1]==maze[0][0]) break; path_left(&m,&n); break; } } if(m==0&&n!=0&&n!=N/2-1)//The cursor is in the first line { switch(rand()%3) { case 0://down if(maze[m+1][n]==maze[0][0]) break; path_down(&m,&n); break; case 1://left if(maze[m][n-1]==maze[0][0]) break; path_left(&m,&n); break; case 2://To the right if(maze[m][n+1]==maze[0][0]) break; path_right(&m,&n); } } if(m!=0&&m!=M/2-1&&n==0)//The cursor is in the first column { switch(rand()%3) { case 0://up if(maze[m-1][n]==maze[0][0]) break; path_up(&m,&n); break; case 1://down if(maze[m+1][n]==maze[0][0]) break; path_down(&m,&n); break; case 2://To the right if(maze[m][n+1]==maze[0][0]) break; path_right(&m,&n); } } if(m==M/2-1&&n!=0&&n!=N/2-1)//The cursor is on the last line { switch(rand()%3) { case 0://up if(maze[m-1][n]==maze[0][0]) break; path_up(&m,&n); break; case 1://left if(maze[m][n-1]==maze[0][0]) break; path_left(&m,&n); break; case 2://To the right if(maze[m][n+1]==maze[0][0]) break; path_right(&m,&n); } } if(m!=0&&m!=M/2-1&&n==N/2-1)//The cursor is in the last column { switch(rand()%3) { case 0://up if(maze[m-1][n]==maze[0][0]) break; path_up(&m,&n); break; case 1://down if(maze[m+1][n]==maze[0][0]) break; path_down(&m,&n); break; case 2://left 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)//The cursor is in the middle part { switch(rand()%4) { case 0://up if(maze[m-1][n]==maze[0][0]) break; path_up(&m,&n); break; case 1://down if(maze[m+1][n]==maze[0][0]) break; path_down(&m,&n); break; case 2://left if(maze[m][n-1]==maze[0][0]) break; path_left(&m,&n); break; case 3://To the right 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;//The initial position is dead if(m==0&&n==N/2-1&&maze[m][n-1]==maze[0][0]&&maze[m+1][n]==maze[0][0]) T=0;//The upper right corner dead end if(m==M/2-1&&n==0&&maze[m][n+1]==maze[0][0]&&maze[m-1][n]==maze[0][0]) T=0;//The lower left corner dead end 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;//The end is dead 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;//The first dead end 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;//The first dead end 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;//The last dead end 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;//The last line of dead 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;//The middle part is dead } } 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" "Congratulations, it's successful!"); getch(); }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.