0

我的代码中的枚举类型有问题。我有一个迷宫,我用for循环加载和循环,如果char符号等于'*',我想将我的二维数组枚举设置为“Wall”,而“Clear”是符号是空格。这是我的代码..

包含枚举初始化的头文件:

#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

我的实现文件:注意读取迷宫的功能和显示迷宫的功能,因为这是我的问题区域..

#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;
}

问题是......我不断得到大量的输出。我在“阅读迷宫”功能和“显示迷宫”功能中加入了一个 cout,以显示两者之间的区别。

我的输出:

****************** *

*     *      ***** *

* ***** ***        *

* ***** *****   ** *

* *             *  *

* *******  *       *

************ *******

*********************
*********************
*********************
*********************
*********************
*********************
*********************
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
****************** *
*     *      ***** *
* ***** ***        *
* ***** *****   ** *
* *             *  *
* *******  *       *
************ *******

如您所见,就好像枚举类型总是认为字符是“墙”,而从迷宫的第一个输出来看,它显然不是。有什么建议么?我错过了一些我可能看过的东西吗?任何帮助表示赞赏。谢谢!

4

1 回答 1

3

ReadMaze

Maze[i][k] == Clear;

这是一个比较。我相信你想要分配:

Maze[i][k] = Clear;

对于Maze[i][k] == Wall;.

于 2013-03-20T18:32:40.850 回答