我正在嘗試制作一個迷宮。墻壁由“#”組成,我想使用“.”創建一條從左上角到右下角的路徑。我已經走了這么遠,但是當我現在運行它時,我遇到了分段錯誤并且沒有任何反應。有誰知道我做錯了什么?我認為我的問題在于我的遞回函式。做我的客人來編輯它!提前致謝!:D
#include <vector>
#include <stack>
#include <sstream>
#include <time.h>
#define North 0
#define East 1
#define South 2
#define West 3
class Maze {
private:
int mazeHeight;
int mazeWidth;
int seedValue;
std::vector <std::vector <char>> Maze;
public:
void checkuserInput(int Input1, int Input2);
void mazeConstructor(int x, int y, int z, std::vector <std::vector <char>> vect);
std::vector <std::vector <char>> initializeMaze();
};
class Path {
private:
std::vector <std::vector <char>> Grid;
bool visited;
int Height;
int Width;
public:
void pathConstructor(std::vector <std::vector <char>> Maze, int mazeHeight, int mazeWidth);
bool checkValid(int yPos, int xPos);
void findPath(int yPos, int xPos);
void printMaze();
};
// Check if user input is valid
void checkUserInput(int Input1, int Input2) {
int Height;
int Width;
if (!(Input1 >> Height)) {
throw std::runtime_error ("Invalid input");
}
if (!(Input2 >> Height)) {
throw std::runtime_error ("Invalid input");
}
}
//Make the variables accesible
void Maze::mazeConstructor(int x, int y, int z, std::vector <std::vector <char>> vect) {
mazeHeight = x;
mazeWidth = y;
seedValue = z;
Maze = vect;
}
// Initialize the outer walls with '#'
std::vector <std::vector <char>> Maze::initializeMaze() {
for (int i = 0; i < mazeWidth; i ) {
for (int j = 0; j < mazeHeight; j ) {
Maze[i][j] = '#';
}
}
return Maze;
}
// Make the variables accessible
void Path::pathConstructor(std::vector <std::vector <char>> Maze, int mazeHeight, int mazeWidth) {
Grid = Maze;
Height = mazeHeight;
Width = mazeWidth;
}
bool Path::checkValid(int yPos, int xPos) {
if(xPos >= Width || xPos < 0) {
return false;
}
if(yPos >= Height || yPos < 0) {
return false;
}
if (Grid[xPos][yPos] == '#') {
return false;
}
return true;
}
// Find a path using recursion
void Path::findPath(int yPos, int xPos) {
if (yPos == Height || xPos == Width) {
printMaze();
}
else {
Grid[yPos][xPos] = '.';
int randomNumber = rand() % 3;
switch (randomNumber) {
case South:
if (checkValid(yPos, xPos)) {
findPath(yPos 1, xPos);
}
else {
findPath(yPos, xPos);
}
case East:
if (checkValid(yPos, xPos)) {
findPath(yPos, xPos 1);
}
else {
findPath(yPos, xPos);
}
case North:
if (checkValid(yPos, xPos)) {
findPath(yPos - 1, xPos);
}
else {
findPath(yPos, xPos);
}
case West:
if (checkValid(yPos, xPos)) {
findPath(yPos, xPos - 1);
}
else {
findPath(yPos, xPos);
}
}
}
}
// Output the maze
void Path::printMaze() {
for (int i = 0; i < Grid.size(); i ) {
for (int j = 0; j < Grid[0].size(); j ) {
std::cout << Grid[i][j];
}
std::cout << std::endl;
}
}
// Get command line arguements
int main(int argc, char* argv[]) {
Maze maze;
Path Path;
srand (time(0));
int Height;
int Width;
int seedValue;
Height = atoi(argv[2]);
Width = atoi(argv[2]);
try {
checkUserInput(Height, Width);
}
catch(std::runtime_error& e) {
std::cerr << e.what() << std::endl;
return 1;
}
if (argc > 3) {
seedValue = atoi(argv[3]);
} else {
seedValue = rand();
}
std::vector <std::vector <char>> Maze (Height * 3 1, std::vector <char> (Width * 5 1, ' '));
maze.mazeConstructor(Height, Width, seedValue, Maze);
Path.pathConstructor(maze.initializeMaze(), Height, Width);
Path.printMaze();
Path.findPath(1, 1);
}```
uj5u.com熱心網友回復:
正方形將無效,如果
- 它的 x,y 位置在迷宮之外。
- 它的 x,y 位置是一塊磚
bool Path::checkValid(int yPos, int xPos) {
if(xPos >= mazeWidth || xPos < 0){
return false;
}
if(yPos >= mazeHeight || yPos < 0){
return false;
}
if(Maze[xPos][yPos] == '#'){
return false;
}
return true;
}
uj5u.com熱心網友回復:
如果我正確理解了這個問題,您只需要檢查該位置是否在迷宮的范圍內,并且它是一個有效的“單元格”。
要檢查單元格的有效性,只需將您存盤的字符與您使用的空字符進行比較。
要檢查位置是否在迷宮內,您需要做的就是確保 y 值和 x 值分別在 [0, mazeHeight) 和 [0, mazeWidth) 范圍內。一個簡單的偽代碼函式可能是這樣的:
function checkValid(yPos, xPos)
// Check that yPos is valid
if yPos < 0 or yPos >= mazeHeight
return false
// Check that xPos is valid
if xPos < 0 or xPos >= mazeWidth
return false
// At this point, you know the location is inside the maze
// so just check it's an empty square
return Maze[yPos][xPos] == ' ' // Assuming ' ' is empty
希望這會有所幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/402511.html
標籤:
