Recurrsion Maze

I'm having some trouble with the actual recursive part. It's a maze that displays from a text file which opens file and displays as it should, but I cant get the bread crumb'@' to display in the maze

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
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

bool mazeEscape(rows, cols)
{
    int breadcrumb='@';
	If()
		Return false;
	If ('@'=='E')?
		Return true;
	If outside of maze ?
		Return false;
	If breadcrumb ?
		Return false;


	if (mazeEscape(row - 1, col))	   // recursively move up
		return true;

	else if (mazeEscape(row + 1, col)) // recursively move down
		return true;

	else if (mazeEscape(row, col - 1)) // recursively move left
		return true;
	else if (mazeEscape(row, col + 1)) // recursively move right
		return true;

	else
		return false;
}
int main()
{
	const int ROWS = 10;
	const int COLS = 10;


	char maze[ROWS][COLS];

	string filename;
	string line;
	cout << " Please Select a Maze ";
	getline(cin, filename);
	fstream file(filename, ios::in);
	if (file.is_open())
	{
		for (int row = 0; row < ROWS; row++)
		{
			getline(file, line);
			for (int col = 0; col < COLS; col++)
			{
				maze[row][col] = line[col];
			}
		}
	}
	for (int row = 0; row < ROWS; row++)
	{
		for (int col = 0; col < COLS; col++)
		{
			cout << maze[row][col];
		}
		cout<< endl;
	}
	//Recursively move UP = 1 less then current

	
	system("pause");
		return 0;
		
	/*
	//General CASE
	drop breadcrumb '@'*/








}
Lines 10-16: Your if statements need work. if is not capitalized. Your conditions are pseudo-code, not C++ statements. return should not be capitalized.

Line 10: What is this supposed to be testing?

Line 12: Comparing two different character literals will never be true.

Line 36-37: move to after line 6 so these consts are available to the entire program.

line 14: Outside the maze is easy if you know the size of the maze.
14
15
16
17
 
  //  Note the change from rows,cols to row,col
  if (row < 0 || row >= ROWS || col < 0 || col >= COLS) 
    return false;


Line 16: It's a little hard to check for a breadcrumb if you don't have access to the maze. You need to pass maze as an argument to mazeEscape.

7
8
9
bool mazeEscape(char maze[ROWS][COLS], row, col)  
// changed rows,cols to singular
//  See below 


16
17
  if (maze(row][col] == breadcrumb)
    return true;


Lines 20,23,26,28: You use row,col here, but your argument names are rows,cols.

Line 47: If you can't open the file, you fall through to line 58 as if nothing had happened. You should report that you can't open the file and exit the program (or prompt for the filename again).

Lines 58-65: You probably want to put displaying the maze in a separate function.

I don't see where you ever call mazeEscape from main().
Last edited on
In addition to what AbstractAnon said, you still need code to drop and pick up the breadcrumb. I suggest changing lines 20-32 to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    bool result = false;

    // drop the bread crump

    if (mazeEscape(row - 1, col))   // recursively move up
        result =  true;
    else if (mazeEscape(row + 1, col)) // recursively move down
        result =  true;
    else if (mazeEscape(row, col - 1)) // recursively move left
        result =  true;
    else if (mazeEscape(row, col + 1)) // recursively move right
        result =  true;

    // Pick up the bread crump

    return result;


Note that this means you don't drop the breadcrumb until after you check for other checks (outside of maze, hit an existing crumb, found the exit etc.). That's important.
ok ive changed those things but it keeps telling me row and col are undefined when they are defined with the array


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
 const int ROWS = 10;
const int COLS = 10;

