C++ 迷宫游戏

介绍

本程序是根据广度优先遍历算法的思想设计的一款迷宫游戏,游戏设计了两种模式一种自动游戏模式,一种手动模式。因为项目在 Linux 开发,需要在 Windows 开发的,请查看源代码中需要修改地方的备注。

截图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码
#include <iostream>
#include <cstdlib>			//标准库
#include <unistd.h>			//延时函数
#include <stdio.h>			//getchar	
#include <ctime>			
#include <termios.h>		//终端设置

#define MAX_X 20
#define MAX_Y 30
bool flag = false;
bool slow = false;
bool autogame = true;

using namespace std;

int maze[MAX_X][MAX_Y];		//迷宫

//路线栈
class stack_of_maze{
private:
	//记录迷宫坐标
	struct node
	{
		int x;
		int y;
		char direction;  	//上一步路径(如何来的)
		node* next;
	};
	node* head;
public:
	stack_of_maze(){
		head = NULL;
	}

	~stack_of_maze(){
		node* p = head;
		while(head!=NULL){
			head = head->next;
			delete p;
			p = head;
		}
	}

	//压栈
	void push(int xx,int yy,char ddirection){
		node* new_node = new node;
		if(new_node!=NULL){
			new_node->x = xx;
			new_node->y = yy;
			new_node->direction = ddirection;
			new_node->next = NULL;

			if(head==NULL)
				head = new_node;
			else{
				new_node->next = head;
				head = new_node;
			}
		}
		else
			cout<<"内存分配失败"<<endl;

	}

	//出栈
	node* pop(int& xx,int& yy){
		if(head!=NULL){
			node* p = head;
			head = head->next;
			xx = p->x;
			yy = p->y;
			delete p;
		}
		return head;
	}

	void print(){
		if(head!=NULL){
			node* p = head;
			while(p!=NULL){
				cout<<" "<<p->x<<" "<<p->y<<" "<<p->direction<<endl;
				p = p->next;
			}
		}
		else
			cout<<"栈为空,打印失败"<<endl;
	}
};

//创建迷宫
void createMaze(){
	int maxway = MAX_X * MAX_Y;		//最大通路
	int x,y;

	for(x=0;x<MAX_X;x++)
		for(y=0;y<MAX_Y;y++)
			maze[x][y] = 1;			//先填充迷宫

	srand((unsigned)time(NULL));	//随机函数种子,以时间为参数
	for(int i=0;i<maxway;i++)		//随机构建迷宫通路
	{
		x = rand() % (MAX_X-2) + 1;
		y = rand() % (MAX_Y-2) + 1;
		maze[x][y] = 0;
	}	

	maze[1][1] = 0; 				//入口
	maze[MAX_X-2][MAX_Y-2] = 0;		//出口

	maze[0][1] = 3;
 	maze[MAX_X-1][MAX_Y-2] = 0;
}

//输出迷宫
void printMaze(){
	int x,y;
	system("clear");				//windows下使用system("cls")
	//cout<<endl;
	for(x=0;x<MAX_X;x++)
	{
		for(y=0;y<MAX_Y;y++)
		{
			if(maze[x][y]==0){cout<<"  ";continue;}		//通路
			if(maze[x][y]==1){cout<<"■";continue;}		//墙
			if(maze[x][y]==2){cout<<"×";continue;}		//死胡同
			if(maze[x][y]==3){cout<<"↓";continue;}		//向下走
			if(maze[x][y]==4){cout<<"→";continue;}
			if(maze[x][y]==5){cout<<"←";continue;}
			if(maze[x][y]==6){cout<<"↑";continue;}
			if(maze[x][y]==7){cout<<"※";continue;}		//当前站立位置
		}
		cout<<endl;
	}
	if(slow){
		sleep(1);											//延时函数
	}
}

void check(stack_of_maze &s){
	int temp[MAX_X][MAX_Y];

	for(int x=0;x<MAX_X;x++)
		for(int y=0;y<MAX_Y;y++)
			temp[x][y] = maze[x][y];

	int x=1,y=1;					//出发点	
	while(1){
		temp[x][y] = 2;

		//向下
		if(temp[x+1][y]==0){
			s.push(x,y,'D');
			temp[x][y] = 3;			//在当前位置做一个向下的标志
			x = x + 1;
			temp[x][y] = 7;			//当前位置
			if((x==MAX_X-1)&&(y==MAX_Y-2)){
				flag = true;
				return;
			}
			else
				continue;
		}

		//向右
		if(temp[x][y+1]==0){
			s.push(x,y,'R');
			temp[x][y] = 4;			//在当前位置做一个向右的标志
			y = y + 1;
			temp[x][y] = 7;
			if((x==MAX_X-1)&&(y==MAX_Y-2)){
				flag = true;
				return;
			}
			else
				continue;
		}

		//向上
		if(temp[x-1][y]==0){
			s.push(x,y,'U');
			temp[x][y] = 6;			//在当前位置做一个向上的标志
			x = x - 1;
			temp[x][y] = 7;
			if((x==MAX_X-1)&&(y==MAX_Y-2)){
				flag = true;
				return;
			}
			else
				continue;
		}

		//向左
		if(temp[x][y-1]==0){
			s.push(x,y,'L');
			temp[x][y] = 5;			//在当前位置做一个向右的标志
			y = y - 1;
			temp[x][y] = 7;
			if((x==MAX_X-1)&&(y==MAX_Y-2)){
				flag = true;
				return;
			}
			else
				continue;
		}

		//上下左右不通,则回退
		if(s.pop(x,y)==NULL && temp[x-1][y]!=0 && temp[x][y-1]!=0 && temp[x][y+1]!=0 && temp[x+1][y]!=0){
			temp[0][1] = 7;
			if(temp[1][1]!=1)
				temp[1][1] = 2;
			return;
		}
	}
}

