C++ random maze generator program keeps crashing most of the time

Hi there, I am trying to make a program where a random maze is generated however, the program crashes most of the time. I tried to solve this without using struct and without creating a 2D array dynamically and it worked fine then. But after doing it in this way the program doesnt work. I think there is something wrong in the SolveMaze() function but i cant figure out what it is :(

Here is the code.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int ROW=7;
const int COL=7;

class myclass
{

private:

    struct M
    {
        int val;
        bool validPath;
    };

    M **Maze;
    int x;
    bool MazeSolved=false;

public:

    void CreateRandomMaze();
    void SolveMaze(int,int);
    void CanMazeBeSolved();
};

void myclass::CreateRandomMaze()
{

    Maze=new M*[ROW];
    for(int i=0;i<COL;i++)
    {
        Maze[i]=new M[COL];
    }

    srand(time(0));

    for(int i=0;i<ROW;i++)
    {
        for(int j=0;j<COL;j++)
        {
            x=rand()%4;
            Maze[i][j].val=x;

            if(Maze[ROW-1][COL-1].val!=1)
            {
                Maze[ROW-1][COL-1].val=1;
            }
            if(Maze[0][0].val!=1)
            {
                Maze[0][0].val=1;
            }
            if(Maze[i][j].val > 0)
            {
                Maze[i][j].val=1;
            }

            Maze[i][j].validPath=false;

            cout << Maze[i][j].val << " ";
        }
        cout << endl;
    }
}

void myclass::CanMazeBeSolved()
{
    SolveMaze(ROW-1,COL-1);

    if(MazeSolved==true)
    {
        cout << "This maze can be solved." << endl;
    }

    else
    {
        cout << "This maze cannot be solved." << endl;
    }
}

void myclass::SolveMaze(int row,int col)
{
    if(row==0 && col==0)
    {
        MazeSolved=true;
        return;
    }

    if(Maze[row-1][col].val==1 && MazeSolved==false && row < ROW)
    {
        SolveMaze(row-1,col);
    }

    if(Maze[row][col-1].val==1 && MazeSolved==false && col < COL)
    {
        SolveMaze(row,col-1);
    }

    return;
}

int main()
{
    myclass c;
    c.CreateRandomMaze();
    c.CanMazeBeSolved();
    return 0;
}  
Last edited on
Could you edit your post and provide code tags for your code to make it readable - http://www.cplusplus.com/articles/jEywvCM9/
The problem is when row becomes 0 and you access Maze with an index of -1.

1
2
3
4
 if(Maze[row-1][col].val == 1 && MazeSolved == false && row < ROW)
  {
    SolveMaze(row-1,col);
  }
@Thomas1965 Hi, you are right and i missed that point and so i changed the if statement to this

1
2
3
4
    if(Maze[row-1][col].val==1 && MazeSolved==false && row < ROW && row > 0)
    {
        SolveMaze(row-1,col);
    }


but it still does not work.
put the index check at the start of the if statement so if that test fails the -1 index won't be used in the remaining tests.

if( row < ROW && row > 0 && Maze[row-1][col].val==1 && MazeSolved==false )
@Moooce Yea i actually tried that by creating a nested if statement like this:

1
2
3
4
5
6
7
    if(row!=0)
    {
        if(Maze[row-1][col].val==1 && MazeSolved==false)
        {
            SolveMaze(row-1,col);
        }
    }


and now it works. The problem was that the program kept crashing every time row==0 happened, and I guess you are right, for some reason it didnt check the row < ROW && row > 0 part maybe because it was at the very end of the if statement. But now it is running fine.

Thank you all for helping. Peace :)
and I guess you are right, for some reason it didnt check the row < ROW && row > 0 part maybe because it was at the very end of the if statement.


http://stackoverflow.com/questions/5211961/how-does-c-handle-short-circuit-evaluation
Topic archived. No new replies allowed.