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

时间:2014-04-22 11:36:24

标签: c++

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

我使用Aldous-Broder方法生成了一个迷宫。生成迷宫似乎完美无缺,但解决它似乎给了我一些问题。原因是因为看起来将墙设置为假实际上并不合适。

当我回去检测墙壁是否存在时,我可以确定特定路径是否可以朝那个方向移动,似乎总是输出墙壁就在那里,即使我看到它是isn&# 39;吨。这是相当多的代码,但如果有人能够仔细查看并告诉我哪里出错了,我将不胜感激。

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

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

使用namespace 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;

1 个答案:

答案 0 :(得分:1)

您没有发布整个代码,因此无法完全测试。但是,最有可能的是:

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

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

使用引用代替maze;这将使它修改你当前的迷宫,而不是副本:

// Generate the maze.
pointT generateMaze(pointT current_point, Maze &maze) { ...
相关问题