Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey everyone, I have a programing projest from teacher, but I don\'t understand

ID: 662412 • Letter: H

Question

Hey everyone, I have a programing projest from teacher, but I don't understand what is says.....Anyone can help?

(Maze Traversal) The following grid of #s and dot ( . ) is a double-subscripted array representation of a maze.

# # # # # # # # # # # #

# .   .   .   # .   .   .   .   .   .   #

.   .   # .   # .   # # # # .   #

# # # .   # .   .   .   .   # .   #

# .   .   .   .   # # # .   # .   .

# # # # .   # .   # .   # .   #

# .   .   # .   # . #   .   # .   #

# # .   # .   # . #   .   # .   #

# .   .   .   .   .   .   .   .   # .   #

# # # # # # .   # # # .   #

# .   .   .   .   .   .   # .   .   .   #

# # # # # # # # # # # #

In the preceding double-subscripted array, the #s represent the walls of the maze and the dots represent squares in the possible paths through the maze. Moves can only be made to a location in the array that contain a dot.

         There is a simple algorithm for walking through a maze that guarantees finding the exit (assuming there is an exit). If there is not an exit, you will arrive at the starting location again. Place your right hand on the wall to your right and begin walking forward. Never remove your hand from the wall. If the maze turns to the right, you follow the wall to the right. As long as you do not remove your hand from wall, eventually you will arrive at the exit of the maze. There may be a shorter path than the one you have taken, but you are guaranteed to get out of the maze if you follow the algorithm.

         Write recursive function mazeTraverse to walk through the maze. The function should receive as arguments a 12-by-12 character array representing the maze, and the starting location of the maze. As mazeTraverse attempts to locate the exit from the maze, it should place the character X in each square in the path. The function should display the maze after each move so the user can watch as the maze is solved.

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>

