我的代码中的枚举类型出现问题。我有一个迷宫,我加载并循环使用for循环,如果char符号等于' *'我想将我的2D数组枚举设置为" Wall"和#34;清除"是符号是空白。这是我的代码..
包含枚举初始化的头文件:
#ifndef TYPE_H
#define TYPE_H
struct coordinate
{
int row;
int col;
};
enum SquareType {Wall, Clear, Visited, Path};
#endif
头文件声明枚举的二维数组:
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
#include "type.h"
const int MAX = 50;
#ifndef MazeClass_H
#define MazeClass_H
class MazeClass
{
private:
SquareType Maze[MAX][MAX];
coordinate Entrance, Exit;
int height, width;
public:
MazeClass();
void ReadMaze(ifstream&);
void DisplayMaze();
coordinate GetEntrance();
coordinate GetExit();
void MarkVisited(coordinate);
void MarkPath(coordinate);
bool IsWall(int x, int y);
bool IsClear(int x, int y);
bool IsPath(int x, int y);
bool IsVisited(coordinate);
bool IsExit(int x, int y);
bool IsInMaze(int x, int y);
};
#endif
我的实现文件:注意Read Maze的功能和显示迷宫的功能,因为这是我的问题区域..
#include "MazeClass.h"
#include <iostream>
#include <cstdlib>
MazeClass::MazeClass()
{
}
void MazeClass::ReadMaze(ifstream& myIn)
{
int x, y;
char ch;
myIn >> x;
myIn >> y;
height = x;
width = y;
myIn >> x;
myIn >> y;
Entrance.row = x;
Entrance.col = y;
myIn >> x;
myIn >> y;
Exit.row = x;
Exit.col = y;
myIn.ignore(100, '\n');
for(int i = 0; i < height; i++)
{
for(int k = 0; k < width + 1; k++)
{
myIn.get(ch);
if(ch == ' ')
Maze[i][k] == Clear;
else
Maze[i][k] == Wall;
cout << ch;
}
cout << endl;
}
}
void MazeClass::DisplayMaze()
{
char ch;
for(int i = 0; i < height; i++)
{
for(int k = 0; k < width + 1; k++)
{
if(Maze[i][k] == Wall)
{
ch = '*';
cout << ch;
}
else if(Maze[i][k] == Clear)
{
ch = ' ';
cout << ch;
}
}
cout << endl;
}
}
coordinate MazeClass::GetEntrance()
{
return Entrance;
}
coordinate MazeClass::GetExit()
{
return Exit;
}
bool MazeClass::IsWall(int x, int y)
{
if(Maze[x][y] == Wall)
return true;
else
return false;
}
bool MazeClass::IsClear(int x, int y)
{
if(Maze[x][y] == Clear)
return true;
else
return false;
}
bool MazeClass::IsExit(int x, int y)
{
if(x == Exit.row && y == Exit.col)
return true;
else
return false;
}
bool MazeClass::IsInMaze(int x, int y)
{
cout << "Height: " << height << " " << "Width: " << width << endl;
if(x < 0 || x > height || y < 0 || y > width)
return false;
else
return true;
}
最后我的文件使用这些函数并测试输出:
#include "MazeClass.h"
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc,char *argv[])
{
MazeClass maze;
ifstream myIn;
int x,y;
string filename = argv[1]; // command line arguement stuff
myIn.open(filename.c_str());
maze.ReadMaze(myIn); //reads in the maze from a data file
maze.DisplayMaze();
cout << "The entrance is at: " << maze.GetEntrance().row << " " << maze.GetEntrance().col << endl;
cout << "The exit is at: " <<maze.GetExit().row << " " << maze.GetExit().col << endl;
if(maze.IsWall(1,1) == true) //uses the IsWall method to determine wether it is a wall or not
cout << "location (1,1) is a wall\n";
else
cout << "location (1,1) is not a wall\n";
if(maze.IsClear(1,3) == true) //uses the IsClear method to determine wether it is a clear or not
cout << "location (1,3) is clear\n";
else
cout << "location (1,3) is not clear\n";
if(maze.IsInMaze(1,5) == true) //uses the IsInMaze method to determine wether it is a clear or not
cout << "location (1,5) is in the Maze\n";
else
cout << "location (1,5) is not in the Maze\n";
if(maze.IsInMaze(25,35) == true)
cout << "location (25,35) is in the maze\n";
else
cout << "location (25,35) is not in the maze\n";
myIn.close();
return 0;
}
问题是......我不断得到蠢货。我已经在我的&#34;阅读Maze&#34;功能和我的显示迷宫&#34;功能显示两者之间的差异。
我的输出:
****************** *
* * ***** *
* ***** *** *
* ***** ***** ** *
* * * *
* ******* * *
************ *******
*********************
*********************
*********************
*********************
*********************
*********************
*********************
The entrance is at: 0 18
The exit is at: 6 12
location (1,1) is a wall
location (1,3) is not clear
Height: 7 Width: 20
location (1,5) is in the Maze
Height: 7 Width: 20
location (25,35) is not in the maze
这是我正在阅读的数据文件:
7 20
0 18
6 12
****************** *
* * ***** *
* ***** *** *
* ***** ***** ** *
* * * *
* ******* * *
************ *******
正如你所看到的,就好像enum类型总是认为char是&#34; Wall&#34;当从mazze的第一个输出时它显然不是。有什么建议?我错过了一些我可能会看过的东西吗?任何帮助表示赞赏。谢谢!
答案 0 :(得分:3)
在ReadMaze
:
Maze[i][k] == Clear;
这是一个比较。我相信你想要任务:
Maze[i][k] = Clear;
Maze[i][k] == Wall;
也是如此。