QtCreator C++:二维矩阵在相同 class 的不同成员函数中给出不同的结果

QtCreator C++ : 2D matrix gives different results in different member function of the same class

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

程序是这样的

//Matrix.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

//Matrix.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;
    }
}

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

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