Question: Q) Rewrite this code. #include /* This function is an easy way to print the maze. Takes the maze * as an argument and prints
Q) Rewrite this code.
#include
/* This function is an easy way to print the maze. Takes the maze
* as an argument and prints it out. No return value.
* */
void print_maze(char maze[12][12]) {
for (int i = 0; i < 12; ++i) {
for (int j = 0; j < 12; ++j) {
printf("%c ", maze[i][j]);
}
putchar(' ');
}
putchar(' ');
}
/* This is the recursive maze traversal function. It takes the maze, a
* beginning cooordinate in the maze, and what direction we are facing at the
* current point in the maze based on our moves, as arguments It first
* evaluates if we are at the ending point, or next to it. Afterward, it
* figures out which way we are facing at the current point in the maze and
* decides how to move based on that, then feeds that info back to itself.
* No return value.
*/
void mazeTraverse(char maze[12][12], int row, int col, char* facing) {
// For clarity of statemnts, and typing sanity, we'll define directions
char north = maze[row - 1][col];
char south = maze[row + 1][col];
char east = maze[row][col + 1];
char west = maze[row][col - 1];
char north_west = maze[row - 1][col - 1];
char north_east = maze[row - 1][col + 1];
char south_west = maze[row + 1][col - 1];
char south_east = maze[row + 1][col + 1];
// If at the exit
if (col == 11) {
printf("Success! ");
}
// If the exit is right next to the current position
else if (col + 1 == 11 && east == '.') {
maze[row][col + 1] = 'x';
*facing = '>';
print_maze(maze);
mazeTraverse(maze, row, (col + 1), facing);
}
// Otherwise, figure out which way we are facing and move accordingly
else if (*facing == '>') {
// Move south
if (
(south != '#' && south_west == '#' && (north != '.' || north != 'x')) ||
(south != '#' && south_west != '#' && south_east != '#')
) {
maze[row + 1][col] = 'x';
*facing = 'v';
print_maze(maze);
mazeTraverse(maze, (row + 1), col, facing);
}
// Move east
else if (
(east != '#' && south_east == '#') ||
(east != '#' && south_east != '#') ||
(east != '#' && south_east != '#' && north_east != '#')
) {
maze[row][col + 1] = 'x';
*facing = '>';
print_maze(maze);
mazeTraverse(maze, row, (col + 1), facing);
}
// Move north
else if (
(north != '#' && north_east == '#') ||
(north != '#' && north_east != '#') ||
(north != '#' && north_east == '#' && north_west != '#')
) {
maze[row - 1][col] = 'x';
*facing = '^';
print_maze(maze);
mazeTraverse(maze, (row - 1), col, facing);
}
// Move west
else if (west != '#' ) {
maze[row][col - 1] = 'x';
*facing = '<';
print_maze(maze);
mazeTraverse(maze, row, (col - 1), facing);
}
}
else if (*facing == 'v') {
// Move south
if (
(south != '#' && south_west == '#' && (north != '.' || north != 'x')) ||
(south != '#' && south_west != '#' && south_east != '#') ||
(south != '#' && south_west != '#' && south_east == '#')
) {
maze[row + 1][col] = 'x';
*facing = 'v';
print_maze(maze);
mazeTraverse(maze, (row + 1), col, facing);
}
// Move west
else if (west != '#' ) {
maze[row][col - 1] = 'x';
*facing = '<';
print_maze(maze);
mazeTraverse(maze, row, (col - 1), facing);
}
// Move east
else if (
(east != '#' && south_east == '#') ||
(east != '#' && south_east != '#') ||
(east != '#' && south_east != '#' && north_east != '#')
) {
maze[row][col + 1] = 'x';
*facing = '>';
print_maze(maze);
mazeTraverse(maze, row, (col + 1), facing);
}
// Move north
else if (
(north != '#' && north_east == '#') ||
(north != '#' && north_east != '#') ||
(north != '#' && north_east == '#' && north_west != '#')
) {
maze[row - 1][col] = 'x';
*facing = '^';
print_maze(maze);
mazeTraverse(maze, (row - 1), col, facing);
}
}
else if (*facing == '^') {
// Move east
if (
(east != '#' && south_east == '#') ||
(east != '#' && south_east != '#') ||
(east != '#' && south_east != '#' && north_east != '#')
) {
maze[row][col + 1] = 'x';
*facing = '>';
print_maze(maze);
mazeTraverse(maze, row, (col + 1), facing);
}
// Move north
else if (
(north != '#' && north_east == '#') ||
(north != '#' && north_east != '#') ||
(north != '#' && north_east == '#' && north_west != '#')
) {
maze[row - 1][col] = 'x';
*facing = '^';
print_maze(maze);
mazeTraverse(maze, (row - 1), col, facing);
}
// Move west
else if (west != '#' ) {
maze[row][col - 1] = 'x';
*facing = '<';
print_maze(maze);
mazeTraverse(maze, row, (col - 1), facing);
}
// Move south
else if (
(south != '#' && south_west == '#' && (north != '.' || north != 'x')) ||
(south != '#' && south_west != '#' && south_east != '#')
) {
maze[row + 1][col] = 'x';
*facing = 'v';
print_maze(maze);
mazeTraverse(maze, (row + 1), col, facing);
}
}
else if (*facing == '<') {
// Move west
if (
(west != '#' && north != '.')
) {
maze[row][col - 1] = 'x';
*facing = '<';
print_maze(maze);
mazeTraverse(maze, row, (col - 1), facing);
}
// Move north
else if (
(north != '#' && north_east == '#') ||
(north != '#' && north_east != '#') ||
(north != '#' && north_east == '#' && north_west != '#')
) {
maze[row - 1][col] = 'x';
*facing = '^';
print_maze(maze);
mazeTraverse(maze, (row - 1), col, facing);
}
// Move east
else if (
(east != '#' && south_east == '#') ||
(east != '#' && south_east != '#') ||
(east != '#' && south_east != '#' && north_east != '#')
) {
maze[row][col + 1] = 'x';
*facing = '>';
print_maze(maze);
mazeTraverse(maze, row, (col + 1), facing);
}
// Move south
else if (
(south != '#' && south_west == '#' && (north != '.' || north != 'x')) ||
(south != '#' && south_west != '#' && south_east != '#')
) {
maze[row + 1][col] = 'x';
*facing = 'v';
print_maze(maze);
mazeTraverse(maze, (row + 1), col, facing);
}
}
}
int main(void) {
char maze[12][12] = {'#','#','#','#','#','#','#','#','#','#','#','#',
'#','.','.','.','#','.','.','.','.','.','.','#',
'.','.','#','.','#','.','#','#','#','#','.','#',
'#','#','#','.','#','.','.','.','.','#','.','#',
'#','.','.','.','.','#','#','#','.','#','.','.',
'#','#','#','#','.','#','.','#','.','#','.','#',
'#','.','.','#','.','#','.','#','.','#','.','#',
'#','#','.','#','.','#','.','#','.','#','.','#',
'#','.','.','.','.','.','.','.','.','#','.','#',
'#','#','#','#','#','#','.','#','#','#','.','#',
'#','.','.','.','.','.','.','#','.','.','.','#',
'#','#','#','#','#','#','#','#','#','#','#','#'};
int row;
int col = 0;
/* Decide with direciton we're facing. This will be '^', '<',
* '>', or 'v', so as not to conflict with the cardinal
* directions. We'll initialize it to '>' since we know
* the maze starts out facing east.
*/
char facing = '>';
// Find starting point
for (int i = 0; i < 12; ++i) {
if (maze[i][0] == '.') {
row = i;
break;
}
}
// Put an 'x' in the starting point
maze[row][col] = 'x';
// Run the maze traversal
mazeTraverse(maze, row, col, &facing);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
