1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
void printMaze(const char maze[][12], int xLoc, int yLoc);
int mazeTraverse(char maze[][12], int xLoc, int yLoc, int facing);
int main()
{
char maze[ 12 ][ 12 ] =
{ {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{ '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#'},
{ '.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#'},
{ '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#'},
{ '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.'},
{ '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
{ '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
{ '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
{ '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#'},
{ '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#'},
{ '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#'},
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'} };
int success = 0;
success = mazeTraverse(maze, 2, 0, 1); // Call function to move through the maze, assign returned value to success.
if (success == 1) // If success is equal to 1...
cout << "Congratulations! The maze has been solved.\n"; // ...output congratulations...
else // ...else...
cout << "Unfortunately the maze has not been solved correctly.\n"; // ...output failed message.
return 0; // End program.
}
// print the current state of the maze
void printMaze( const char maze[][ 12 ], int xLoc, int yLoc)
{
// nested for loops to iterate through maze
for ( int x = 0; x < 12; ++x )
{
for ( int y = 0; y < 12; ++y )
if ((x == xLoc) && (y == yLoc))
cout << 'X' << ' ';
else
cout << maze[ x ][ y ] << ' ';
cout << '\n';
} // end for
cout << "\nHit return to see next move\n";
cin.get();
} // end function printMaze
// Traverse through the maze one square at a time
int mazeTraverse(char maze[][12], int xLoc, int yLoc, int facing)
{
int success = 0;
maze[xLoc][yLoc] = '*'; // Mark current location in the maze.
printMaze(maze, xLoc, yLoc); // Call function to display maze with current location marked.
while (success == 0) // While success is not equal to 0...
{
if ((xLoc == 4) && (yLoc == 11)) // If current location is the exit of the maze...
{
success = 1; // ...set success to 1.
}
else if (facing == 0) // Else if facing up...
{
if (maze[xLoc][yLoc+1] == '.') // ...check square to the right for valid move...
{
success = mazeTraverse(maze, xLoc, yLoc+1, 1); // Move to the right.
}
else if (maze[xLoc-1][yLoc] == '.') // ...check square above for valid move...
{
success = mazeTraverse(maze, xLoc-1, yLoc, 0); // Move up.
}
else if (maze[xLoc][yLoc-1] == '.') // ...check square to the left for valid move...
{
success = mazeTraverse(maze, xLoc, yLoc-1, 3); // Move to the left.
}
else // If nowhere to go...
return 0; // ...close recursion to the previous junction.
}
else if (facing == 1) // If facing right...
{
if (maze[xLoc+1][yLoc] == '.') // ...check square below for valid move...
{
success = mazeTraverse(maze, xLoc+1, yLoc, 2); // Move down.
}
else if (maze[xLoc][yLoc+1] == '.') // ...check square to the right for valid move...
{
success = mazeTraverse(maze, xLoc, yLoc+1, 1); // Move right.
}
else if (maze[xLoc-1][yLoc] == '.') // ...check square above for valid move...
{
success = mazeTraverse(maze, xLoc-1, yLoc, 0); // Move up.
}
else // If nowhere to go...
return 0; // ...close recursion to the previous junction.
}
else if (facing == 2) // If facing down...
{
if (maze[xLoc][yLoc-1] == '.') // ...check square to the left for valid move...
{
success = mazeTraverse(maze, xLoc, yLoc-1, 3); // Move to the left.
}
else if (maze[xLoc+1][yLoc] == '.') // ...check square below for valid move...
{
success = mazeTraverse(maze, xLoc+1, yLoc, 2); // Move down.
}
else if (maze[xLoc][yLoc+1] == '.') // ...check square to the right for valid move...
{
success = mazeTraverse(maze, xLoc, yLoc+1, 1); // Move to the right.
}
else // If nowhere to go...
return 0; // ...close recursion to the previous junction.
}
else if (facing == 3) // If facing left...
{
if (maze[xLoc-1][yLoc] == '.') // ...check square above for valid move...
{
success = mazeTraverse(maze, xLoc-1, yLoc, 0); // Move up.
}
else if (maze[xLoc][yLoc-1] == '.') // ...check square to the left for valid move...
{
success = mazeTraverse(maze, xLoc, yLoc-1, 3); // Move to the left.
}
else if (maze[xLoc+1][yLoc] == '.') // ...check square below for valid move...
{
success = mazeTraverse(maze, xLoc+1, yLoc, 2); // Move down.
}
else // If nowhere to go...
return 0; // ...close recursion to the previous junction.
}
} // ...end while loop.
return success; // Return value of success.
}
|