Issue with maze solving program!

I'm not sure whats wrong with this program
It runs but has something incorrect in it and please don't yell at me for using system(""); commands i know they security stuff i'll fix that later.

The programs not quite finished but it should show movement of The computer abit not finish it though!! WHY WON"T IT lol.
My programs abit long sorry;
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <fstream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <utility>

using namespace std;

string Direction(string &up, string &down, string &left, string &right) {
    string ReturnTXTl;
    int check = 0;
    Sleep(100);

    if (up == "_" or up == "F") {
        check++;
        ReturnTXTl = "up";
    }
    if (down == "_" or down == "F") {


        check++;
        ReturnTXTl = "down";

    }
    if (left == "_" or left == "F") {

        check++;
        ReturnTXTl = "left";

    }
    if (right == "_" or right == "F") {
        check++;
        ReturnTXTl = "right";

    }
    if (check > 1)

    {
        ReturnTXTl = "checkpoint";
    }
    if (check ==0) {
        ReturnTXTl = "previous";
    }
    cout<<ReturnTXTl<<endl;
    return ReturnTXTl;
}



char stringtochar(string a, int b) {
    char character = a[b];
    return character;
}

void Create() {

start:
    string tmp;
    int Mode;
    int x, y;
    char Filename[100];
    cout << "Enter: \n1. To create a new maze.\n2. To edit a saved maze.\n3. To quit to main Menu." << endl;
    cin >> Mode;

    if (Mode == 1) {

        cout << "Enter filename to save to with .txt at end." << endl;
        cin >> Filename;
        ofstream Mazer1(Filename);
        cout << "Creating file...." << endl;
        Sleep(100);
        cout << "Opening file....." << endl;
        cout << "Enter maze width:";
        cin >> x;
        cout << "Enter maze height:";
        cin >> y;
        string Maze[y];
        Mazer1 << y << endl;
        system("cls");
        cout << endl << "Enter Lines:\t\tLines left: " << y << endl;

        for (int n = 0; n < y; n++) {
            cin >> tmp;
            Maze[n] = tmp;
            Mazer1 << tmp.substr(0, x) << endl;
        }

        Mazer1.close();

        system("PAUSE");
    }


    else if (Mode == 2) {

        cout << "Enter filename to edit:" << endl;
        cin >> Filename;
        string Line[100];
        int lines, count = 1, Tempw;
        fstream Mazer2;
        Mazer2.open(Filename, fstream::in | fstream::out);

        if (!Mazer2) {
            cerr << "Error opening input!!!" << endl;
            system("PAUSE");
        }

        while (! Mazer2.eof()) {
            getline(Mazer2, Line[count]);
            cout << Line[count] << endl;
            count++;
        }

        char temp[100];

        for (int i = 0; i < Line[1].length(); i++) {
            temp[i] += Line[1][i];
        }

        lines = atoi(temp);

        cout << "There are: " << lines << " lines!!" << endl;
        Sleep(1000);
        cout << "How many lines will you be adding?" << endl;
        cin >> Tempw;
        lines += 2;
        Mazer2.close();
        ofstream Mazer3(Filename);

        for (int t = 0;t < lines;t++) {
            cout << Line[t] << endl;
        }

        for (int j = 0; j < Tempw; j++) {
            cin >> tmp;
            Line[lines+j] = tmp;
        }

        for (int e = 0; e <= (lines);e++) {
            Mazer3 << Line[e+1] << endl;
        }


        system("PAUSE");

        Mazer3.close();
    }



    else if (Mode == 3) {
        return;
    }

    else {
        cout << "Invalid Entry Try again!!!" << endl;
        Sleep(1000);
        system("cls");
        goto start;
    }
}

