迷宫寻路-自动随机生成迷宫

#include<iostream>
#include"stdio.h"
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<ctime>
#include <windows.h>
#include<stack>
using namespace std;

#define M 40                               //the width of the maze 
#define N 82                               //the length of the maze 

char maze[M / 2][N / 2];                   //define the array of maze 
char path[M - 1][N - 1];                   //define the array of path
//int visited[M][N];

void setview(void);                        //set the information about console window
char menu_maze(void);                       //the main menu
void startgame(void);                      //start the game
void init_maze(void);                      //initialize the maze
void gotoxy(int x, int y);                 //move the cursor
void path_up(int *x, int *y);              //up path
void path_down(int *x, int *y);            //down path
void path_left(int *x, int *y);            //left path 
void path_right(int *x, int *y);           //right path
void setxy(int x, int y);                  //specifies a bit path to get through
void path_local(int x, int y);             //local path
void HideCursor(void);                     //hide the cursor

int T;
int F;
int m;
int n;
short int x;
int target;
int flag;
int local_x;
int local_y;

int main()
{
	
	setview();
	
	if (int(menu_maze()) == 49)
	{
		system("cls");
		startgame();
	}
	else
			exit(0);
	
	return 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 size of handle buffer
	SMALL_RECT rc = { 0,0,167,43 };
	SetConsoleWindowInfo(hOut, true, &rc);         //set of positon and size of window

	SetConsoleTitle("MAZE");                       //set the title of window

	HideCursor();                                  //hide the cursor
}

char menu_maze(void)
{
	char c;	
		system("cls");
		printf("\n\n\n\n\n\n\n\n");
		printf("                                                    ………………^welcome to the automatically maze^………………\n\n");
		printf("                                                   ************************************************************\n\n");
		printf("                                                   *********************** 1.start game ***********************\n\n");
		printf("                                                   ********************* 2.exit the game **********************\n\n");
		printf("                                                   ************************************************************\n\n");
		c = getch();
		return c;
}

void startgame()
{
	
	/*for (int i = 0; i < M; i++)
		for (int j = 0; i < N; j++)
			visited[i][j] = 0;*/
	stack<int>x1;                                      //use the stack to memory the ordinate
	stack<int>y1;                                      //use the stack to memory the abscassa
	local_x = 0;
	local_y = 0;
	x1.push(local_x);
	y1.push(local_y);

	system("cls");
	init_maze();
	gotoxy(2 * local_y + 2, local_x + 2);
	printf("★");
	gotoxy(2*(N-1)+2,M);
	printf("  ");
	path[38][80] = 'h';
	//visited[local_x][local_y] == 1;
	while (local_x!=38||local_y!=80)
	{
		Sleep(10);
		if (path[local_x][local_y + 1] != 'f'&&local_y<80)
		{
			if (local_x < 0 || local_y + 1 < 0)
				continue;
			local_y = local_y + 1;

			x1.push(local_x);
			y1.push(local_y);
			path[local_x][local_y ] = 'f';
			gotoxy(2*local_y+2,local_x+2);
			printf("★");
		}
		else if (path[local_x + 1][local_y] != 'f'&&local_x<38)
		{
			if (local_x+1 < 0 || local_y  < 0)
				continue;
			local_x = local_x + 1;
			x1.push(local_x);
			y1.push(local_y);
			path[local_x][local_y] = 'f';
			gotoxy(2 * local_y + 2, local_x + 2);
			printf("★");
		}
		else if (path[local_x][local_y - 1] != 'f'&&local_y>0)
		{
			if (local_x < 0 || local_y -1 < 0)
				continue;
			local_y = local_y -1;
			x1.push(local_x);
			y1.push(local_y);
			path[local_x][local_y ] = 'f';
			gotoxy(2 * local_y + 2, local_x + 2);
			printf("★");
		}
		else if (path[local_x -1][local_y] != 'f'&&local_x>0)
		{
			if (local_x-1 < 0 || local_y  < 0)
				continue;
			local_x = local_x -1;
			x1.push(local_x);
			y1.push(local_y);
			path[local_x][local_y ] = 'f';
			gotoxy(2 * local_y + 2, local_x + 2);
			printf("★");
		}
		else 
		{
			if (x1.empty())
				break;
			gotoxy(2 * y1.top()+2, x1.top()+2);
			printf(" ");
			x1.pop();
			y1.pop();
			local_x = x1.top();
			local_y = y1.top();
			
		}
	}
	gotoxy(30, M + 5);
	printf("you success to find a path!");

}

