My teacher wrote this code.
i am having trouble understanding what is going on in the code.
can someone please help me by commenting in the program
if you can tell me the importance of the functions and how everything works.
please help me.... i'm a noob programmer

#include <iostream>
using namespace std;

int col = 0;
int row = 1;
const int ROWMAX = 12;
const int COLMAX = 12;

char maze[ROWMAX][COLMAX] =
{
{'#','#','#','#','#','#','#','#','#','#','#','#'},
{'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
{' ',' ','#',' ','#',' ','#','#','#','#',' ','#'},
{'#','#','#',' ','#',' ',' ',' ',' ','#',' ','#'},
{'#',' ',' ',' ',' ','#','#','#',' ','#',' ',' '},
{'#','#','#','#',' ','#',' ','#',' ','#',' ','#'},
{'#',' ',' ','#',' ','#',' ','#',' ','#',' ','#'},
{'#','#',' ','#',' ','#',' ','#',' ','#',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#'},
{'#','#','#','#','#','#',' ','#','#','#',' ','#'},
{'#',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#'}
};
void printMaze();
void mazeTraverse(int, int);


void printMaze()
{
for(int row = 0; row < ROWMAX; row++)
{
for(int col=0; col < COLMAX; col++)
cout << maze[row][col];
cout << "\n";
}
}

void mazeTraverse(int row, int col)
{
if( (row>0 && row<ROWMAX) && (col>=0 && col<COLMAX)) {


if( maze[row][col] == ' ') {
maze[row][col]='*';

mazeTraverse(row, col+1);
mazeTraverse(row, col-1);
mazeTraverse(row-1, col);
mazeTraverse(row+1, col);
}
}
}

int main()
{
cout << "Maze before solution:\n";
printMaze();
cout << "Maze after solution:\n";
mazeTraverse(1, 2);
printMaze();
return 0;
}

The function mazeTraverse() is pretty much this whole program. It checks to see if the position in the maze that is it at is a space and then if it is it runs its self 4 items (up one spot, down one, left one, right one) and doing the same checks as before and if those spots are spaces then it runs its self another 4 times. This happens until it comes to a dead end.

Not sure if that helps it took me a few secs to look at the function to figure out what it was doing.

commented: thank you +0

Declare a maze that will consist a 12 by 12 board represented internally by a 2D char array. The characters in the array will be # to indicate walls, space char to indicate available choices (unvisited cells) and * to indicate that we've been here before and can't go there again from another cell (though we will go back to each cell we've visited that initially contained a space until we've looked in all four directions from that cell). There will be a solid wall around the maze except for two possible exits. You will start at a predetermined cell given by the program.

The program will look at the maze using a recursive function of type void taking current row and column indexes as parameters. Each call to the funciton will look at a single cell. If the cell is within the maze and if the cell currently contains a space char then the function will place an asterix in the current cell and will then use a protocol of evaluating neighbors of the current cell by looking up before down before left before right.

The output of the program will be a picture of the maze before the recursive function is called and a picture of the maze after the recursive function has looked at all the cells it can.

In the picture of the maze after the recursive function has ended if there is an asterix in one or both of the cells in the external wall of the maze then the maze is solvable; if not, then it's not.

Note that the program is not designed to give a specific solution, or the shortest solution, if there is one. It will just determine if one is possible.

Also note that the program will not stop once a solution is found.

A recursive function is one that "calls itself". It may create a never ending loop so a gaurantee that it will stop must be built in to prevent the program from locking up.

commented: thank you very much +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.