C++自动生成迷宫游戏

本文实例为大家分享了C++实现迷宫游戏的具体代码,供大家参考,具体内容如下

运用并查集自动生成迷宫地图,并运用队列和栈寻找迷宫通路并打印出来

#include<stdlib.h>
#include<iostream>
#include<time.h>
#include<queue>
#include<stack>
using namespace std;
using std::queue;
using std::stack;
typedef struct Point
{
 int x;
 int y;
 int d;//方向 若方向为-1,则表示起点
}Point;
queue<Point> mqueue;
stack<Point> mstack;
Point pos, pos1;
int m, n;//迷宫行(tm-1)/2和列(tn-1)/2
int tm, tn;//实际作图
int x, y, tx1, tx2, ty1, ty2;//点坐标
int d;
int s[10000000];
int maze[1000][1000], mark[1000][1000];//最大迷宫
int sign[4][2] = { { -1,0 },{ 1,0 },{ 0,-1 },{ 0,1 } };//上下左右四个方向 0上 1下 2上 3下
Point start;
int Find_x(int x);
void unionSets(int node1, int node2);
void Init();
int getAdd(int x, int y);
void foundpath();
void fixmaze();
int connected(int node1, int node2);
void Findpath();
void changemaze();
int main()
{
 Init();
 cout << "请输入迷宫规模2x-1,2y-1:(x y)" << endl;
 cin >> m >> n;
 tm = m * 2 + 1;
 tn = n * 2 + 1;
 start.x = 1;
 start.y = 1;
 start.d = -1;
 mqueue.push(start);
 for (int i = 0; i < tm; i++)
 {
 for (int j = 0; j < tn; j++)
 {
  maze[i][j] = 1;
  mark[i][j] = 0;
 }
 }
 for (int i = 1; i < tm - 1; i += 2)
 {
 for (int j = 1; j < tn - 1; j += 2)
  maze[i][j] = 0;
 }
 srand(time(NULL));
 foundpath();
 fixmaze();
 cout << "迷宫全图:" << endl;
 for (int i = 0; i < tm; i++)
 {
 for (int j = 0; j < tn; j++)
 {
  if (maze[i][j] == 1)
  cout << "▇";
  else if (maze[i][j] == 0) cout << "□";
 }
 cout << endl;
 }
 Findpath();
 changemaze();
 cout << "找到的通路:“..”表示:" << endl;
 for (int i = 0; i < tm; i++)
 {
 for (int j = 0; j < tn; j++)
 {
  if (maze[i][j] == 1)
  cout << "▇";
  else if (maze[i][j] == 0) cout << "□";
  else if (maze[i][j] == -1) cout << "..";

 }
 cout << endl;
 }
 system("pause");
 return 0;
}
int connected(int node1, int node2)
{
 return Find_x(node1) == Find_x(node2);
}
int Find_x(int x)
{
 if (s[x] < 0)
 return x;
 else
 return Find_x(s[x]);

};
void unionSets(int node1, int node2)
{
 int root1 = Find_x(node1);
 int root2 = Find_x(node2);
 if (root1 == root2)
 return;
 if (s[root2] < s[root1])
 s[root1] = root2;
 else {
 if (s[root1] == s[root2])
  s[root1]--;
 s[root2] = root1;
 }
};
int getAdd(int x, int y)
{
 return (x*tn + y);
};
void Init()
{
 for (int i = 0; i < 10000000; ++i)
 s[i] = -1;
};
void foundpath()
{
 while (connected(getAdd(1, 1), getAdd(tm - 2, tn - 2)) != 1)
 {
 do
 {
  x = rand() % (tm - 2) + 1;
  y = rand() % (tn - 2) + 1;
 } while (maze[x][y] == 0);
 d = x % 2;
 if (d == 0)
 {
  tx1 = x + 1;
  ty1 = y;
  tx2 = x - 1;
  ty2 = y;
  if (connected(getAdd(tx1, ty1), getAdd(tx2, ty2)) != 1)
  {
  maze[x][y] = 0;
  unionSets(Find_x(getAdd(tx1, ty1)), Find_x(getAdd(tx2, ty2)));
  }
 }
 else if (d == 1)
 {
  tx1 = x;
  ty1 = y + 1;
  tx2 = x;
  ty2 = y - 1;
  if (connected(getAdd(tx1, ty1), getAdd(tx2, ty2)) != 1)
  {
  maze[x][y] = 0;
  unionSets(Find_x(getAdd(tx1, ty1)), Find_x(getAdd(tx2, ty2)));
  }
 }
 }
}
void fixmaze()
{
 for (int i = 1; i < tm - 1; i++)
 {
 for (int j = 1; j < tn - 1; j++)
 {
  if (maze[i - 1][j] == 1 && maze[i + 1][j] == 1 && maze[i][j + 1] == 1 && maze[i][j - 1] == 1)
  {
  maze[i][j] = 1;
  }
 }
 }
 for (int i = 1; i < tm - 1; i++)
 {
 for (int j = 1; j < tn - 1; j++)
 {
  if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0)
  {
  maze[i][j] = 1;
  }
  if (maze[i - 1][j - 1] == 1 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0)
  {
  maze[i][j] = 1;
  }
  if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 1 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0)
  {
  maze[i][j] = 1;
  }
  if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 1 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0)
  {
  maze[i][j] = 1;
  }
  if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 1 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0)
  {
  maze[i][j] = 1;
  }
  if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 1 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0)
  {
  maze[i][j] = 1;
  }
  if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 1 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0)
  {
  maze[i][j] = 1;
  }
  if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 1 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0)
  {
  maze[i][j] = 1;
  }
  if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 1 && maze[i + 1][j + 1] == 0)
  {
  maze[i][j] = 1;
  }
  if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 1)
  {
  maze[i][j] = 1;
  }
 }
 }//局部优化,防止出现大面积通路
}
void Findpath()
{
 int flag = 0;
 int i, j;
 while (!mqueue.empty())
 {
 i = mqueue.front().x;
 j = mqueue.front().y;
 mark[i][j] = 1;
 for (int k = 0; k < 4; k++)
 {
  if (mark[i + sign[k][0]][j + sign[k][1]] == 0 && maze[i + sign[k][0]][j + sign[k][1]] == 0)
  {
  pos.x = i + sign[k][0];
  pos.y = j + sign[k][1];
  pos.d = k;
  mark[pos.x][pos.y] = 1;
  mqueue.push(pos);
  if (mqueue.back().x == tm - 2 && mqueue.back().y == tn - 2)
  {
   mstack.push(mqueue.front());
   mstack.push(mqueue.back());
   flag = 1;
   break;
  }
  }
 }
 if (flag) break;
 mstack.push(mqueue.front());
 if (!mqueue.empty())
  mqueue.pop();
 }
}
void changemaze()
{
 int i, j, k;
 i = mstack.top().x;
 j = mstack.top().y;
 k = mstack.top().d;
 maze[i][j] = -1;
 while (mstack.size()>0)
 {
 if (mstack.top().x == i - sign[k][0] && mstack.top().y == j - sign[k][1])
 {
  i = i - sign[k][0];
  j = j - sign[k][1];
  k = mstack.top().d;
  maze[i][j] = -1;
  if (!mstack.empty())
  mstack.pop();
 }
 else if (!mstack.empty())
  mstack.pop();

 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C++基于prim实现迷宫生成

    本文实例为大家分享了C++实现迷宫生成的具体代码,供大家参考,具体内容如下 只用到了c++中的vector,其余的和纯C差别不大,纯C可能需要手动弄一个vector太繁琐了不太想弄. 看了迷宫的一些算法,prim还是比较好看的,网上的代码python c#居多,而且不太容易搞懂,那我在这里用C++(大部分C)实现了这个目的 prim算法:随机Prim算法生成的迷宫岔路较多,整体上较为自然而又复杂,算法核心为(根据维基百科). 1.让迷宫全是墙. 2.选一个单元格作为迷宫的通路(我一般选择起点),

  • C++实现迷宫游戏

    本文实例为大家分享了C++实现迷宫游戏的具体代码,供大家参考,具体内容如下 #include<iostream> using namespace std; //点为2表示迷宫图为"█",点为0表示迷宫图为" " int migo[9][9]= { {2, 2, 2, 2, 2, 2, 2, 2, 2}, {2, 0, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 2, 2, 0, 2, 2, 0, 2}, {2, 0, 2, 0, 0, 2,

  • C++随机生成迷宫算法

    本文实例为大家分享了C++随机生成迷宫的具体代码,供大家参考,具体内容如下 我们今天来做一个迷宫游戏.在其中有几个要领: 1.方向的控制 我们建立的迷宫是以坐标的形式出现的,越往上x坐标越小,越往左y坐标越小,这雨平面直角坐标系不同,要注意! 2.随机生成算法: void init_maze(void); //初始化迷宫 void gotoxy(int x, int y); //移动光标 void path_up(int *x, int *y); //上构路径 void path_down(in

  • C++实现迷宫小游戏

    介绍 本程序是根据广度优先遍历算法的思想设计的一款迷宫游戏,游戏设计了两种模式一种自动游戏模式,一种手动模式.因为项目在 Linux 开发,需要在 Windows 开发的,请查看源代码中需要修改地方的备注. 截图 代码 #include <iostream> #include <cstdlib> //标准库 #include <unistd.h> //延时函数 #include <stdio.h> //getchar #include <ctime&g

  • C++迷宫问题的求解算法

    本文实例为大家分享了C++实现迷宫的具体代码,供大家参考,具体内容如下 一. 实验目的: (1) 熟练掌握链栈的基本操作及应用. (2) 利用链表作为栈的存储结构,设计实现一个求解迷宫的非递归程序. 二.实验内容: [问题描述] 以一个m×n的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍.设计一个程序,对信任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论. [基本要求] 首先实现一个链表作存储结构的栈类型,然后编写一个求解迷宫的非递归程序.求得的通路以三元组(i,j,d)的形

  • 迷宫游戏控制台版C++代码

    本文实例分享了C++设计的一个可以调整大小的迷宫游戏,给定迷宫的入口.如果存在出口,程序能够显示行走的路径,并最终到达出口,并输出"成功走出迷宫":如果不存在出口,程序也能够显示行走的过程,并最终回退到入口,并输出"回退到入口". //这是一个迷宫游戏 #include<iostream> #include<ctime> #include<cstdlib>/*用于生成随机数,形成随机变化的迷宫*/ #include<ioma

  • C++利用递归实现走迷宫

    本文实例为大家分享了C++利用递归实现走迷宫的具体代码,供大家参考,具体内容如下 要求: 1.将地图的数组保存在文件中,从文件中读取行列数 2..动态开辟空间保存地图 3..运行结束后再地图上标出具体的走法 说明: 1.文件中第一行分别放置的是地图的行数和列数 2.其中1表示墙,即路不通,0表示路,即通路 3.程序运行结束后用2标记走过的路径 4.当走到"死胡同"时用3标记此路为死路 5.每到一个点,按照 左 上 右 下 的顺序去试探 6.没有处理入口就是"死胡同"

  • C++控制台实现随机生成路径迷宫游戏

    本程序是在控制台下随机生成迷宫路径的一个C++程序,可以通过修改宏定义 M 和 N 的值来修改迷宫的长度和宽度,运行程序后 按1开始游戏 按2退出游戏,游戏入口在左上角,出口在右下角,人物(星星)到达右下角出口提示成功闯关. #include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> #include<iostream.h> #include<ctime

  • C++迷宫的实现代码

    本文实例为大家分享了C++实现迷宫游戏的具体代码,供大家参考,具体内容如下 //文件的输入,有墙 #include<iostream> #include<ctime> #include<cstdlib> #include<fstream> using namespace std; const int max1=100*100; //加入墙 const int max2=102; bool value[max2][max2]; //记录是否被访问过 int m

  • C++利用循环和栈实现走迷宫

    本文实例为大家分享了C++利用循环和栈实现走迷宫的具体代码,供大家参考,具体内容如下 要求: 1.将地图的数组保存在文件中,从文件中读取行列数 2..动态开辟空间保存地图 3..运行结束后再地图上标出具体的走法 说明: 1.文件中第一行分别放置的是地图的行数和列数 2.其中1表示墙,即路不通,0表示路,即通路 3.程序运行结束后用2标记走过的路径 4.当走到"死胡同"时用3标记此路为死路 5.每到一个点,按照 左 上 右 下 的顺序去试探 6.没有处理入口就是"死胡同&quo

  • C++实现简单走迷宫的代码

    本文实例为大家分享了C++实现走迷宫的具体代码,供大家参考,具体内容如下 用n*n个小方格代表迷宫,每个方格上有一个字符0或1,0代表这个格子不能走,1代表这个格子可以走.只能一个格子一个走,而且只能从一个格子向它的上.下.左.右四个方向走,且不能重复.迷宫的入口和出口分别位于左上角和右下角,存在唯一的一条路径能够从入口到达出口,试着找出这条路径. 例如,下图是一个迷宫,红色表示走出迷宫的一条路径 输入:入口坐标(startX,startY),出口坐标(endX,endY) 输出:如果存在这样一

随机推荐