2012-03-19 30 views
1

我正在研究一个程序,该程序将读取包含有关迷宫信息的文件,并使用该信息在迷宫中找到路径。我遇到的问题是代表阵列中的迷宫。我不知道如何将X转换为整数来将它们表示为阵列中的墙。在一个参数构造函数中,我使用for循环遍历文件并将每个字符放入相应的数组索引中。我这样做的方式显然不起作用,如果有人能就如何正确表示阵列中的迷宫给出一些建议,我将不胜感激。阅读文本文件并创建迷宫

/**@file Maze.h*/ 

    #include <iostream> 
    #include <string> 
    #include <fstream> 
    using namespace std; 

    const int MAX_ROW = 60; 
    const int MAX_COL = 40; 
    const int WALL = 0; 
    const int CLEAR = 1; 
    const int PATH = 2; 
    const int VISITED = 3; 

    struct Position 
    { 
     int r; 
     int c; 
    }; 
    class Maze 
    { 
    public: 
    Maze(); 

    /*One argument constructor that takes a filename and reads 
    *the contents of a maze file. 
    */ 
    Maze(string filename); 
    void displayMaze(); 
    bool isWall(Position p); 
    bool isPath(Position p); 
    bool isClear(Position p); 
    bool isVisited(Position p); 
    void setWall(Position p); 
    void setPath(Position p); 
    void setClear(Position p); 
    void setVisited(Position p); 
    Position getEntrance(); 
    Position getExit(); 
    private: 
     int row; 
     int col; 
     Position exit; 
     Position entrance; 
     int maze[MAX_ROW][MAX_COL]; 
    }; 

/**@file Maze.cpp*/ 

#include "Maze.h" 

Maze::Maze() 
{} 

Maze::Maze(string filename) 
{ 
    ifstream inStream; 
    inStream.open(filename.c_str()); 
    if(inStream.fail()) 
{ 
    cout << "Input file opening failed.\n"; 
} 
//Get the dimensions of the maze. 
inStream >> col >> row; 
//Get the exit to the maze. 
inStream >> exit.r >> exit.c; 
//Get the entrance to the maze. 
inStream >> entrance.r >> entrance.c; 
//Read maze from the file. 
for(int r = 0; r < row; r++) 
{ 
    for(int c = 0; c < col; c++) 
    { 
     inStream >> maze[r][c]; 
     if(maze[r][c])== 'X') 
      maze[r][c] = WALL; 
     else 
      maze[r][c] = CLEAR; 
    } 
} 
}//end one argument constructor 

void Maze::displayMaze() 
{ 
cout << '\t' << '\t' << "Row" << '\t' << "Column" << endl; 
cout << "Dimensions:" << '\t' << row << '\t' << col << endl; 
cout << "Exit:" << '\t' << '\t' << exit.r << '\t' << exit.c << endl; 
cout << "Entrance: " << '\t' << entrance.r << '\t' << entrance.c << endl; 

for(int r = 0; r < row; r++) 
{ 
    for(int c = 0; c < col; c++) 
    { 
     cout << maze[r][c]; 
    } 
    cout << endl; 
} 
}//end displayMaze() 

bool Maze::isWall(Position p) 
{ 
return maze[p.r][p.c] == WALL; 
}//end isWall() 

bool Maze::isPath(Position p) 
{ 
return maze[p.r][p.c] == PATH; 
}//end isPath() 

bool Maze::isClear(Position p) 
{ 
return maze[p.r][p.c] == CLEAR; 
}//end isClear() 

bool Maze::isVisited(Position p) 
{ 
return maze[p.r][p.c] == VISITED; 
}//end isVisited() 

void Maze::setWall(Position p) 
{ 
maze[p.r][p.c] = WALL; 
}//end setWall() 

void Maze::setPath(Position p) 
{ 
maze[p.r][p.c] = PATH; 
}//end setPath() 

void Maze::setClear(Position p) 
{ 
maze[p.r][p.c] = CLEAR; 
}//end setClear() 

void Maze::setVisited(Position p) 
{ 
maze[p.r][p.c] = VISITED; 
}//end setVisited() 

Position Maze::getEntrance() 
{ 
return entrance; 
}//end getEntrance() 

Position Maze::getExit() 
{ 
return exit; 
}//end getExit() 

以下是文件内容的示例。第一组数字是列和行中迷宫的尺寸。第二组数字是出口的行和列位置,第三组数字是入口的行和列位置。

20 7 
0 18 
6 12 
xxxxxxxxxxxxxxxxxx x 
x  x  xxxx x 
x xxxxx xxxxx xx x 
x xxxxx xxxxxxx xx x 
x x   xx xx x 
x xxxxxxxxxx xx x 
xxxxxxxxxxxx xxxxxxx 
+1

它不起作用...以什么方式? – jrok 2012-03-19 17:35:05

+0

在for循环中,我试图将每一块迷宫都读入数组中。但是,if语句正在被跳过,并且只有else语句正在执行,因此所有空格都被标记为CLEAR。我想知道如何读取文件,以便在数组中将X表示为0(零)。 – Eric 2012-03-19 18:31:43

+0

尝试更改您的数组以保存'char's。 – jrok 2012-03-19 18:40:00

回答

2

要表示迷宫连接,您可以将迷宫中的某个位置与存在路径的相邻单元的列表关联起来。该列表可以是实际列表,也可以是比如位图等更紧凑的表示:例如, 1010可能意味着我们可以南下,但不是东西。

你也可能发现使用一个比迷宫大的数组(比如在数组中包含迷宫中的边界单元)有助于迷宫单元的相邻单元可以被引用而不用担心边缘条件。