QtCreator C ++:2D矩阵在同一个类的不同成员函数中给出不同的结果

时间:2018-02-18 10:10:02

标签: c++ qt matrix qt-creator

最近我一直在研究一个二维迷宫创建者,它可以从2D矩阵创建一个迷宫。我使用了一个布尔矩阵,其中1表示一个迷宫室,一切正常,但由于某些奇怪的原因,在setRoomCode函数中,矩阵的每个元素都被检测为零。但是在迷宫在矩阵中创建之后调用setRoomCode函数,所以一些元素应该是1.不知道我在哪里出错了,一​​些帮助将会非常感激...谢谢。

这就是程序的样子

// Matrix.h

Parse

// Matrix.cpp

#ifndef MATRIX_H
#define MATRIX_H

#include<QObject>
#include<iostream>
#include<time.h>
#include<stdio.h>
#include<QGridLayout>
#include"room.h"

class matrix:public QObject
{
    Q_OBJECT

 private:
    enum direction{LEFT, RIGHT, UP, DOWN};
    enum roomCode
    {
        N_A,ONE_L,ONE_R,ONE_U,ONE_D,
        TWO_LU,TWO_LD,TWO_RU,TWO_RD,TWO_LR,TWO_UD,
        THREE_LRU,THREE_LRD,THREE_LUD,THREE_RUD,
        FOUR
    };
    enum specialRooms{START, DIAGONAL, NORMAL_ROOMS};
    enum roomCodeArrayValues{TRUE_A, FALSE_A, NONE_A};

    bool **Matrix;               //The 2X2 matrix
    int curr_room_i, curr_room_j;          // variables for holding the current position in the matrix while iterating through it
    int first_room_i, first_room_j; // variable to store the first position
    int theSize;                 // Size of the matrix and no. of rooms

    QGridLayout *matrixGridLayout ;
    void createGUIMaze();

    int roomCodeArray[4];
    void categorizeRooms(int x, int y);
    void setRoomCode(bool leftPermission, bool rightPermission, bool upPermission, bool downPermission, bool x, bool y); //checking existing neighbors;
    int getRoomCode(int x, int y); // function to retrieve the room code

 public:
    matrix(const int theSize = 5);
    bool inMatrix(int valX, int valY) const;            // Out of bound check for an element
    bool allNeighborInMatrix(int valX, int valY) const; // Out of Bound check for an element's surrounding neighbor
    bool isAllNeighborOne(int valX, int valY) const;    // To check if a matrix element's LEFT,RIGHT,UP & DOWN elements are 1
    void setLocations();
    void printMaze() const;

    QGridLayout *getLayout();
    int getMartixFirstX();
    int getMatrixFirstY();

 public slots:
    void createMaze(int noOfRooms = 20);
};

#endif // MATRIX_H

1 个答案:

答案 0 :(得分:1)

在你的函数setRoomCode中,参数x和y是布尔值,但我怀疑你的意思是它们是int,因为你使用它们与矩阵的+/- 1(默认情况下是5x5矩阵)。

这可能是您的问题,将它们更改为int?