gpt4 book ai didi

c++ - 迷宫功能在检测已删除的墙壁方面不起作用

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

在这里搜索过,还没有遇到这个问题……看起来是某个特定的方法不能正常工作。

我使用 Aldous-Broder 方法生成了一个迷宫。生成迷宫似乎工作得很好,但解决它似乎给了我一些问题。原因是因为将 walls 设置为 false 似乎并没有真正起作用。

当我回去检测墙壁是否在那里,以便我可以确定特定路径是否可以朝那个方向前进时,它似乎总是输出墙壁在那里,即使我可以看到它不是'吨。这是相当多的代码,但如果有人可以查看它并告诉我哪里出错了,我将不胜感激。

我是 C++ 的新手,所以这对我来说也是早期——如果您想查看更多代码,我非常乐意提供。

谢谢。

这创建了迷宫:

int main() {

// Initialize.
Randomize();
InitGraphics();
Maze maze(MAZE_ROWS, MAZE_COLS, true); // These are the parameters given in the interface file.
maze.draw();
cout << "Welcome to the Maze!!" << endl;

// Setup.
pointT current_point = setupCurrentPoint(maze);

while (true) {

// Break out of this when all the cells have been included in the set.
if (included_point.size() == ALL_CELLS) {
break;
}

// Generate the maze.
pointT selected_neighbor = generateMaze(current_point, maze);

// Set the neighbor equal to the current point.
current_point = selected_neighbor;

}

这是 generateMaze 函数:

// Generate the maze.
pointT generateMaze(pointT current_point, Maze maze) {

// Generate a random neighbor.
int picked_neighbor = RandomInteger(0, 3);

// Declare the point.
pointT selected_neighbor = pickANeighbor(current_point, picked_neighbor);

// If it's not within the boundaries of the maze, pick a new neighbor.
while (maze.pointInBounds(selected_neighbor) == false) {

// Generate a random neighbor.
int picked_neighbor = RandomInteger(0, 3);

selected_neighbor = pickANeighbor(current_point, picked_neighbor);

if (maze.pointInBounds(selected_neighbor) == true) {
break;
}
}

// If the neighbor is excluded, remove the wall and mark it as included.
if (included_point.contains(selected_neighbor) == false) {

if (maze.isWall(selected_neighbor, current_point) == true) {
maze.setWall(selected_neighbor, current_point, false);

}

// Add the point if it's not already in the set.
included_point.add(selected_neighbor);

}

return selected_neighbor;
}

这就是我试图解决迷宫的方法:

// SOLVE -- THIS PART IS IN PROGRESS. THE ABOVE CODE IS COMPLETELY FINISHED.

// Create a path with just a start location, enqueue it and then dequeue it.
Stack<pointT> start_path = setupPath();
store_paths.enqueue(start_path);
Stack<pointT> shortest_path = store_paths.dequeue();


// If the path ends at the goal, you're finished.
pointT end_point;
end_point.row = MAZE_ROWS - 1;
end_point.col = MAZE_COLS - 1;

pointT popped_point;
popped_point = shortest_path.peek();

while (true) {

cout << "START AGAIN" << endl;

// Otherwise, for each neighbor accessible from the last point, make a copy of the path.
int picked_neighbor = RandomInteger(0, NUM_NEIGHBORS);
pointT add_point = pickANeighbor(popped_point, picked_neighbor);
cout << "the point to add is " << toString(add_point) << endl;

if (maze.pointInBounds(add_point) && !checkForCircle(shortest_path, add_point) && !maze.isWall(popped_point, add_point)){
cout << "the point that is OK is: " << toString(add_point) << endl;
Stack<pointT> path_copy = shortest_path;
path_copy.push(add_point);
store_paths.enqueue(path_copy);

}


if (store_paths.size() != 0) {
// Dequeue the shortest path.
shortest_path = store_paths.dequeue();

// Figure out what the last point in it was.
popped_point = shortest_path.peek();

// Figure out whether the popped point matches the end point.
if (popped_point == end_point) {
cout << "You're done..." << endl;
//break;
}
}
}

// Now trace out the right path.
drawPath(shortest_path, maze);

编辑:如果您还想查看函数原型(prototype):

使用命名空间标准;

/* Function prototypes */
Stack<pointT> setupPath();
pointT setupCurrentPoint(Maze maze);
pointT pickANeighbor(pointT point, int picked_neighbor);
pointT generateMaze(pointT current_point, Maze maze);
string toString(pointT point);
bool checkForCircle(Stack<pointT>, pointT);
void drawPath(Stack<pointT> shortest_path, Maze maze);
bool wallBetweenPoints(pointT popped_point, pointT add_point, Maze maze);

/* Private instance variables */
Set<pointT> included_point;
Vector<pointT> select_a_neighbor;
Queue<Stack<pointT> > store_paths;

/* Constants */
const int MAZE_ROWS = 5;
const int MAZE_COLS = 8;
const int ALL_CELLS = (MAZE_ROWS) * (MAZE_COLS);
const int NUM_NEIGHBORS = 4;

最佳答案

您没有发布完整的代码,所以我无法对其进行全面测试。但是,最有可能的是:在

// Generate the maze.
pointT generateMaze(pointT current_point, Maze maze) { ...

您正在转发迷宫的拷贝。读取它是无害的,但在此函数中写入它不会导致任何结果,因为当您离开该函数时拷贝将被丢弃。

使用引用代替迷宫;这将使它修改您当前的迷宫,而不是拷贝:

// Generate the maze.
pointT generateMaze(pointT current_point, Maze &maze) { ...

关于c++ - 迷宫功能在检测已删除的墙壁方面不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218349/

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