RECURSIVELY FINDING PATHS THROUGH A MAZE.

Following program reads and prints the maze in the maze.txt, but it doesn't do any changes to the maze according to void path(int y, int x). I couldn't figure out what's wrong it. Please help me

#include <iostream>
#include <fstream>
using namespace std;

const int yMAX = 20; //Total dimension of the array on y axis
const int xMAX = 20; //Total dimension of the array on x axis

// Declaring a maze
// and we could read that file and follow the similar steps
char maze[yMAX][xMAX] = {0};

void printMaze() // function to display the maze on the screen
{
for(int y = 0; y < yMAX; y++) // loop for 2D matrix (x-axis and y-axis)
{
for(int x = 0; x < xMAX; x++)
cout << maze[y][x];
cout << "\n"; //line break
}
}

void path(int y, int x) // Defining cases on the function to determine where the @ sign should occur
{
if( (y >= 0 && y < yMAX) && (x > 0 && x < xMAX)) //boundary check
{
if( maze[y][x] == '+' )
return ; // returns to the function and iterates again
if( maze[y][x] == ' ') // if the destination is reached
{
maze[y][x]='@'; // marks current cell as @ if detected a blank space
// Calling the function itself to change and detect position in the maze.

path(y, x+1); //Testing rightway
path(y-1, x); // (Testing down)
path(y, x-1); //Testing left way
path(y+1, x); // Testing up
// I guess I have to backtrack here but my code didn't match up ... maze[y][x]= ‘ ’;???
}
}
}

int main()
{
// this initialization could also be done in the beginning of the program
// but its better to define it locally in the main function rather than globally
// initializes the default value for x and y
//char maze[yMAX][xMAX];
int x = 0;
int y = 0;

ifstream fp("maze.txt");
if (! fp) {
cout << "Error, file couldn't be opened" << endl;
return 1;
}
for(int row = 0; row < 20; row++) { // stop loops if nothing to read
for(int column = 0; column < 20; column++){
fp >> maze[row][column];
if ( ! fp ) {
cout << "Error reading file for element " << row << "," << column << endl;
return 1;
}
}
}
//design for display on the screen
cout << "Original Maze :\n";
printMaze(); //prints original 20 by 20 maze
cout << "\n\n Maze after solution:\n\n"; //makes more space for the sokution maze
path(0,1); //starting point of the maze solution (i.e. @ starts at this point)
printMaze(); //prints the solved maze
return 0; //returns integer value 0
}
Use code tags. Your post is almost unreadable without.
Topic archived. No new replies allowed.