代码之家  ›  专栏  ›  技术社区  ›  D-PUNK-R

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

  •  0
  • D-PUNK-R  · 技术社区  · 7 年前

    最近,我一直在开发一个二维迷宫生成器,它可以从二维矩阵创建一个迷宫。我使用了一个布尔矩阵,其中1代表一个迷宫房间,一切正常,但由于某些奇怪的原因,在setRoomCode函数中,矩阵的每个元素都被检测为零。但是setRoomCode函数是在矩阵中创建迷宫之后调用的,因此一些元素应该是1。不知道我哪里出错了,我会非常感谢你的帮助。。。谢谢

    这就是程序的样子

    //矩阵。h类

    #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
    

    //矩阵。cpp公司

    #include "matrix.h"
    #include <QDebug>
    
    //The Constructor
    
    matrix::matrix(const int theSize)
    {
        srand(time(NULL));                     // seeding with system time
        curr_room_i = theSize-1;//rand()%theSize;               //(rand()%2 == 0) ? 0 : (rand()%theSize);
        curr_room_j = theSize-1;//rand()%theSize;               //(first_i == 0) ? 1 : (rand()%theSize);
    
        first_room_i = curr_room_i;
        first_room_j = curr_room_j;
    
        this->theSize = theSize;
    
    //Matrix Initialization
    
        Matrix = new bool*[theSize];
    
        for (int i = 0;  i < theSize ; i++)
        {
            Matrix[i] = new bool[theSize];
            for(int j = 0; j < theSize ; j++)
            Matrix[i][j] = 0;
        }
       // Matrix[0][0] = Matrix[theSize-1][theSize-1] = Matrix[theSize-1][0] = Matrix[0][theSize-1] = 1;
    }
    
    // Creating the Maze
    
    void matrix::createMaze(int noOfRooms)
    {
        if(noOfRooms > theSize*theSize)
            {std::cout<<"Room Overflow!!!!!"<<std::endl; return;}
    
        Matrix[curr_room_i][curr_room_j] = 1;                                    // First maze room initialization
        srand(time(NULL));
    
        while(noOfRooms > 1)                                           //greater than 1 because first location is already initialized before reaching the loop
        {
            if (allNeighborInMatrix(curr_room_i,curr_room_j) && !isAllNeighborOne(curr_room_i, curr_room_j))
            {
                 switch(rand()%4)
                 {
                     case LEFT: if(Matrix[curr_room_i][curr_room_j-1] == 0){Matrix[curr_room_i][--curr_room_j] = 1; noOfRooms--;} break;
                     case RIGHT:if(Matrix[curr_room_i][curr_room_j+1] == 0){Matrix[curr_room_i][++curr_room_j] = 1; noOfRooms--;} break;
                     case UP:   if(Matrix[curr_room_i-1][curr_room_j] == 0){Matrix[--curr_room_i][curr_room_j] = 1; noOfRooms--;} break;
                     case DOWN: if(Matrix[curr_room_i+1][curr_room_j] == 0){Matrix[++curr_room_i][curr_room_j] = 1; noOfRooms--;} break;
                     default: std::cout<<"Invalid Direction !!!";
                 }
            }
            else
            {
                switch(rand()%4)
                 {
                     case LEFT: if (curr_room_j-1 >= 0)
                                {
                                    if(Matrix[curr_room_i][curr_room_j-1] == 0) noOfRooms--;
                                    Matrix[curr_room_i][--curr_room_j] = 1;
                                }break;
                     case RIGHT: if (curr_room_j+1 < theSize)
                                {
                                    if(Matrix[curr_room_i][curr_room_j+1] == 0) noOfRooms--;
                                    Matrix[curr_room_i][++curr_room_j] = 1;
                                }break;
                     case UP:    if (curr_room_i-1 >= 0)
                                {
                                    if(Matrix[curr_room_i-1][curr_room_j] == 0) noOfRooms--;
                                    Matrix[--curr_room_i][curr_room_j] = 1;
                                }break;
                     case DOWN:  if (curr_room_i+1 < theSize)
                                {
                                    if(Matrix[curr_room_i+1][curr_room_j] == 0) noOfRooms--;
                                    Matrix[++curr_room_i][curr_room_j] = 1;
                                }break;
                     default: std::cout<<"Invalid Direction !!!";
                 }
            }
        }
        createGUIMaze();//creating the GuiMaze
    }
    
    //function to retrive the first position of the maze
    
    int matrix::getMartixFirstX()
    {
        return first_room_i;
    }
    
    int matrix::getMatrixFirstY()
    {
        return first_room_j;
    }
    
    //Function to check if an element is in matrix
    
    bool matrix:: inMatrix(int valX, int valY) const
    {
        if(valX >= 0 && valX < theSize && valY >= 0 && valY < theSize)
            return true;
        else return false;
    }
    
    //Function to check if the neighboring elments of a an element are in matrix
    
    bool matrix::allNeighborInMatrix(int valX, int valY) const
    {
        if(valX-1 > 0 && valX+1 < theSize && valY-1 > 0 && valY+1 < theSize)
            return true;
        else return false;
    }
    
    //Function to check if the surrounding neighbors of a matrix element is one
    
    bool matrix::isAllNeighborOne(int valX, int valY) const
    {
        if(allNeighborInMatrix(valX,valY) && Matrix[valX][valY-1] == 1 && Matrix[valX][valY+1] == 1 && Matrix[valX-1][valY] == 1 && Matrix[valX+1][valY] == 1)
            return true;
        else return false;
    }
    
    void matrix::printMaze() const
    {
        for(int i = 0; i< theSize; i++)
        {
            for( int j = 0; j<theSize; j++)
               {
                    std::cout<<Matrix[i][j]<<" ";
               }
               std::cout<<"\n";
        }
    }
    
    //Function to create the GUI Maze
    
    void matrix::createGUIMaze()
    {
        matrixGridLayout = new QGridLayout;
    
        for(int k = 0; k < theSize; k++)
        {
            matrixGridLayout->addWidget(new room(DIAGONAL),k,k,Qt::AlignCenter);
        }
        for(int i = 0; i< theSize; i++)
        {
            for(int j =0; j < theSize; j++)
            if(Matrix[i][j] == 1)
            {
                if(i == getMartixFirstX() && j == getMatrixFirstY())
                    matrixGridLayout->addWidget(new room(START),j,i,Qt::AlignCenter);
                else
                {
                    matrixGridLayout->addWidget(new room(NORMAL_ROOMS,getRoomCode(i,j)),j,i,Qt::AlignCenter);
                }
            }
           // else if(Matrix[i][j] == 0) matrixGridLayout->addWidget(new room(0),j,i,Qt::AlignCenter);
        }
        matrixGridLayout->setSpacing(0);
        matrixGridLayout->setMargin(0);
    }
    
    //Function to retrive the grid layout representing the maze
    
    QGridLayout *matrix::getLayout()
    {
        return matrixGridLayout;
    }
    
    //Function to get roomCode
    
    int matrix::getRoomCode(int x, int y)
    {
        categorizeRooms(x,y);
        //four doors
    
        if(roomCodeArray[LEFT] == TRUE_A && roomCodeArray[RIGHT] == TRUE_A && roomCodeArray[UP] == TRUE_A && roomCodeArray[DOWN] == TRUE_A)
        {
            return FOUR;
        }
        //three doors
    
        else if(roomCodeArray[LEFT] == TRUE_A && roomCodeArray[RIGHT] == TRUE_A && roomCodeArray[UP] == TRUE_A && roomCodeArray[DOWN] == FALSE_A)
        {
            return THREE_LRU;
        }
        else if(roomCodeArray[LEFT] == TRUE_A && roomCodeArray[RIGHT] == TRUE_A && roomCodeArray[UP] == FALSE_A && roomCodeArray[DOWN] == TRUE_A)
        {
            return THREE_LRD;
        }
        else if(roomCodeArray[LEFT] == TRUE_A && roomCodeArray[RIGHT] == FALSE_A && roomCodeArray[UP] == TRUE_A && roomCodeArray[DOWN] == TRUE_A)
        {
            return THREE_LUD;
        }
        else if(roomCodeArray[LEFT] == FALSE_A && roomCodeArray[RIGHT] == TRUE_A && roomCodeArray[UP] == TRUE_A && roomCodeArray[DOWN] == TRUE_A)
        {
            return THREE_RUD;
        }
    
        //two opposite doors
    
        else if(roomCodeArray[LEFT] == FALSE_A && roomCodeArray[RIGHT] == FALSE_A && roomCodeArray[UP] == TRUE_A && roomCodeArray[DOWN] == TRUE_A)
        {
            return TWO_UD;
        }
        else if(roomCodeArray[LEFT] == TRUE_A && roomCodeArray[RIGHT] == TRUE_A && roomCodeArray[UP] == FALSE_A && roomCodeArray[DOWN] == FALSE_A)
        {
            return TWO_LR;
        }
    
        //two adjacent doors
    
        else if(roomCodeArray[LEFT] == FALSE_A && roomCodeArray[RIGHT] == TRUE_A && roomCodeArray[UP] == FALSE_A && roomCodeArray[DOWN] == TRUE_A)
        {
            return TWO_RD;
        }
        else if(roomCodeArray[LEFT] == FALSE_A && roomCodeArray[RIGHT] == TRUE_A && roomCodeArray[UP] == TRUE_A && roomCodeArray[DOWN] == FALSE_A)
        {
            return TWO_RU;
        }
        else if(roomCodeArray[LEFT] == TRUE_A && roomCodeArray[RIGHT] == FALSE_A && roomCodeArray[UP] == FALSE_A && roomCodeArray[DOWN] == TRUE_A)
        {
            return TWO_LD;
        }
        else if(roomCodeArray[LEFT] == TRUE_A && roomCodeArray[RIGHT] == FALSE_A && roomCodeArray[UP] == TRUE_A && roomCodeArray[DOWN] == FALSE_A)
        {
            return TWO_LU;
        }
    
        //one door
    
        else if(roomCodeArray[LEFT] == TRUE_A && roomCodeArray[RIGHT] == FALSE_A && roomCodeArray[UP] == FALSE_A && roomCodeArray[DOWN] == FALSE_A)
        {
            return ONE_L;
        }
        else if(roomCodeArray[LEFT] == FALSE_A && roomCodeArray[RIGHT] == TRUE_A && roomCodeArray[UP] == FALSE_A && roomCodeArray[DOWN] == FALSE_A)
        {
            return ONE_R;
        }
        else if(roomCodeArray[LEFT] == FALSE_A && roomCodeArray[RIGHT] == FALSE_A && roomCodeArray[UP] == TRUE_A && roomCodeArray[DOWN] == FALSE_A)
        {
            return ONE_U;
        }
        else if(roomCodeArray[LEFT] == FALSE_A && roomCodeArray[RIGHT] == FALSE_A && roomCodeArray[UP] == FALSE_A && roomCodeArray[DOWN] == TRUE_A)
        {
            return ONE_D;
        }
        else if(roomCodeArray[LEFT] == FALSE_A && roomCodeArray[RIGHT] == FALSE_A && roomCodeArray[UP] == FALSE_A && roomCodeArray[DOWN] == FALSE_A)
        {
            return ONE_D;
        }
        return N_A;
    }
    
    //Function to categorize different types of poosible rooms like rooms with two doors, rooms with three doors and so on
    
    void matrix::categorizeRooms(int x, int y)
    {
        if(allNeighborInMatrix(x,y))
        {
            setRoomCode(true,true,true,true,x,y);
        }
        else
        {
           if(x == 0 && y == 0)
           {
               setRoomCode(false,true,false,true,x,y);
           }
           else if(x == 0 && y == theSize-1)
           {
               setRoomCode(true,false,false,true,x,y);
           }
           else if(x == theSize-1 && y == theSize-1)
           {
               setRoomCode(true,false,true,false,x,y);
           }
           else if(x == theSize-1 && y == 0)
           {
               setRoomCode(false,true,true,false,x,y);
           }
           else
           {
               if(x == 0) setRoomCode(true,true,false,true,x,y);
               if(x == theSize-1) setRoomCode(true,true,true,false,x,y);
               if(y == 0) setRoomCode(false,true,true,true,x,y);
               if(y == theSize-1) setRoomCode(true,false,true,true,x,y);
           }
        }
    }
    
    //Function to set roomCodeArray
    //The boolean arguments in this function decides whether it is allowed to check
    //in a direction or not, directions being LEFT, RIGHT, UP and DOWN
    
    void matrix::setRoomCode(bool leftPermission, bool rightPermission, bool upPermission, bool downPermission, bool x, bool y)
    {
        //re-initializing the roomCodeArray
        for(int i=0 ; i<4; i++)
        {
            roomCodeArray[i] = NONE_A;
        }
    
        //setting the room code
    
        if(leftPermission == true)
        {
            if(Matrix[x][y-1] == 1)
            roomCodeArray[LEFT] = TRUE_A;
            else if(Matrix[x][y-1] == 0)
            roomCodeArray[LEFT] = FALSE_A;
        }
        if(rightPermission == true)
        {
            if(Matrix[x][y+1] == 1)
            roomCodeArray[RIGHT] = TRUE_A;
            else if(Matrix[x][y+1] == 0)
            roomCodeArray[RIGHT] = FALSE_A;
        }
        if(upPermission == true)
        {
            if(Matrix[x-1][y] == 1)
            roomCodeArray[UP] = TRUE_A;
            else if(Matrix[x-1][y] == 0)
            roomCodeArray[UP] = FALSE_A;
        }
        if(downPermission == true)
        {
            if(Matrix[x+1][y] == 1)
            roomCodeArray[DOWN] = TRUE_A;
            else if(Matrix[x+1][y] == 0)
            roomCodeArray[DOWN] = FALSE_A;
        }
    
        for(int i=0 ; i<4; i++)
        {
            if(roomCodeArray[i] == NONE_A)
            roomCodeArray[i] = FALSE_A;
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Syl    7 年前

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

    这可能是您的问题,请将其更改为int?