vector<pair<Point, Cell::Direction> > RecursiveBacktrackerMaze::GetUnMarkedNeighbours(Point& cur) { vector<pair<Point, Cell::Direction> > neighbours; for (auto dir : directions_) if (InBounds(cur.Direction(dir)) && !marked_.IsMarked(cur.Direction(dir))) neighbours.push_back(make_pair(cur.Direction(dir), dir)); return neighbours; }
vector<pair<Point, Cell::Direction> > RecursiveBacktrackerMaze::GetUnMarkedNeighbours(Point& cur) { vector<pair<Point, Cell::Direction> > neighbours; for (int i = 0; i < Cell::Direction::Size; ++i) { auto dir = Cell::Direction(i); if (InBounds(cur.Direction(dir)) && !marked_.IsMarked(cur.Direction(dir))) neighbours.push_back(make_pair(cur.Direction(dir), dir)); } return neighbours; }
void WilsonsMaze::GenerateNext() { Point random_pt; Point walker; Cell::Direction random_dir; random_pt = GetRandomValidPoint(outside_cells_); walker = random_pt; while (!marked_.IsMarked(walker)) { random_dir = GetValidRandomDirection(walker); paths_[walker.x()][walker.y()] = random_dir; walker = walker.Direction(random_dir); } while (!marked_.IsMarked(random_pt)) { OpenPassage(random_pt, paths_[random_pt.x()][random_pt.y()]); marked_.Mark(random_pt); random_pt = random_pt.Direction(paths_[random_pt.x()][random_pt.y()]); } }
Cell::Direction Maze::GetValidRandomDirection(Point& current) { vector<Cell::Direction> valid_dirs; Point random_dir; Point tmp_pt; int randN; for (auto dir : directions_) { tmp_pt = current.Direction(dir); if (InBounds(tmp_pt)) valid_dirs.push_back(dir); } randN = rand() % (valid_dirs.size()); return valid_dirs[randN]; }
vector<Point> Solver::DFSolveRawMaze(RawMaze const& maze) { vector<Point> path; Point current = maze.start; vector<vector<int>> raw_maze = maze.raw_maze; vector<vector<bool>> marked(raw_maze.size(), vector<bool>(raw_maze[0].size(), false)); path.push_back(current); marked[current.x()][current.y()] = true; while (!path.empty()) { current = path.back(); if (maze.finish == current) return path; bool found = false; for (int i = 0; i < Cell::Direction::Size; ++i) { Point p = current.Direction(Cell::Direction(i)); if (!raw_maze[p.x()][p.y()] && !marked[p.x()][p.y()]) { path.push_back(p); marked[p.x()][p.y()] = true; found = true; break; } } if (!found) path.pop_back(); } return path; }
vector<Point> Solver::DFSolve(Maze::Ptr const& maze) { Point p; vector<Point> path; Point current = maze->GetStart(); Marked marked(maze->Columns(), maze->Rows()); path.push_back(current); marked.Mark(current); while (!path.empty()) { current = path.back(); if (maze->GetFinish() == current) return path; bool found = false; for (int i = 0; i < Cell::Direction::Size; ++i) { p = current.Direction(Cell::Direction(i)); if (maze->DirOpen(current, Cell::Direction(i)) && !marked.IsMarked(p)) { path.push_back(p); marked.Mark(p); found = true; break; } } if (!found) path.pop_back(); } return path; }
void Maze::SetWall(Point p, Cell::Direction dir) { Get(p).RemoveDirection(dir); Get(p.Direction(dir)).RemoveDirection(OppositeDirection(dir)); }
void Maze::OpenPassage(Point p, Cell::Direction dir) { Get(p).AddDirection(dir); Get(p.Direction(dir)).AddDirection(OppositeDirection(dir)); }