This article examples share a resized maze game designed by C++, with the entrance to a given maze. If there is an exit, the program can display the walking path and finally reach the exit and output "Successfully walk out of the maze"; if there is no exit, the program can also display the walking process and eventually fall back to the entrance and output "Roll back to the entrance".
//This is a maze game#include<iostream> #include<ctime> #include<cstdlib>/* is used to generate random numbers to form a maze of random changes*/ #include<iomanip>/* Used for output format control, so that the generated maze appearance rules*/ using namespace std; /*The function mazegenerator*/ char*mazegenerator(int m,int n) { int i,j,k;/*counter*/ char *p=new char[m*n];/* Use an array of length m*n to store the maze of mxn size*/ srand(int(time(0)));/* Used to generate random numbers*/ if(m<10||n<10)//When the order is small, it will be more interesting for(i=0;i<m*n;i++) { if(rand()%3==0)p[i]='.';//When the random number is a multiple of three, define the point in the maze as a path else p[i]='#'; } else for(i=0;i<m*n;i++) { if(rand()%2==0)p[i]='.';/*When the random number is an even number, define the point in the maze as a path*/ else p[i]='#'; } /*Cross the left and upper boundaries, leaving only one entrance, and can only walk out from the right and lower boundaries. Make it more challenging to get out of the maze*/ for(i=0;i<m;i++) p[i]='#'; for(i=0;i<m*n;i+=m) p[i]='#'; for(i=m-1;i<m*n;i+=m) p[i]='#'; for(i=m*(n-1);i<m*n;i++) p[i]='#'; /*Next, the first 3 orders are constructed into T-shaped*/ p[0]=p[1]=p[2]=p[2*m]=p[2*m+2]='#'; p[m+1]=p[m+2]=p[2*m+1]='.'; /*The maze entrance is defined as 'x'*/ p[m]='x'; /*The maze is random and requires a path to be constructed*/ /*This is a path that turns every three cells*/ for(j=3;j<m;j+=3) { for(k=0;k<3;k++) { i=(j-3)+k+1; if(i*m+j<m*n) p[i*m+j]='.'; } } for(i=4;i<m;i+=3) for(k=0;k<3;k++) { j=(i-3)+k+2; if(i*m+j<m*n) p[i*m+j]='.'; } return p; } /*A new maze needs to be output after generating or modifying the maze*/ void showmaze(char*p,int m,int n) { int i,j;/*i represents row, j represents column, i*n+j is the sequence number of that point in the array*/ for(i=0;i<n;i++) { for(j=0;j<m;j++) cout<<setw(2)<<p[i*m+j];/*Each character occupies two, making the maze square*/ cout<<endl; } } /*Mazetraverse function mazetraverse*/ void mazetraverse(char*p,int m,int n) { int x,y,z;/*y is the current position, x is the previous step, z is the next step, t is used to enter the instruction to continue walking the maze*/ /*Initialize x,y, that is, the location of the entrance to the maze*/ x=m; y=m+1; p[y]='x';/*This is the first step in the maze*/ cout<<"Please press Enter to move:";/*Let the user control the process of walking the maze*/ (); cout<<endl; showmaze(p,m,n);/*The output of the maze after leaving*/ cout<<endl; cout<<"Please press Enter to move: "; (); /*The key is to use x and y to determine the value of z*/ while(!((y>m*(n-1))||(((y+1)%m)==0)))/*Execute loop maze before reaching the boundary*/ { /*Judge the right direction, if the right is '.', then go to the right*/ if(y-x==1)z=y+m; if(y-x==m)z=y-1; if(y-x==-m)z=y+1; if(y-x==-1)z=y-m; /*When the right is not '.', walk ahead of y*/ if(p[z]=='#') { /*Judge the front of y, if the front is '.', then go forward*/ if(y-x==1)z=y+1; if(y-x==m)z=y+m; if(y-x==-m)z=y-m; if(y-x==-1)z=y-1; /*If the front is '#', go left*/ if(p[z]=='#') { /*Judge the left position*/ if(y-x==1)z=y-m; if(y-x==m)z=y+1; if(y-x==-m)z=y-1; if(y-x==-1)z=y+m; /*If the left is '#', it is a dead end and you need to go backwards*/ if(p[z]=='#')z=x; } } p[z]='x';/*In the end, z should be 'x', indicating the traces of passing*/ showmaze(p,m,n); cout<<endl; cout<<"Please press Enter to move: "; (); cout<<endl; /*Replace x,y to realize the current position movement*/ x=y; y=z; } } int main() { int m,n;/*n is the order of the maze, due to the limitation of the output window size, n cannot be greater than the number of characters that can be displayed on each line*/ cout<<"Chen Weihang's Maze Game\nMy maze is very neat!^_^\n"<<endl; cout<<"This is a maze of any size,"<<endl; cout<<"The maze generated is different every time.\n"<<endl; cout<<"Because the screen displays up to 80 characters at a time, \nEach character takes up two more squares,"<<endl <<"So it's not good to enter more than 40th level,\nAlthough it's still right. Haha\n"<<endl; cout<<"Please enter the width of the maze in turnmand heightn(m>2,n>2):"<<endl; /* Multiple sets of data can be tested*/ while(cin>>m>>n) { char*a=mazegenerator(m,n); showmaze(a,m,n); cout<<endl; cout<<"The first step in the Long March:\n"<<endl; mazetraverse(a,m,n); cout<<"Congratulations! You have successfully walked out of the maze!"<<endl<<endl; cout<<"To try a new maze,Please enter the order of the new mazen(n>2),\notherwise,according toctrl+z,再according toenterFinish"<<endl; } }
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.