void Solve() {
    //Inilisation of Variables

    bool Finished = false, foundstart = false;
    string Maze[100][100];
    char Filename[100];
    cout << "Enter filename to edit:" << endl;
    cin >> Filename;
    string Line[100];
    int lines, count = 1;
    fstream Mazer2;
    Mazer2.open(Filename, fstream::in | fstream::out);

    if (!Mazer2) {
        cerr << "Error opening input!!!" << endl;
    }

    while (! Mazer2.eof()) {
        getline(Mazer2, Line[count]);
        cout << Line[count] << endl;
        count++;
    }

    char temp[100];

    for (int i = 0; i < Line[1].length(); i++) {
        temp[i] += Line[1][i];
    }

    lines = atoi(temp);

    cout << "There are: " << lines << " lines!!" << endl;

    //Maze Inilisation

    for (int a = 1;a <= lines;a++) {
        for (int b = 0;b < (Line[a+1].size());b++) {
            Maze[b][a] = stringtochar(Line[a+1], b);
            cout << Maze [b][a];
        }

        cout << endl;
    }

    int Position[3];

    int checkpoint1[50];
    int checkpoint2[50];


    while (foundstart == false) {
        for (int a = 1 ;a < 10;a++) {
            if (Maze[a][1] == "S") {
                foundstart = true;
                checkpoint1[1] = a+10;
                checkpoint2[1] = 1;
                Position[0] = a+1;
                Position[1] = 1;


            }
        }
    }
    cout<<Position[0]<<" ,"<<Position[1]<<endl;

    string up, down, left, right, arrow;
    system("PAUSE");
    int previous = 1;

    while (Finished == false) {
        up   =  Maze[Position[0]][Position[1] - 1];
        down  = Maze[Position[0]][Position[1] + 1];
        left  = Maze[Position[0] - 1][Position[1]];
        right  = Maze[Position[0] + 1][Position[1]];
        arrow = Direction(up, down, left, right);

        if (arrow == "up") {
            Position[1]--;
            Maze[Position[0]][Position[1]] = 'P';
        }

        else if (arrow == "down") {
            Position[1]++;
            Maze[Position[0]][Position[1]] = 'P';
        }

        else if (arrow == "left") {
            Position[0]--;
            Maze[Position[0]][Position[1]] = 'P';
        }

        else if (arrow == "right") {
            Position[0]++;
            Maze[Position[0]][Position[1]] = 'P';
        }

        else if (arrow == "checkpoint") {
            checkpoint1[previous] = Position[0];
            checkpoint2[previous] = Position[1];
            previous++;
        }
        else if (arrow == "previous") {
            Position[0] = checkpoint1[previous];
            Position[1] = checkpoint2[previous];
            previous--;
        }

        else if (Maze[Position[0]][Position[1]] == "f") {
            Finished = true;
        }



    }


    cout << endl;

    Mazer2.close();
    for (int a = 1;a <= (lines);a++) {
        for (int b = 0;b < (Line[a+1].size());b++) {
            cout << Maze [b][a];
        }

        cout << endl;
    }
    system("PAUSE");
}



int main() {

start:
    ofstream Mazer;
    int Mode;
    cout << "Welcome to maze bot!!" << endl << endl;
    Sleep(500);
    cout << "Enter: \n1. To create a maze.\n2. To solve it.\n3. To quit program." << endl;
    cin >> Mode;

    switch (Mode) {

    case 1:
        system("cls");
        Create();
        system("cls");
        goto start;
        break;

    case 2:
        system("cls");
        Solve();
        system("cls");
        goto start;
        break;

    case 3:
        exit(1);

    default:
        cout << "Incorrect Syntax!!!(Jerk)";
        Sleep(1000);
        system("cls");
        goto start;
        break;

    }

    Mazer.close();
}


Sorry bout code length and how do i make tabs work with these questions??
[/c0de]



Compiler=CODE::BLOCKS:
and it runs

bump!!!
BLA BORED NEED ANSWER LOL!!
Last edited on
Bump i really need answer before within an hour
closed account (z05DSL3A)
It compiles?


else if ( check > 1 ); somewhere around line 76, should probably be else if ( check > 1 )


In your solve function you have code like Maze[Position[1]][Position[2]] == 'P';, this will do nothing, should it be Maze[Position[1]][Position[2]] = 'P';?


string Maze[y]; I would not expect this to compile, as y is not constant.
...