void init_maze()
{
	int i, j;

	T = 1;
	F = 1;
	m = 0;
	n = 0;
	x = 0;
	flag = 0;

	srand((unsigned)time(NULL));           //use the system time initialize the random seeds 

	for (i = 0; i<M / 2; i++)              //initialize the array of maze
	{
		for (j = 0; j<N / 2; j++)
			maze[i][j] = 'f';
	}

	for (i = 0; i<M -1; i++)              //initialize the array of path
	{
		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 << "■";
			//value[i][j] = '■';
		}
		cout << endl;

	}
	for (i = 0; i<N + 1; i++)
		cout << "**";
	cout << endl;

	while (F)                             //create the 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]) //the are still unthrough path epi
						{
							path_up(&m, &n);
							m = i;
							n = j;
							flag--;
							break;
						}
						if (maze[m + 1][n] == maze[0][0]) //there are still unthrough path hypo
						{
							path_down(&m, &n);
							m = i;
							n = j;
							flag--;
							break;
						}
						if (maze[m][n - 1] == maze[0][0]) //there are still unthrough path in the left
						{
							path_left(&m, &n);
							m = i;
							n = j;
							flag--;
							break;
						}
						if (maze[m][n + 1] == maze[0][0]) //there are still unthrough path in 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 position that the cursor starts and ends
			{
				maze[m][n] = 't';
				path_local(m, n);
				switch (rand() % 2)
				{ 
				case 0:                                       //down
					path_down(&m, &n);
					break;
				case 1:                                       //right
					path_right(&m, &n);
				}
			}
			if (m == M / 2 - 1 && n == 0)                    //the cursor in the lower left corner
			{
				switch (rand() % 2)
				{
				case 0:                                      //up
					if (maze[m - 1][n] == maze[0][0]) break; //the path has been through
					path_up(&m, &n);
					break;
				case 1:                                      //right
					if (maze[m][n + 1] == maze[0][0]) break;
					path_right(&m, &n);
				}
			}
			if (m == 0 && n == N / 2 - 1)                    //the cursor in the lower 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 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)          //cursor on the first row
			{
				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:                                      //right
					if (maze[m][n + 1] == maze[0][0]) break;
					path_right(&m, &n);
				}
			}
			if (m != 0 && m != M / 2 - 1 && n == 0)          //cursor on the first rank
			{
				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:                                      //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)//cursor 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:                                    //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)//cursor on the lastest colum
			{
				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)//cursor on 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:                                              //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 blind alley
				if (m == 0 && n == N / 2 - 1 && maze[m][n - 1] == maze[0][0] && maze[m + 1][n] == maze[0][0]) T = 0;//right upper blind alley
				if (m == M / 2 - 1 && n == 0 && maze[m][n + 1] == maze[0][0] && maze[m - 1][n] == maze[0][0]) T = 0;//left lower blind alley
				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 blind alley
				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 line is blind alley
				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 colum is blind alley
				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 colum is blind alley
				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 si blind alley
				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 parts is blind alley
			}
		}
		if (flag == M * N / 4)
			F = 0;
	}
	/*i = M + 3;
	path[M - 2 - 1][N - 1 - 1] = 'h';
	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");
			if (path[i][j] == 'h')
				printf("4");
		}
		printf("\n");
	}*/
}

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(" ");
	//value[i][j] = ' ';
}

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(" ");
	//value[i][j] = ' ';
}

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(" ");
	//value[i][j] = ' ';
}

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(" ");
	//value[i][j] = ' ';
}

void setxy(int x, int y)
{
	gotoxy(x, y);
	printf(" ");
	//value[x][y] = ' ';
}

void path_local(int x, int y)
{
	int i, j;
	i = 4 * y + 2;
	j = 2 * x + 2;
	gotoxy(i, j);
	printf(" ");
	//value[i][j] = ' ';
	path[2 * x][2 * y] = path[0][0];
	flag++;
}



void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };// 
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}


发布了81 篇原创文章 · 获赞 25 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_43090158/article/details/101510424
今日推荐