C++ console implements randomly generated path maze game
- 2020-06-03 07:57:26
- OfStack
This procedure is to control the audience randomly generated maze path 1 C + + program, you can change the value of the macro definition M and N to modify the length and width of the maze, run the program after press 1 to start the game by 2 out of the game, the game entry in the upper left corner, exports in the lower right corner, export tip successfully recruit characters (stars) to the lower right corner.
#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 // Length of the maze
#define N 82 // The width of the labyrinth
char maze[M/2][N/2]; // Define maze array
char path[M-1][N-1]; // Define path array
void setview(void); // Set the console window information
int menu_maze(void); // Home directory
void startgame(void); // Start the game
void init_maze(void); // Initialization maze
void gotoxy(int x, int y); // Move the cursor
void path_up(int *x, int *y); // Structure on the path
void path_down(int *x, int *y); // The constitutive path
void path_left(int *x, int *y); // Left constitutive path
void path_right(int *x, int *y); // Constitutive right path
void setxy(int x, int y); // Specifies the bit through path
void path_local(int x, int y); // This set path
void go_up(int *x,int *y); // Move up
void go_down(int *x,int *y); // Move down the
void go_left(int *x,int *y); // Move to the left
void go_right(int *x,int *y); // To the right
void HideCursor(void); // Hide the cursor
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); // Gets 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 the 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 use DOS Maze game ^ ........................ \n");
printf(" *******************************************\n");
printf(" **************** 1. Start the game ****************\n");
printf(" **************** 2. Quit 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(" u ");
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; // An impassable or transboundary path
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++) // Initializes the maze array
{
for(j=0;j<N/2;j++)
maze[i][j]='f';
}
for(i=0;i<M-1;i++) // Initializes 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++) // A border
cout<<"**";
cout<<endl;
for(i=0;i<M+1;i++)
{
for(j=0;j<N+1;j++)
{
cout<<" s ";
}
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's an open path up here
{
path_up(&m,&n);
m=i;
n=j;
flag--;
break;
}
if(maze[m+1][n]==maze[0][0]) // There's an open path down here
{
path_down(&m,&n);
m=i;
n=j;
flag--;
break;
}
if(maze[m][n-1]==maze[0][0]) // There's an open path to the left
{
path_left(&m,&n);
m=i;
n=j;
flag--;
break;
}
if(maze[m][n+1]==maze[0][0]) // There is an open 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 at 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:// upward
if(maze[m-1][n]==maze[0][0]) break; // Open path
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:// To the 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:// upward
if(maze[m-1][n]==maze[0][0]) break;
path_up(&m,&n);
break;
case 1:// To the 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 in the first 1 line
{
switch(rand()%3)
{
case 0:// down
if(maze[m+1][n]==maze[0][0]) break;
path_down(&m,&n);
break;
case 1:// To the 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 in the first 1 column
{
switch(rand()%3)
{
case 0:// upward
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 at the end 1 line
{
switch(rand()%3)
{
case 0:// upward
if(maze[m-1][n]==maze[0][0]) break;
path_up(&m,&n);
break;
case 1:// To the 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 at the end 1 column
{
switch(rand()%3)
{
case 0:// upward
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 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
{
switch(rand()%4)
{
case 0:// upward
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 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;// Initial position dead end
if(m==0&&n==N/2-1&&maze[m][n-1]==maze[0][0]&&maze[m+1][n]==maze[0][0]) T=0;// Dead end in the upper right corner
if(m==M/2-1&&n==0&&maze[m][n+1]==maze[0][0]&&maze[m-1][n]==maze[0][0]) T=0;// Dead end in the lower left corner
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 ends,
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 1 Line ends,
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 1 Column ends,
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 1 Column ends,
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 1 Line ends,
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(" u ");
(*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(" u ");
(*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(" u ");
(*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(" u ");
(*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 worked! ");
getch();
}