迷宫功能无法检测已删除的墙
Maze function not working with respect to detecting deleted walls
在这里搜索过,还没有遇到这个问题。。。看起来某个特定方法无法正常工作。
我使用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) { ...
相关文章:
- 检测win32服务创建和删除的最佳方法
- 删除字符串后C++检测到堆损坏
- 如何检测文件夹是否太重,如果是,请删除其中的一部分
- 防止或检测"this"在使用过程中被删除
- CRT 检测到应用程序在堆缓冲区(新建/删除)类结束后写入内存
- C++ 删除数组时检测到堆损坏
- JNI检测到应用程序中的错误:使用已删除的弱全局参考
- 在无锁FIFO缓冲区中删除了节点检测
- Python 对象删除检测
- 是否可以检测绑定成员函数引用的对象是否被删除或销毁
- 在已删除的副本分配中检测到虚假错误
- 在 Visual C++ 中删除调用后检测到堆损坏
- 在删除行的正常块之后检测到堆损坏
- 检测和删除一个网格的隐藏表面
- 是否有任何工具/方法来检测/删除所有未使用的变量,宏,头(包括)和函数从c++代码
- 在C++非GUI应用程序中检测USB插入/删除
- 如何仅检测卷上已删除、已更改和已创建的文件?
- 如何传播或检测对象的删除
- 如何使用动态强制转换检测指针的删除
- 如何检测指针是否已删除并安全删除