C++自动生成迷宫游戏

 更新时间:2020年03月20日 07:17:05   作者:qq_40399247  
这篇文章主要为大家详细介绍了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();

 }
}

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

相关文章

  • 浅谈c++11线程的互斥量

    浅谈c++11线程的互斥量

    互斥量是个类对象,理解成一把锁(保护共享数据,其他想操作共享数据的线程必须等待解锁),互斥量使用要小心,保护数据不多也不少,少了则没达到保护效果,多了则影响效率。本文将介绍c++11线程的互斥量,感兴趣的同学,可以参考下。
    2021-06-06
  • C语言函数封装及变量的作用域

    C语言函数封装及变量的作用域

    这篇文章主要介绍了C语言函数封装及变量的作用域,以及分享一些字符串的相关实战练习,字符串转整数、整数转字符串、浮点数转字符串、字符串转浮点数、判断平年闰年、技术字符串长度等等。下文详细内容需要的小伙伴可以参考一下
    2022-05-05
  • 教你用C语言实现三子棋

    教你用C语言实现三子棋

    这篇文章主要为大家详细介绍了C语言实现简单三子棋程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • 深入理解C++模板如何实现多态思想

    深入理解C++模板如何实现多态思想

    这篇文章主要为大家详细介绍了C++模板如何实现多态的相关资料,文中的示例代码讲解详细,对我们深入了解C++有一定的帮助,感兴趣的可以了解一下
    2023-03-03
  • 解读C语言非void函数却没有return会怎么样

    解读C语言非void函数却没有return会怎么样

    这篇文章主要介绍了解读C语言非void函数却没有return会怎么样的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • C++ OpenCV实现boxfilter方框滤波的方法详解

    C++ OpenCV实现boxfilter方框滤波的方法详解

    box filter的作用很简单,即对局部区域求平均,并把值赋给某个点,一般我们赋给区域中心。本文将用C++实现boxfilter方框滤波,需要的可以了解一下
    2022-10-10
  • 深入探讨C语言中局部变量与全局变量在内存中的存放位置

    深入探讨C语言中局部变量与全局变量在内存中的存放位置

    本篇文章是对在C语言中局部变量与全局变量在内存中的存放位置进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C语言实现歌手大奖赛计分程序

    C语言实现歌手大奖赛计分程序

    这篇文章主要为大家详细介绍了C语言实现歌手大奖赛计分程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • C++中类模板的应用你了解多少

    C++中类模板的应用你了解多少

    这篇文章主要为大家详细介绍了C++中类模板的应用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • C++ 中的异常抛出和捕获方式

    C++ 中的异常抛出和捕获方式

    这篇文章主要介绍了C++ 中的异常抛出和捕获方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07

最新评论