//main-function
int mazeTraverse(int currentRow, int currentColumn );
void printMaze( const int currentRow, const int currentColumn );

                          // 0    1    2    3    4    5    6    7    8    9    10   11
   char maze[ 12 ][ 12 ] = {{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}, // 0
                             {'#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#'}, // 1
                             {'.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#'}, // 2
                             {'#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#'}, // 3
                             {'#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.'}, // 4
                             {'#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#'}, // 5
                             {'#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'}, // 6
                             {'#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'}, // 7
                             {'#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#'}, // 8
                             {'#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#'}, // 9
                             {'#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#'}, // 10
                             {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'} };// 11
char printableMaze[12][12];
int main( void ){
   int startRow = 2, startColumn = 0, state = 0;
   printf("The maze ******** ");
   printMaze(12, 12 );
   //printf(" ");
    int i,j;
    for(i=0;i<12;i++)
       for(j=0;j<12;j++)
           printableMaze[i][j]=maze[i][j];
   state = mazeTraverse(startRow, startColumn );
  
   if(state == 0){
      printf(" Player found the exit! ");      
   }
   return 0;
}

//global
int startingFlag = 1, direction = 0;

//enumeration
enum Stat { OVER, NOT_OVER };
/*core function*/
int mazeTraverse( int currentRow, int currentColumn ){
   //sub-function
   int gameOver( const int currentRow, const int currentColumn );
   int move( int *currentRow, int *currentColumn, int *currentDirection );
  
   enum Stat State;
  
   //check if game is over
   State = gameOver( currentRow, currentColumn );

   if(State == OVER && startingFlag == 0){
      printMaze( currentRow, currentColumn );
      return OVER; //return OVER indicating succesfully find the exit
      //algorithm for if this function can't find the exit is not available    
   }
  
   //indicate player is ready to move
   if(startingFlag == 1){
      //set first direction based on starting position
      if(currentRow == 0){
         direction = 1;           
      }else if(currentRow == 11){
         direction = 0;   
      }else if(currentColumn == 0){
         direction = 2;   
      }else if(currentColumn == 11){
         direction == 3;   
      }
      startingFlag = 0;          
   }
  
   //seek for next move
   move(&currentRow, &currentColumn, &direction );
   mazeTraverse(currentRow, currentColumn );
   return OVER;
}

/*seek for next move*/
enum Direction { NORTH, SOUTH, EAST, WEST };
int move( int *currentRow, int *currentColumn, int *currentDirection ){
   int posibble[ 4 ] = { 0 };// 1 -> North; 2 -> South; 3 -> East; 4 -> West;
   int counter = 0;
  
   enum Direction Seek;
  
   Seek = *currentDirection;
  
   /*move the player respect to current direction*/
  
   //cover the current position
   maze[ *currentRow ][ *currentColumn ] = '.';
  
   //move the player respect to current direction
   if(Seek == NORTH){
      *currentRow -= 1;
   }else if(Seek == SOUTH){
      *currentRow +=1;   
   }else if(Seek == EAST){
      *currentColumn += 1;
   }else if(Seek == WEST){
      *currentColumn -= 1;
   }
    char te;
   //print each move
   printMaze( *currentRow, *currentColumn );// print maze with player current position
   printf("Press enter to continue.");
   scanf("%c",&te);
   /*analyse for next direction*/
  
   //seek posibble direction
   if(maze[ *currentRow - 1 ][ *currentColumn ] == '.' && Seek != SOUTH){
      posibble[ 0 ] = 1;
      counter++;
   }
   if(maze[ *currentRow + 1 ][ *currentColumn ] == '.' && Seek != NORTH){
      posibble[ 1 ] = 1;
      counter++;
   }
   if(maze[ *currentRow ][ *currentColumn + 1 ] == '.' && Seek != WEST){
      posibble[ 2 ] = 1;
      counter++;
   }
   if(maze[ *currentRow ][ *currentColumn - 1 ] == '.' && Seek != EAST){
      posibble[ 3 ] = 1;
      counter++;
   }
  
   //follow right wall
   //Direction { NORTH, SOUTH, EAST, WEST };
   if(counter == 1){
      if(posibble[ 1 ] == 1){//south
         *currentDirection = 1;
      }else if(posibble[ 2 ] == 1){//east
         *currentDirection = 2;
      }else if(posibble[ 0 ] == 1){//north
         *currentDirection = 0;   
      }else if(posibble[ 3 ] == 1){//west
         *currentDirection = 3;
      }
   }else if(counter == 2){
      if(posibble[ 2 ] == 1 && posibble[ 3 ] == 1){// posibble: EAST, WEST
         if(Seek == SOUTH){
            *currentDirection = 3;
         }else if(Seek == NORTH){
            *currentDirection = 2;   
         }             
      }else if(posibble[ 0 ] == 1 && posibble[ 1 ] == 1){// posibble: NORTH,SOUTH
         if(Seek == EAST){
            *currentDirection = 1;     
         }else if(Seek == WEST){
            *currentDirection = 0;   
         }
      }else if(posibble[ 0 ] == 1 && posibble[ 3 ] == 1){// NORTHWEST
            *currentDirection = 0;     
      }else if(posibble[ 0 ] == 1 && posibble[ 2 ] == 1){// NORTHEAST
            *currentDirection = 2;     
      }else if(posibble[ 1 ] == 1 && posibble[ 2 ] == 1){// SOUTHEAST
            *currentDirection = 1;     
      }else if(posibble[ 1 ] == 1 && posibble[ 3 ] == 1){// SOUTHWEST
            *currentDirection = 3;     
      }
   }else if(counter == 3){
      if(Seek == NORTH){
         *currentDirection = 2;     
      }else if(Seek == SOUTH){
         *currentDirection = 3;   
      }else if(Seek == EAST){
         *currentDirection = 1;   
      }else if(Seek == WEST){
         *currentDirection = 0;   
      }
   }else if(counter == 0){
      //dead end
      if(Seek == NORTH){
         *currentDirection = 1;      
      }else if(Seek == SOUTH){
         *currentDirection = 0;   
      }else if(Seek == EAST){
         *currentDirection = 3;   
      }else if(Seek == WEST){
         *currentDirection = 2;   
      }
   }
  
}

/*check if game is over*/
int gameOver( const int currentRow, const int currentColumn ){
   if(currentRow == 0 || currentRow == 11 || currentColumn == 0 || currentColumn == 11 ){
      return OVER;
   }else{
      return NOT_OVER;   
   }
}

/*print current maze*/
void printMaze( const int currentRow, const int currentColumn ){
    int mazeRow, mazeColumn;
   
    printf("     ");
    for(mazeRow = 0; mazeRow < 12; mazeRow++){
      for(mazeColumn = 0; mazeColumn < 12; mazeColumn++){
         if(mazeRow == currentRow && mazeColumn == currentColumn){
            printableMaze[ mazeRow ][ mazeColumn ] = 'X';
         }
         printf("%2c", printableMaze[ mazeRow ][ mazeColumn ] );
        
      }
      printf("     ");
   }
   printf(" ");
}