944,111 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1251
  • C++ RSS
Sep 29th, 2009
0

Help With this program

Expand Post »
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;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xxunknown321 is offline Offline
24 posts
since Sep 2009
Sep 29th, 2009
1

Re: Help With this program

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.
Reputation Points: 164
Solved Threads: 98
Practically a Master Poster
sfuo is offline Offline
642 posts
since Jul 2009
Sep 29th, 2009
1

Re: Help With this program

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: initialise value in linklist
Next Thread in C++ Forum Timeline: list iterator not dereferencable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC