0

コードの列挙型に問題があります。forループでロードしてループする迷路があり、char記号が「*」に等しい場合、2D配列列挙型を「Wall」に設定し、「Clear」に設定すると記号は空白になります。これが私のコードです..

列挙型の初期化を含むヘッダー ファイル:

#ifndef TYPE_H
#define TYPE_H

struct coordinate
{
    int row;
    int col;
};
enum SquareType {Wall, Clear, Visited, Path};

#endif

列挙型の 2D 配列を宣言するヘッダー ファイル:

#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 を含めて、2 つの違いを示しました。

私の出力:

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

*     *      ***** *

* ***** ***        *

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

* *             *  *

* *******  *       *

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

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

ご覧のとおり、mazze の最初の出力から明らかにそうではない場合、enum 型は常に char が「壁」であると考えているかのようです。助言がありますか?私はおそらく見た何かを見逃していますか?どんな助けでも大歓迎です。ありがとう!

4

1 に答える 1

3

ReadMaze

Maze[i][k] == Clear;

これは比較です。割り当てが必要だと思います:

Maze[i][k] = Clear;

についても同様ですMaze[i][k] == Wall;

于 2013-03-20T18:32:40.850 に答える