迷宫功能无法检测已删除的墙

Maze function not working with respect to detecting deleted walls

本文关键字:删除 检测 功能 迷宫      更新时间:2023-10-16

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

我使用Aldous-Broder方法生成了一个迷宫。生成迷宫似乎很完美,但解决它似乎给我带来了一些问题。原因是,把墙设为假墙似乎并不奏效。

当我回去检测墙是否在那里,这样我就可以确定一条特定的路径是否可以朝那个方向前进时,它似乎总是输出墙在那里,尽管我可以看到它不在。这是相当多的代码,但如果有人能仔细查看并告诉我哪里出了问题,我将不胜感激。

我是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;
}

这是generateMase函数:

// 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);

编辑:如果你也想看看功能原型:

使用命名空间std;

/* 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) { ...

您正在转发迷宫的副本。读它是无害的,但在这个函数中写它不会有任何结果,因为当你离开这个函数时,副本会被丢弃。

请改用对maze引用;这将使它修改你当前的迷宫,而不是一个副本:

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