C++ maze

Hello,

I've been working on a C++ maze assignment but I've become stuck -- below is the code I have so far. I'm trying to get it to print the unsolved maze (read from a txt file) and then the solved maze.

Any input on what I need to do to get it to print the solved maze would be appreciated...thanks.

Daniel


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
#include<stdio.h>
#include<iostream>
#include <cstdlib>
#include<ctime>
#define FALSE 0
#define TRUE 1

using namespace std;
const int SIZE=12;

class Maze {
	private:
	char  M[SIZE][SIZE]; // array of random numbers
	int size; // size of the array

	public:
	Maze();  // constructor
	void read_maze();
	int maze_traverse(int row, int col);
	void display_maze();
};

int main() {
	int i, j;
	Maze  M;
	
	M.display_maze();
	M.maze_traverse(2,0);
	M.display_maze();
}

// constructor
Maze::Maze() {
	read_maze();
}

// constructor
void Maze::read_maze() {
	int i, j;

	FILE* ifp;

	ifp=fopen("maze.txt", "r");

	for(i=0; i< SIZE; i++)
	for(j=0; j< SIZE; j++)
		fscanf(ifp, " %c ", &M[i][j]);
}

int Maze::maze_traverse(int row, int col) {
	// return TRUE if at the end of the maze
	if (M[row][col] == 'E') return TRUE;

	// return FALSE if not at the end of the maze
	if (M[row][col] > SIZE-1 || M[row][col] == 'S') return FALSE;

	// mark + for part of the path
	M[row][col] = '+';

	// return TRUE if the path goes to the North
	if (M[row-1][col] == TRUE) return TRUE;

	// return TRUE if the path goes to the South
	if (M[row+1][col] == TRUE) return TRUE;

	// return TRUE if the path goes to the East
	if (M[row][col+1] == TRUE) return TRUE;

	// return TRUE if the path goes to the West
	if (M[row][col-1] == TRUE) return TRUE;

	// mark x for the wrong path
	M[row][col] = 'x';

	return FALSE;
}

void Maze::display_maze(){
	int i, j;
	
	for(i=0; i<SIZE; i++) {
	for(j=0; j< SIZE; j++)
		printf("%c ", M[i][j]);
		cout << endl;
	}
	cout << endl;
}

/*
// Pause the console so it doesn't close (when running in Windows)
cout << endl << endl << endl << endl << endl;
system("pause");
*/




This is what the txt file looks like:
1
2
3
4
5
6
7
8
9
10
11
12
# # # # # # # # # # # #
# . . . # . . . . . . #
S . # . # . # # # # . #
# # # . # . . . . # . #
# . . . . # # # . # . E
# # # # . # . # . # . #
# . . # . # . # . # . #
# # . # . # . # . # . #
# . . . . . . . . # . #
# # # # # # . # # # . #
# . . . . . . # . . . #
# # # # # # # # # # # # 
if (M[row-1][col] == TRUE) return TRUE;

But your arrays elements never contain the value TRUE. So those conditions will never succeed.
So should it be something like
if (M[row-1][col] == '+') return TRUE;
instead? I'm just not sure what to do. Thanks
You will need a lot more code if you want to know if a move is valid and where that path leads.

First check if a move is valid by comparing it the '.' char. If you want to know which branch they are taking you could insert a special char at each branch so when they reach it you can determine which path they will take.
Topic archived. No new replies allowed.