c++迷宫通路生成算法

#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";
}






















评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

超维Ai编程

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

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

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

打赏作者

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

抵扣说明:

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

余额充值