我正在尝试(现在无论如何),从数据文件中显示迷宫。第一组数字是大小,第二组是入口,第三组是出口坐标。
我有一个名为“MyMaze1.dat”的简单输入文件:
7 20
0 18
6 12
****************** *
* * ***** *
* ***** *** *
* ***** ***** ** *
* * * *
* ******* * *
************ *******
我用来读这个迷宫的代码:
void MazeClass::ReadMaze(ifstream& mazedata){
mazedata >> row >> column;
GetExit(mazedata);
GetEntrance(mazedata);
maze= new char*[row];
for (unsigned i=0; i<row;i++)
{
maze[i]=new char[column];
}
/*
maze=new char[column];
*maze[i]=new char[column];
*/
for (int y=0;y<column;y++)
{//Keeping the maze inside boundries (step 1)
for (int x=0;x<row;x++)//(Step 2)
{
maze[x][y]=mazedata.get();
}
}
}
我用来显示迷宫的代码是:
void MazeClass::Display(){
cout << "Entrance is: " << entx << ' ' << enty << endl;
cout << "Exit is: " << exitx << ' ' << exity << endl;
cout << "Maze is: " << endl;
for (int y=0;y<column;y++)
{
for (int x=0;x<row;x++)
{
cout << maze[x][y];
}
cout << endl;
}
}
其输出如下:
Entrance is: 6 12
Exit is: 0 18
Maze is:
******
*******
***** *
*
*
***** *
* ****
* ***
*
* ****
* *****
** *
* *
* *
* ****
*** *
*
******
******
提前感谢您提供所有帮助。
答案 0 :(得分:2)
\n
)。
for (int y=0; y < rows; y++) // rows outer
{
for (int x=0;x < columns ;x++)// columns inner
{
maze[y][x]=mazedata.get(); // Swapped y and x
}
mazedata.get(); // skip \n
}
Display
答案 1 :(得分:0)
在你的打印输出中,第一个循环应该是行,然后是列
即,执行一行及其所有列,然后执行下一行....等等
答案 2 :(得分:0)
您的迷宫阅读不太正确。
// outer loop reads rows.
for (int x=0;x<row;x++)
{
// Go across the columns first.
for (int y=0;y<column;y++)
{
maze[x][y]=mazedata.get();
}
// After you have read all the data you need to skip the newline (and any blanks)
mazedata.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
显示路由需要相同的调整。除非你想横向打印迷宫。
void MazeClass::Display()
{
cout << "Entrance is: " << entx << ' ' << enty << endl;
cout << "Exit is: " << exitx << ' ' << exity << endl;
cout << "Maze is: " << endl;
for (int x=0;x<row;x++)
{
for (int y=0;y<column;y++)
{
cout << maze[x][y];
}
cout << endl;
}
cout << endl;
}