Ok so i wrote this program, and it finds the exit of the maze, but it has alot of if statements, I would like to know if there is a more compact/efficient way of doing this.
Code:
#include<iostream>
using namespace std;
void printMaze(char a[12][12])
{
for(int i=0;i<12;i++)
{
cout<<" ";
for(int j=0;j<12;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl<<endl;
}
}
void mazeTraverse(char a[12][12], int i, int j)
{
cout<<endl;
printMaze(a);
int xStart = 2; // starting X and Y coordinates for maze
int yStart = 0;
int x = xStart; // current X and Y coordinates
int y = yStart;
mazeTraversal( maze, xStart, yStart, x, y, RIGHT );
return 0;
} // end main
// Assume that there is exactly 1 entrance and exactly 1 exit to the maze.
void mazeTraversal( char maze[][ 12 ], const int xStart, const int yStart,
int xCoord, int yCoord, int direction )
{
static bool flag = false;
if ( coordsAreEdge( xCoord, yCoord ) && xCoord != xStart &&
yCoord != yStart )
{
cout << "\nMaze successfully exited!\n\n";
return; // maze is complete
} // end if
else if ( xCoord == xStart && yCoord == yStart && flag )
{
cout << "\nArrived back at the starting location.\n\n";
return;
} // end else if
else
{
flag = true;
// for loop uses switch to determine appropriate move
for ( int move = direction, count = 0; count < 4; count++,
move++, move %= 4 )
{
switch( move )
{
case DOWN: // move down
if ( validMove( maze, xCoord + 1, yCoord ) )
{
mazeTraversal(
maze, xStart, yStart, xCoord + 1, yCoord, LEFT );
return;
} // end if
break;
case RIGHT: // move right
if ( validMove( maze, xCoord, yCoord + 1 ) )
{
mazeTraversal(
maze, xStart, yStart, xCoord, yCoord + 1, DOWN );
return;
} // end if
break;
case UP: // move up
if ( validMove( maze, xCoord - 1, yCoord ) )
{
mazeTraversal(
maze, xStart, yStart, xCoord - 1, yCoord, RIGHT );
return;
} // end if
break;
case LEFT: // move left
if ( validMove( maze, xCoord, yCoord - 1 ) )
{
mazeTraversal(
maze, xStart, yStart, xCoord, yCoord - 1, UP );
return;
} // end if
break;
} // end switch
} // end for
} // end else
} // end function mazeTraversal
// validate move
bool validMove( const char maze[][ 12 ], int r, int c )
{
return
( r >= 0 && r <= 11 && c >= 0 && c <= 11 && maze[ r ][ c ] != '#' );
} // end function validate
// function to check coordinates
bool coordsAreEdge( int x, int y )
{
if ( ( x == 0 || x == 11 ) && ( y >= 0 && y <= 11 ) )
return true;
else if ( ( y == 0 || y == 11 ) && ( x >= 0 && x <= 11 ) )
return true;
else
return false;
} // end function coordsAreEdge
// print the current state of the maze
void printMaze( const char maze[][ 12 ] )
{
// nested for loops to iterate through maze
for ( int x = 0; x < 12; x++ )
{
for ( int y = 0; y < 12; y++ )
cout << maze[ x ][ y ] << ' ';
cout << '\n';
} // end for
cout << "\nHit return to see next move\n";
cin.get();
} // end function printMaze