2013-03-20 68 views
0

我在我的代碼中遇到了枚舉類型的問題。我有一個迷宮,我加載循環與循環,如果char符號等於'*'我想將我的二維數組枚舉設置爲「牆」和「清除」是符號是空格。這裏是我的代碼..C++枚舉類型沒有正確初始化

頭文件,其中包含枚舉初始化:

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

問題是...我不斷收到shotty輸出。我在「閱讀迷宮」功能和我的「顯示迷宮」功能中加入了一個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 
****************** * 
*  *  ***** * 
* ***** ***  * 
* ***** ***** ** * 
* *    * * 
* ******* *  * 
************ ******* 

因此,大家可以看到,它彷彿枚舉類型一直認爲,一個字符是「牆」從mazze的第一個輸出時,顯然不是。有什麼建議麼?我錯過了一些我可能看過的東西嗎?任何幫助表示讚賞。謝謝!

+0

請注意,對於所有'MazeClass :: IsXXX'函數,您可以用'return'替換'if'並刪除所有其他行! – 2013-03-20 18:34:58

+0

作爲供參考: '如果(迷宮[X] [Y] ==清除)' 如果你看你的分配循環: '的for(int i = 0; I <高度;我++) { (int k = 0; k KidneyChris 2013-03-20 18:39:22

+0

您也可以將它轉置爲轉置,但請注意,您的「IsXXX」函數對其他所有工作都可以工作90度。 – KidneyChris 2013-03-20 18:41:37

回答

3

ReadMaze

Maze[i][k] == Clear; 

這是一個比較。我相信你想分配:

Maze[i][k] = Clear; 

這同樣適用於Maze[i][k] == Wall;如此。

+0

就是這樣!非常感謝。我不敢相信我看着這樣一個簡單的錯誤。我一直困惑這30分鐘! – Volman2014 2013-03-20 18:37:08