【发布时间】:2011-03-13 20:59:19
【问题描述】:
我正在尝试(无论如何现在),从数据文件中显示迷宫。第一组数字是大小,第二组数字是入口,第三组数字是出口坐标。
我有一个名为“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:
******
*******
***** *
*
*
***** *
* ****
* ***
*
* ****
* *****
** *
* *
* *
* ****
*** *
*
******
******
提前感谢您的所有帮助。
【问题讨论】:
标签: c++ class io fstream dynamic-memory-allocation