gpt4 book ai didi

c++ - 生成随机 "maze"-- 3d 数组 C++

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:11 25 4
gpt4 key购买 nike

所以我在学习 C++ 的同时正在做学校作业。我不是在寻找给我的代码,而是在帮助理解/提出解决这个问题的正确算法。

我需要创建一个由 1 和 0 组成的 (5x5x5) 3d 迷宫。随机填充它(除了 0,0,0 是开始时的 1 和 4,4,4 是结束时的 1。

这是我所做的。我制作了一个立方体对象:

#include "cube.h"
Cube :: Cube(int cube_value)
{
cube_value = 0;
chk_up = false;
chk_down = false;
chk_left = false;
chk_right = false;
chk_front = false;
chk_back = false;
}
Cube :: ~Cube(void){}

在我的迷宫管理类中我这样初始化

PathFinder::PathFinder()
{
// initializing / sizing 5x5x5 Maze
Maze.resize(5);
for(int y = 0; y < 5 ; ++y)
{
Maze[y].resize(5);
for(int z = 0; z<5 ; ++z)
{
Maze[y][z].resize(5);
}
}

int at_x = 0;
int at_y = 0;
int at_z = 0;
}

该类的标题: #include "PathfinderInterface.h" #include "cube.h"

class PathFinder : public PathfinderInterface {
private:
int at_x;
int at_y;
int at_z;
public:
vector<vector<vector<Cube> > > Maze;

PathFinder();
virtual ~PathFinder();

string getMaze();

void createRandomMaze();

bool importMaze(string file_name);

vector<string> solveMaze();
};

所以我正在尝试填充它,这就是我所拥有的,它可能没有多大意义:

void PathFinder :: fillmaze()
{
Maze[0][0][0].cube_value = 1;
Maze[4][4][4].cube_value = 1;
int atx = 0 , aty = 0 , atz = 0;
while(atx<5 && aty < 5 && atz < 5)
{

if(atz == 5)
{
aty = aty + 1;
}
if(aty == 5)
{
atx = atx + 1;
atx = 0;
}

for(atz=0 ; atz<5 ; ++atz)
{
if((atx!= 0 && aty!=0 && atz!=0) || (atx!=4 && aty!=4 && atz!= 4) )
{
Maze[atx][aty][atz].cube_value = (rand() % 2);
}
}
}
}

我正在尝试填充所有 z 轴并在 x 上向右追踪,然后向上移动一个 y 并执行相同的操作,这是一个好方法,还是有更好的方法?我只是变得很困惑。

最佳答案

void PathFinder :: fillmaze()
{
int atx = 0 , aty = 0 , atz = 0;
while(atz<=4)
{
if(atx == 5)
{
aty = aty + 1;
}
if(aty == 5)
{
aty = 0;
atz = atz + 1;
}
if(atz < 5)
{

for(atx=0 ; atx<5 ; ++atx)
{
Maze[atx][aty][atz].cube_value = (rand() % 2);
}

}
}
Maze[0][0][0].cube_value = 1;
Maze[4][4][4].cube_value = 1;

}

这成功了!现在开始迷宫穿越! :/

关于c++ - 生成随机 "maze"-- 3d 数组 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19772940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com