How to: Put code into your postings
http://www.cplusplus.com/forum/articles/1624/
Last edited on
Lol mingw isn't that fussy usually Thank you though Helped alot
everything besides one thing is fixed
whats wrong with this?
up = Maze[Position[1]][Position[2] - 1];
closed account (z05DSL3A)
With int Position[2]; you should be referencing as Position[0] and Position[1].

Last edited on
Bump still doesn't work somethings up in the Direction function i think.
Bump
closed account (z05DSL3A)
Re-post the code in its current state and give a little more detail to "still doesn't work..."
Last edited on
I have reposted the code :p
and when i go into solve from the menu and type in the file to load it'salright then when i press a key to continue it errors!!
1
2
3
4
         up   =  Maze[Position[0]][Position[1] - 1];
        down  = Maze[Position[0]][Position[1] + 1];
        left  = Maze[Position[0] - 1][Position[1]];
        right  = Maze[Position[0] + 1][Position[1]];

Those lines are flagged in debug
both strings though
closed account (z05DSL3A)
Sorry, can you also post the contents of a sample 'maze' file, so I can run it.
just create a maze with
3
#S#
#_#
#F#

goto create maze then create new maze then do that :p
line by line
without the 3
the 3 is automatic
and is it amount of lines
Last edited on
closed account (z05DSL3A)
The results from compiling the code at the top of the post

Compiling...
test.cpp
c:\data\projects\consoletest\test\test.cpp(82) : error C2057: expected constant expression
c:\data\projects\consoletest\test\test.cpp(82) : error C2466: cannot allocate an array of constant size 0
c:\data\projects\consoletest\test\test.cpp(82) : error C2133: 'Maze' : unknown size
c:\data\projects\consoletest\test\test.cpp(121) : warning C4018: '<' : signed/unsigned mismatch
c:\data\projects\consoletest\test\test.cpp(122) : warning C4244: '+=' : conversion from 'int' to 'char', possible loss of data
c:\data\projects\consoletest\test\test.cpp(193) : warning C4018: '<' : signed/unsigned mismatch
c:\data\projects\consoletest\test\test.cpp(194) : warning C4244: '+=' : conversion from 'int' to 'char', possible loss of data
c:\data\projects\consoletest\test\test.cpp(204) : warning C4018: '<' : signed/unsigned mismatch
c:\data\projects\consoletest\test\test.cpp(288) : warning C4018: '<' : signed/unsigned mismatch


NB: Line numbers are off by one to your code as I have to add #include <iso646.h> , so line 82 in the error message is line 81 in your code.

to be continued... (soon).
Thanks for this hope you like my program :D.
btw this is not in visual c++
it's in MingW
OOOH
and if you create a txt file through notepad INCLUDE THE AMOUNT OF LINES AT TOP
3
#S#
#_#
#F#
which in that case is 3
Last edited on
closed account (z05DSL3A)
I have 'fixed' the size of the 'Maze' array at 10 for my testing (line 81).

I have created a maze file, when I go to 'Solve' it reads the file but says there are zero lines and then just sits in a loop (haven't had time to look at why yet, I'm 'rebuilding' a computer on the network). The file seems to be created ok.
Thats cool
Umm in the Maze file did you create it in the program?
if not do that
closed account (z05DSL3A)
I did use the program to create the Maze file.

I have changed the code at line 192 from:
1
2
3
for (int i = 0; i < Line[1].length(); i++) {
        temp[i] += Line[1][i];
    }

to:
1
2
3
for (int i = 0; i < Line[1].length(); i++) {
        temp[i] = Line[1][i];
    }

This then gives me the correct number of lines.

I think I'm now at the point where you are;
Enter filename to edit:
maze.txt
3
#S#
#_#
#F#

There are: 3 lines!!
#S#
#_#
#F#
2 ,1
Press any key to continue . . .
previous
previous


Then it crashes with an Access violation reading location ...
It would suggest that your code is going out off the bounds of the Maze array. For example if the current Position is (0,0) then your code will try to access the Maze array at [0,-1] for the up direction.

You will need to put in some code to gaurd against going out of bounds.

HTH.

Thank You :p lol why did i put that + there lol ooh well thanks i will do that :P
Topic archived. No new replies allowed.