//输入,windows下可以使用#incldue<conio.h>替代此函数
char getch(){
	char ch;   
    static struct termios oldt, newt;				//保存原有终端属性和新设置的终端属性
    tcgetattr( STDIN_FILENO, &oldt);				//获得终端原有属性并保存在结构体oldflag

    //设置新的终端属性
    newt = oldt;
    newt.c_lflag &= ~(ICANON);          
    tcsetattr( STDIN_FILENO, TCSANOW, &newt);

    //取消回显
    system("stty -echo");
    ch = getchar();
    system("stty echo");    

    tcsetattr( STDIN_FILENO, TCSANOW, &oldt);		//让终端恢复为原有的属性
	return ch;
}

void move(){
	int x=1,y=1;					//出发点	
	while(1){
		switch(getch()){
			case 's':
				if(maze[x+1][y]==0){
					maze[x][y] = 0;
					x = x + 1;
					maze[x][y] = 7;			//当前位置
					printMaze();
					if((x==MAX_X-1)&&(y==MAX_Y-2)){
						cout<<"\n\n              成功走出"<<endl;
						return;
					}
				}				
				break;
			case 'd':
				if(maze[x][y+1]==0){
					if(maze[x][y+1]==0){
						maze[x][y] = 0;
						y = y + 1;
						maze[x][y] = 7;
						printMaze();
						if((x==MAX_X-1)&&(y==MAX_Y-2)){
							cout<<"\n\n              成功走出"<<endl;
							return;
						}
					}		
				}
				
				break;
			case 'w':
				if(maze[x-1][y]==0){
					maze[x][y] = 0;
					x = x - 1;
					maze[x][y] = 7;
					printMaze();
					if((x==MAX_X-1)&&(y==MAX_Y-2)){
						cout<<"\n\n              成功走出"<<endl;
						return;
					}
				}	
				break;
			case 'a':
				if(maze[x][y-1]==0){
					maze[x][y] = 0;
					y = y - 1;
					maze[x][y] = 7;
					printMaze();
					if((x==MAX_X-1)&&(y==MAX_Y-2)){
						cout<<"\n\n              成功走出"<<endl;
						return;
					}
				}		
				break;
		}
	}
}

void autoMove(stack_of_maze &s){
	int x=1,y=1;					//出发点	
	while(1){
		maze[x][y] = 2;

		//向下
		if(maze[x+1][y]==0){
			s.push(x,y,'D');
			maze[x][y] = 3;			//在当前位置做一个向下的标志
			x = x + 1;
			maze[x][y] = 7;			//当前位置
			if(slow)
				printMaze();
			if((x==MAX_X-1)&&(y==MAX_Y-2)){
				s.push(x,y,'*');
				cout<<"\n\n              成功走出"<<endl;
				return;
			}
			else
				continue;
		}

		//向右
		if(maze[x][y+1]==0){
			s.push(x,y,'R');
			maze[x][y] = 4;			//在当前位置做一个向右的标志
			y = y + 1;
			maze[x][y] = 7;
			if(slow)
				printMaze();
			if((x==MAX_X-1)&&(y==MAX_Y-2)){
				s.push(x,y,'*');
				cout<<"\n\n              成功走出"<<endl;
				return;
			}
			else
				continue;
		}

		//向上
		if(maze[x-1][y]==0){
			s.push(x,y,'U');
			maze[x][y] = 6;			//在当前位置做一个向上的标志
			x = x - 1;
			maze[x][y] = 7;
			if(slow)
				printMaze();
			if((x==MAX_X-1)&&(y==MAX_Y-2)){
				s.push(x,y,'*');
				cout<<"\n\n              成功走出"<<endl;
				return;
			}
			else
				continue;
		}

		//向左
		if(maze[x][y-1]==0){
			s.push(x,y,'L');
			maze[x][y] = 5;			//在当前位置做一个向右的标志
			y = y - 1;
			maze[x][y] = 7;
			if(slow)
				printMaze();
			if((x==MAX_X-1)&&(y==MAX_Y-2)){
				s.push(x,y,'*');
				cout<<"\n\n              成功走出"<<endl;
				return;
			}
			else
				continue;
		}

		//上下左右不通,则回退
		if(s.pop(x,y)==NULL && maze[x-1][y]!=0 && maze[x][y-1]!=0 && maze[x][y+1]!=0 && maze[x+1][y]!=0){
			cout<<"\n\n              没有找到合适的路径"<<endl;
			maze[0][1] = 7;
			if(maze[1][1]!=1)
				maze[1][1] = 2;
			return;
		}
	}
}

