读一个文本文件并创建一个迷宫 [英] Reading a text file and creating a maze
问题描述
#include< iostream>
#include< string>
#include< fstream>
使用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;
结构位置
{
int r;
int c;
};
class迷宫
{
public:
迷宫();
/ *一个参数构造函数,需要一个文件名并读取
*迷宫文件的内容。
* /
迷宫(字符串文件名);
void displayMaze();
bool isWall(Position p);
bool isPath(位置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);
位置getEntrance();
位置getExit();
private:
int row;
int col;
仓位退出;
位置入口;
int迷宫[MAX_ROW] [MAX_COL];
};
/ ** @ file Maze.cpp * /
#includeMaze.h
迷宫::迷宫()
{}
迷宫::迷宫(字符串文件名)
{
ifstream inStream;
inStream.open(filename.c_str());
if(inStream.fail())
{
cout<< 输入文件打开失败。
}
//获取迷宫的尺寸。
inStream>> col>>行;
//退出迷宫。
inStream>> exit.r>> exit.c中;
//进入迷宫的入口。
inStream>>入口>> entrance.c;
//从文件中读取迷宫。 (int c = 0; c< col; c ++)
{
(
for(int r = 0; r < inStream>>迷宫[R] [C];
if(maze [r] [c])=='X')
maze [r] [c] = WALL;
else
迷宫[r] [c] = CLEAR;
}
}
} //结束一个参数构造函数
void Maze :: displayMaze()
{
cout<< '\t'<< '\t'<< 行<< '\t'<< 列<< ENDL;
cout<< 尺寸:<< '\t'<<行<< '\t'<< col< ENDL;
cout<< 退出:<< '\t'<< '\t'<< exit.r<< '\t'<< exit.c<< ENDL;
cout<< 入口:<< '\t'<<入口.r<< '\t'<<入口<< ENDL; (int c = 0; c< col; c ++)
{b;
for(int r = 0; r
cout<<迷宫[R] [C];
}
cout<< ENDL;
} // end displayMaze()
bool Maze :: isWall(Position p)
{
return maze [pr] [pc] ==墙;
} // end isWall()
ool Maze :: isPath(Position p)
{
return maze [p.r] [p.c] == PATH;
} // end isPath()
ool 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()
$ b $ void Maze :: setVisited(Position p)
{
maze [p.r] [p.c] = VISITED;
} // end setVisited()
位置迷宫:: getEntrance()
{
return entrance;
} // end getEntrance()
位置Maze :: getExit()
{
return exit;
} // end getExit()
下面是一个文件内容的例子看起来像。第一组数字是列和行的迷宫的尺寸。第二组数字是出口的行列位置,第三组数字是入口的行和列位置。
20 7
0 18
6 12
xxxxxxxxxxxxxxxxxx x
xx xxxx x
x xxxxx xxxxx xx x
x xxxxx xxxxxxx xx x
xx xx xx x
x xxxxxxxxxx xx x
xxxxxxxxxxxx xxxxxxx
你也可能发现使用比迷宫大的数组是有帮助的其中包括在阵列迷宫周围的边界单元格),以便迷宫单元格的相邻单元格可以引用,而不必担心边缘条件。
I am working on a program that will read a file containing information about a maze and use that information to find a path through the maze. The problem I am having is representing the maze in the array. I am not sure how to turn the X's into integers to represent them as walls in the array. In the one argument constructor I use a for loop to go through the file and put each character into the appropriate array index. The way I have done this clearly does not work and if anybody could give some advice on how properly represent the maze in the array I would appreciate it.
/**@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()
Here is an example of what the file contents will look like. The first set of numbers is the dimensions of the maze in columns and rows. The second set of numbers is row and column position of the exit and the third set of numbers is the row and column position of the entrance.
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
To represent the maze connectivity, you can associate a position in the maze with a list of the neighboring cells to which there exists a path. The list could be an actual list, or a more compact representation such as a bitmap: e.g. 1010 could mean that we can go north and south, but not east and west.
You may also find it helpful to use an array which is larger than the maze (i.e. which includes the border cells around the maze in the array) so that the neighboring cells of a maze cell can be referenced without worrying about the edge conditions.
这篇关于读一个文本文件并创建一个迷宫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!