bool mazeEscape(char maze[ROWS][COLS], row, col)
{
	
	bool result = false;

	// drop the bread crump

	if (mazeEscape(row - 1, col))   // recursively move up
		result = true;
	else if (mazeEscape(row + 1, col)) // recursively move down
		result = true;
	else if (mazeEscape(row, col - 1)) // recursively move left
		result = true;
	else if (mazeEscape(row, col + 1)) // recursively move right
		result = true;

	// Pick up the bread crump

	return result;

	if (row < 0 || row >= ROWS || col < 0 || col >= COLS)
		return false;
	
	if (mazeEscape([row][col] == '@')
		return true;



	if (mazeEscape(row - 1, col))	   // recursively move up
		return true;

	else if (mazeEscape(row + 1, col)) // recursively move down
		return true;

	else if (mazeEscape(row, col - 1)) // recursively move left
		return true;
	else if (mazeEscape(row, col + 1)) // recursively move right
		return true;

	else
		return false;
}
int main()
{
	


	char maze[ROWS][COLS];

	string filename;
	string line;
	cout << " Please Select a Maze ";
	getline(cin, filename);
	fstream file(filename, ios::in);
	if (file.is_open())
	{
		for (int row = 0; row < ROWS; row++)
		{
			getline(file, line);
			for (int col = 0; col < COLS; col++)
			{
				maze[row][col] = line[col];
			}
		}
	}
	for (int row = 0; row < ROWS; row++)
	{
		for (int col = 0; col < COLS; col++)
		{
			cout << maze[row][col];
		}
		cout<< endl;
	}
	 

	
	system("pause");
		return 0;
Line 4: You have to give the type of row and col:
bool mazeEscape(char maze[ROWS][COLS], int row, int col)

Programming is about being very precise in your expression - much more precise than we're used to in speech and writing. For example, I said
I suggest changing lines 20-32
but you have added the lines instead of replacing them.

Now that you've changed the parameters to mazeEscape, you have to go through the program and change each call to mazeEscape to match the new params.

Here is the code with the syntax errors fixed. There are still plenty of logic errors but this will help you make progress:
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
#include <iostream>
#include <fstream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ios;
using std::ifstream;

const int ROWS = 10;
const int COLS = 10;

bool
mazeEscape(char maze[ROWS][COLS], int row, int col)
{

    if (row < 0 || row >= ROWS || col < 0 || col >= COLS)
        return false;

    if (maze[row][col] == 'E')
        return true;

    // drop the bread crump

    bool result;
    if (mazeEscape(maze, row - 1, col))          // recursively move up
        result = true;

    else if (mazeEscape(maze, row + 1, col))     // recursively move down
        result = true;

    else if (mazeEscape(maze, row, col - 1))     // recursively move left
        result = true;
    else if (mazeEscape(maze, row, col + 1))     // recursively move right
        result = true;

    else
        result = false;

    // Pick up the bread crump
    return result;
}

int
main()
{

    char maze[ROWS][COLS];

    string filename;
    string line;
    cout << " Please Select a Maze ";
    getline(cin, filename);
    std::ifstream file(filename, ios::in);
    if (file.is_open()) {
        for (int row = 0; row < ROWS; row++) {
            getline(file, line);
            for (int col = 0; col < COLS; col++) {
                maze[row][col] = line[col];
                maze[row][col] = line[col];
            }
        }
    }
    for (int row = 0; row < ROWS; row++) {
        for (int col = 0; col < COLS; col++) {
            cout << maze[row][col];
        }
        cout << endl;
    }

    mazeEscape(maze, 0, 0);
    return 0;
}

im not sure i understand the actual recursion then ive set my "breadcrumb to the '@' and its still not displaying nor are the starting points ive tried to hard code "mazeEscape(maze,0,3)
Please post your current code and the input file.

What is output supposed to be? A printout of the maze with the breadcrumbs to the exit?
yes ive got it to display using a printMaze function and it completes the maze Im just having trouble with a while loop to for it to go into the bottom of the text file and using those starting points

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void printMaze(char maze[ROWS][COLS], int row, int col)
{
	for (int row = 0; row < ROWS; row++)
	{
		for (int col = 0; col < COLS; col++)
		{
			cout << maze[row][col] << ' ';
		}
		cout << endl;
	}
	cout << "Press Enter to continue";
	cin.get();
	cout << endl;
}

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
int main()
{

	char maze[ROWS][COLS];

	string filename;
	string line;
	int Num = 0;
	int startingRow = 0;
	int startingCol = 0;
	cout << " Please Select a Maze ";
	getline(cin, filename);
	ifstream file(filename, ios::in);
	if (file.is_open()) 
	{
		for (int row = 0; row < ROWS; row++) 
		{
			getline(file, line);
			for (int col = 0; col < COLS; col++)
			{
				
				maze[row][col] = line[col];
			}
		}
	}
	
	
	if (mazeEscape(maze, 1, 1))//hardcoded starting points that work
		cout << "horray I'm Free" << endl;
	else
		cout << "Help Im Trapped" << endl;

	system("pause");
	return 0;
 

im thinking somthing like mazeEscape(maze,Num>9[0],
to get the starting points on its own 9 being the 1st point in the array and im a little lost
+++ +++ ++
this is the text file Im working with


+ + +++ ++
+ + ++
+ +++++
+ +++++
+ ++ +
+ + +++ +
+ + ++++ E
+++ +
++++++++++
0 3
1 1
4 1
6 1
6 3

Topic archived. No new replies allowed.