void menu();

void gamestart(){
	flag = false;
	while(!flag){
		stack_of_maze stack;			//定义一个栈的对象,用来记录行走路线	
		createMaze();
		check(stack);
		system("clear");
		cout<<"\t*                loading.              *"<<endl;
		system("clear");
		cout<<"\t*                loading..             *"<<endl;
		system("clear");
		cout<<"\t*                loading...            *"<<endl;
	}
	printMaze();						//输出当前迷宫的初始状态
	cout<<"\n\n              输入enter键继续"<<endl;
	getchar();
	if(!autogame){
		move();
		cout<<"\n\n              输入enter键继续"<<endl;
		getchar();
		menu();
	}
	else{
		stack_of_maze stack1;				
		autoMove(stack1);					//行走中……
	}	
	printMaze();							//输出迷宫的最终状态
	cout<<"\n\n              输入enter键继续"<<endl;
	getchar();
	menu();
}

void menu(){
	system("clear");
	int num;
	cout<<"\t****************************************"<<endl;
	cout<<"\t*                                      *"<<endl;
	cout<<"\t*               1.查看路径             *"<<endl;
	cout<<"\t*                                      *"<<endl;
	cout<<"\t*               2.自动进行             *"<<endl;
	cout<<"\t*                                      *"<<endl;
	cout<<"\t*               3.自行游戏             *"<<endl;
	cout<<"\t*                                      *"<<endl;
	cout<<"\t*               4.退出游戏             *"<<endl;
	cout<<"\t*                                      *"<<endl;
	cout<<"\t****************************************"<<endl;
	slow = false;
	switch(getch()){
		case '1':
			autogame = true;
			gamestart();break;
		case '2':
			autogame = true;
			slow = true;
			gamestart();
			break;
		case '3':
			autogame = false;
			gamestart();
			break;
		case '4':
			exit(1);break;
		default:
			cout<<"\n\n              错误操作,输入enter返回!"<<endl;
			getchar();
			menu();
	}
	getchar();
}

int main(int argc,char** argv){
	menu();
	return 0;
}
  • 30
    点赞
  • 208
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
深度优先搜索(Depth First Search, DFS)是一种用于图和树的遍历算法。它可以用来生成迷宫。下面是一个用C语言实现DFS迷宫生成的简单示例程序: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> // 定义迷宫的行数和列数 #define ROW 10 #define COL 10 // 定义迷宫的墙壁和路径符号 #define WALL '#' #define PATH ' ' // 定义四个方向的移动向量:上、右、下、左 int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, 1, 0, -1}; char maze[ROW][COL]; // 迷宫地图 // 初始化迷宫地图,所有位置都设置为墙壁 void initMaze() { for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { maze[i][j] = WALL; } } } // 检查位置(x, y)是否在迷宫范围内 int isValid(int x, int y) { return (x >= 0 && x < ROW && y >= 0 && y < COL); } // 以点(x, y)为起点,进行DFS遍历生成迷宫 void DFS(int x, int y) { maze[x][y] = PATH; // 将当前位置标记为路径 // 随机打乱四个方向的顺序 int dirs[4] = {0, 1, 2, 3}; for (int i = 0; i < 4; i++) { int j = rand() % 4; int temp = dirs[i]; dirs[i] = dirs[j]; dirs[j] = temp; } // 在四个方向上进行探索 for (int i = 0; i < 4; i++) { int nx = x + dx[dirs[i]]; int ny = y + dy[dirs[i]]; // 判断新位置(nx, ny)是否有效及是否是墙壁 if (isValid(nx, ny) && maze[nx][ny] == WALL) { int wallCount = 0; // 统计周围的墙壁数量 for (int j = 0; j < 4; j++) { int adjX = nx + dx[j]; int adjY = ny + dy[j]; if (isValid(adjX, adjY) && maze[adjX][adjY] == WALL) { wallCount++; } } // 如果周围有多于1个的墙壁,则打通新位置(nx, ny) if (wallCount >= 2) { maze[nx][ny] = PATH; DFS(nx, ny); // 递归探索新位置 } } } } // 打印迷宫地图 void printMaze() { for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { printf("%c ", maze[i][j]); } printf("\n"); } } int main() { srand(time(0)); // 设置随机数种子,以保证每次生成的迷宫都不一样 initMaze(); // 初始化迷宫地图 DFS(0, 0); // 以起点(0, 0)开始进行DFS生成迷宫 printMaze(); // 打印迷宫地图 return 0; } ``` 这是一个简单的使用DFS算法生成迷宫的示例程序。该程序使用一个二维字符数组`maze`来表示迷宫地图,`'#'`表示墙壁,`' '`表示路径。通过递归调用`DFS`函数,从起点开始生成迷宫。在`DFS`函数中,首先将当前位置标记为路径,然后随机打乱四个方向的顺序,从每个方向上递归探索新的位置,直到所有位置都被访问完毕。最后,调用`printMaze`函数打印生成的迷宫地图。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麦迪尔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值