Need help with a maze game project

I have been working on this maze game for a while, but it seems like my maze.go function isn't working properly.
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
using namespace std;

int i, j;

int return0h (int a, int b, int **p) {
    for (i = 0; i < a; i++) {
        for (j = 0; j < b; j++) {
            if (p[i][j]==0) return i;
        }
    }
}

int return0v (int a, int b, int **p) {
    for (i = 0; i < a; i++) {
        for (j = 0; j < b; j++) {
            if (p[i][j]==0) return j;
        }
    }
}

class maze {
    public:
        void print() const;
        void go(string dir);
        maze();
        maze(int x, int y, int **p);
        bool isCompleted();
        void reset();
    private:
        bool prIsCompleted;
        int length;
        int width;
        int **d;
        int **dr;
        int moves;
};

maze::maze () {
    length = 3;
    width = 3;
    d = new int *[3];
    d[0] = new int[3];
    d[1] = new int[3];
    d[2] = new int[3];
    d[0][0]=0;
    d[0][1]=1;
    d[0][2]=2;
    d[1][0]=2;
    d[1][1]=1;
    d[1][2]=2;
    d[2][0]=2;
    d[2][1]=1;
    d[2][2]=3;
    moves = 0;
    prIsCompleted=0;
    dr=d;
}

maze::maze (int x, int y, int **p) {
    length = x;
    width = y;
    d = p;
    moves = 0;
    prIsCompleted=0;
    dr=d;
}

void maze::print() const {
    int c = 0;
    for (i = 0; i < length; i++) {
        for (j = 0; j < width; j++) {
            cout << d[i][j] << " ";
            c++;
            if (c == length) {
                c = 0;
                cout << endl;
            }
        }
    }
    cout << "\nMoves: " << moves << endl;
}

void maze::go (string dir) {
    int x, y;

    x = return0h(length, width, d);
    y = return0v(length, width, d);

    if (!prIsCompleted) {

        if (dir=="LEFT" && d[x-1][y]==1) {
            if (d[x-1][y]==3) {
                cout << "Congratulations, maze completed!\n";
                prIsCompleted = 1;
            }

            d[x-1][y]==0;
            d[x][y]==1;
            moves++;
        }

        else if (dir=="RIGHT" && d[x+1][y]==1) {
            if (d[x+1][y]==3) {
                cout << "Congratulations, maze completed!\n";
                prIsCompleted = 1;
            }

            d[x+1][y]==0;
            d[x][y]==1;
            moves++;
        }

        else if (dir=="UP" && d[x][y+1]==1) {
            if (d[x][y+1]==3) {
                cout << "Congratulations, maze completed!\n";
                prIsCompleted = 1;
            }

            d[x][y+1]==0;
            d[x][y]==1;
            moves++;
        }

        else if (dir=="DOWN" && d[x][y-1]==1) {
            if (d[x][y-1]==3) {
                cout << "Congratulations, maze completed!\n";
                prIsCompleted = 1;
            }

            d[x][y-1]==0;
            d[x][y]==1;
            moves++;
        }

        else if (dir=="DOWN" || dir=="UP" || dir=="LEFT" || dir =="RIGHT") cout << "Invalid move!" << endl;
        else cout << "Invalid direction, use LEFT, RIGHT, UP or DOWN to navigate through the maze" << endl;
    }
    else cout << "Can't move because maze is completed\n";
}

bool maze::isCompleted() {
    if (prIsCompleted==1) return 1;
    else return 0;
}

void maze::reset() {
    moves=0;
    d = dr;
}


int main () {
    /* int a, b;

    cout << "Enter maze length and width" << endl;
    cin >> a >> b;

    int **p;

    p = new int *[a];

    for (i = 0; i < a; i++) {
        p[i] = new int[b];
    }

    for (i = 0; i < a; i++) {
        for (j = 0; j < b; j++) {
            cout << "Entering element [" << i+1 << "][" << j+1 <<  "]" << endl;
            cin >> p[i][j];
        }
    } */

    string direction;
    maze m;
    while (!m.isCompleted()) {
        m.print();
        cout << endl;
        cout << "Where do you want to move?\n";
        cin >> direction;
        cout << endl;
        m.go(direction);
    }

}


If 0 is in the top left corner, and to navigate you have to go RIGHT (default), if you go LEFT it causes a run time error, if you go RIGHT it says Invalid move!, and if you go UP, it will add 1 to the moves counter, but nothing else would happen. Also, if someone can help me create a function that would make a random maze, that would be nice (or article that might help me with this).
Last edited on

Lines 98/99 you are using == instead of =, same with 109, 110,120, 121, 131, and 132.

There may be other issues but those stand out.

Fixed it, still same errors.
Line 12: What does return0h return if none of the cells are 0?
Line 20: ditto

Lines 46-54: What do these values mean? You have no comments. You should consider using a enum here. Not providing a comment or using an enum makes your code hard toread.

Line 109,110,120,121,131,132: You've edited your original post, but the code still shows you're using the wrong operator. These should be an assignment (=), not an equality operator (==).

Lines 25,84: You trying to use std::string, but haven't included the <string> header.

if you go LEFT it causes a run time error

Well, yeah. You're indexing out of bounds.
Line 93,98: What happens when x is 0? These statements become
1
2
3
4
 
  if (d[-1][y]
...
  d[-1][y] = 0;

Both are illegal references.

You have this problem in all four directions.

Lines 56,65,95,106,117,128: You're setting prIsCompleted to an int value. That's legal, but the better practice is to use the bool constants true and false.

Lines 142-145: maze::isCompleted does not need an if statement. Simply return the bool variable.
1
2
3
bool maze::isCompleted() 
{   return prIsCompleted; 
}





Last edited on
Topic archived. No new replies allowed.