#include "code.h"
#include <QtWidgets/QApplication>
#include<iostream>
#include<vector>
#include <QWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QPushButton>
#include <random>
using namespace std;
int randomInt(int begin, int end);
QPushButton* getButton(QWidget* parent, QString message, int r, int g, int b, int x, int y);
int pixelWidth = 20; //显示的矩形宽度
int pixelHeight = 20; //显示的矩形高度
// 定义一个枚举类型来表示迷宫元素和状态
enum MazeElement {
ENTRANCE, // 1. 入口
EXIT, // 2. 出口
ENTRANCE_EXIT, // 3. 出入口
NEXT_TASK, // 4. 下一个任务迷宫
NO_OBSTACLE, // 5. 没有障碍
HAS_OBSTACLE, // 6. 有障碍
PLAYER_POSITION // 7. 玩家位置
};
enum Type {
ORDERINARY, //普通迷宫
TASKS //任务迷宫
};
class Point {
public:
int x, y;
};
//1.空格 2.墙 3.入口 4.终点 5.双入口
/*
1.入口
2.出口
3.出入口
4.下一个任务迷宫
5.没有障碍
6 有障碍
7.玩家位置
*/
int randomInt(int begin, int end) {
// 创建随机数生成器
random_device rd; // 获取随机数种子
mt19937 gen(rd()); // 创建梅森旋转算法随机数生成器
uniform_int_distribution<> dis(begin, end); // 创建均匀分布
// 生成随机数并返回
return dis(gen);
}
bool ifRange(int x, int y,int row,int col) {
if (x ==0)
return false;
if (y==0)
return false;
if (x == col -1)
return false;
if (y == row - 1)
return false;
return true;
}
bool ifPoint(vector<Point> points, int x, int y) {
for (const auto& point : points) {
if (point.x == x && point.y == y) {
return true; // 找到匹配的点,返回 true
}
}
return false; // 如果遍历完都没有找到,返回 false
}
class Maze {
public:
Type type;//迷宫类型
int row, col;//迷宫的行数和列数
int layer;//所属层
int** data;
Maze();
Maze(int row,int col);
void fill();//清空迷宫
void path();//随机生成路径
void pass(int row, int begin, int end);
};
void Maze::pass(int row, int begin, int end) {
for (int i = begin; i <= end; i++) {
data[row][i] = NO_OBSTACLE; // 没有障碍
}
}
void Maze::fill() {
for (int i = 0; i < row; ++i)
for (int j = 0; j < col; ++j) {
data[i][j] = HAS_OBSTACLE; // 初始化全部障碍
}
}
bool ifAcross(int lastCol1, int lastCol2, int col1, int col2) {
if (col1 >= lastCol1 && col1<= lastCol2)
return true;
if (col2 >= lastCol1 && col2 <= lastCol2)
return true;
return false;
}
/*
//vector<Point> points;
//int rowX = 1, colY = 1;
//points.push_back({ rowX, colY });
//while (points.size() <(2*row+col)) {
// int rand = randomInt(1, 3);
// if (rand == 1) {
// rowX += 1;
// if(!ifPoint(points, rowX, colY))
// if (ifRange(rowX, colY, row, col)) {
// points.push_back({ rowX, colY });
// }
// else {
// rowX -= 1;
// }
//
// }
// if (rand == 2) {
// rowX -= 1;
// if (!ifPoint(points, rowX, colY))
// if (ifRange(rowX, colY, row, col)) {
// points.push_back({ rowX, colY });
//
// }
// else {
// rowX += 1;
// }
//
// }
// if (rand == 3) {
// colY += 1;
// if (!ifPoint(points, rowX, colY))
// if (ifRange(rowX, colY, row, col)) {
// points.push_back({ rowX, colY });
// }
// else {
// colY -= 1;
// }
//
// }
//
//}
//for (const auto& point : points) {
// data[point.x][point.y]=NO_OBSTACLE;
//}
*/
void Maze::path() { //随机生成路径
int beforeCol1, beforeCol2;
int col1, col2;
int i;
for (i = 1; i < row - 1; i++) { //第一行和倒数第二行 随机挖除 然后
if(i ==1){
beforeCol1= randomInt(1, col/4); //随机起点
beforeCol2 = randomInt(beforeCol1, beforeCol1+4); //随机终点
pass(i,beforeCol1, beforeCol2);
}
else { //否则
col1 = randomInt(1, col / 4); //随机起点
col2= randomInt(col1, col1 +4); //随机终点
while (!ifAcross(beforeCol1, beforeCol2, col1, col2)) {
col1 = randomInt(1, col / 4); //随机起点
col2 = randomInt(col1, col1 +4);// 随机终点
}
pass(i, col1, col2); //设置通路
beforeCol1 = col1;
beforeCol2 = col2;
}
}
}
Maze::Maze(int row, int col) {
this->row = row;
this->col = col;
// 为data申请内存
data = new int* [row]; // 申请行指针数组
for (int i = 0; i < row; ++i) {
data[i] = new int[col]; // 申请每一行的列数组
}
}
Maze::Maze() {
this->row = 0;
this->col = 0;
}
class MazeManager { //迷宫管理器
public:
int row;//玩家位置
int col;//玩家位置
int layer;//所属层
int mazeNum = 1;
MazeManager();
vector<Maze> mazes; //存储多个迷宫
};
MazeManager::MazeManager() {
int i;
for (i = 0; i < mazeNum; i++) {
Maze *maze = new Maze(40,40);
maze->fill();//填充
maze->path();//随机生成路径
mazes.push_back(*maze); //将迷宫加入迷宫数组
}
cout << "asd";
}
07-20
1万+
1万+
11-01
270
270

被折叠的 条评论
为什么被折叠?



