Maze Traverse Help!

brich744
brich744 used Ask the Experts™
on
I am writing a maze program and I cannot get the directional button to work.  The program terminates before I can even use the directional button.

#include<iostream>

using namespace std;



bool isEdge(int x, int y);
bool validMove(const char maze[][12], int x, int y);
void mazeSolver(char maze[][12],int xstart, int ystart, int x, int y, int direction);
void printMaze(char maze[][12]);
void getTestCell(int x, int y, int& newx, int& newy, int direction);
int main()
{
	char maze[12][12]= //Draw maze
	{{'#','#','#','#','#','#','#','#','#','#','#','#'},
	 {'#','#','#','#','#','#','#','#','#','#','#','#'},		
	 {' ',' ',' ',' ',' ','#','#','#','#',' ',' ',' '},
	 {'#',' ',' ','#',' ','#','#','#',' ',' ','#','#'},
	 {'#','#','#','#',' ',' ','#','#',' ','#','#','#'},
	 {'#','#','#','#','#',' ','#','#',' ','#','#','#'},
	 {'#','#','#','#','#',' ','#','#',' ','#','#','#'},
	 {'#','#','#','#','#',' ','#','#',' ','#','#','#'},
	 {'#','#','#','#','#',' ',' ',' ',' ','#','#','#'},
	 {'#','#','#','#','#','#','#','#','#','#','#','#'},
	 {'#','#','#','#','#','#','#','#','#','#','#','#'},
	 {'#','#','#','#','#','#','#','#','#','#','#','#'}};

	int xstart = 2;
	int ystart = 0;
	int x = xstart;
	int y= ystart;

	mazeSolver(maze,xstart, ystart, x,y,1);
	return 0;
}

bool validMove(const char maze[][12], int x, int y)
{
	return (maze[x][y]!='#'&&x>=0 && x<12&&y>=0&&y<12);
}

bool isEdge(int x, int y)
{
	if( x==0||x==11&&(y>=0||y<12))//top and bottom edge also taking out && in the y
		return true;
	else if(y==0||y==11&&(x>=0||x<12))//left and right edge also taking out && in the x
		return true;
	else
		return false;
}

void getTestCell(int x, int y, int& testx, int& testy, int direction)
{
	//0:up:1 right:2 down:3 left

	switch(direction)
	{
	case 0: //up
		testx = x-1;
		testy = y;
		break;
	case 1://right
		testx = x;
		testy = y+1;
		break;
	case 2: //down
		testx = x+1;
		testy = y-1;
		break;
	case 3: //left
		testx =x;
		testy = y-1;
		break;
	}}
void mazeSolver(char maze[][12], int xstart, int ystart, int x, int y, int direction)
{
	static bool flag = false;
	//bool edge = isEdge(x,y);
	maze[x][y] = '*';
	printMaze(maze);//shows the progress on the programming test every once an a while
	// base case 1: get back to the starting point
	if(x==xstart&&y==ystart&&flag)
	{
		cout<<"You are back to where you started.\n";
		return;
	}

	
	else if(isEdge(x,y))
	{
		flag = true;
		for(int move= direction, count=0; count<4; count++, move=(move+3)%4)//exploring counter clockwise
		{
			int testx, testy;
			getTestCell(x,y,testx,testy, move);
			if(validMove(maze,testx,testy))
			{
				//recursively explore the maze
				mazeSolver(maze, xstart, ystart,testx,testy,(move+1)%4);//Remind Dr.Milligede to make correction
				//mazeSolver about mazeSolver changing the signature from x and y to testx and testy;
				return;
			}
		}
	}
// base case 2: out of the maze
	else
	{
		cout<<"Congrats"<<endl;
		return;
	}
}
void printMaze(char maze[][12])
{
   cout << endl;//this is the printmaze from the other program
	int j;
    for(int i = 0; i < 12; ++i)
	{
		cout<<"	"<<"	"<<endl;
    for(j = 0; j < 12; ++j)
    {
      cout<<maze[i][j];  
}
	}}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Author of the Year 2009

Commented:
To what button do you refer?
I can't see any code in there that accepts user input -- keyboard, button, or otherwise.
This code is full of bugs. I tell you a few to get you started but I won't debug it completely. You use a flag variable, but you never change its value.

if(x==xstart && y==ystart && flag)
{
         cout<<"You are back to where you started.\n";
         flag = false;
         return;
}

The isEdge() returns true right on the starting point. Changing like this:
else if(! isEdge(x,y) || x==xstart && y==ystart)
gets the solving started, even tho it doesn't solve it completely.
I was wrong, that flag usage is good. But here's something else:

bool validMove(const char maze[][12], int x, int y)
{
      return (maze[x][y]!='#' && maze[x][y]!='*' && x>=0 && x<12&&y>=0&&y<12);
}

So you won't go in circles. Like this you won't go where you already were. Another bug:
case 2: //down
        testx = x+1;
        testy = y;
        break;

The original code had testy = y-1. An a**kick from copy-paste. Like this it's completely debugged, works fine.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial