Thread: Maze traversal in c

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well here is your code, nicely formatted.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    int main()
    {
      // all variables declared
      int rows, columns, r, c, i, rowStart, columnStart, rowEnd, columnEnd, currentX, currentY;
      char **mazeblock;
      char orientation;
      FILE *fPointer;               // file pointer
    
      //read file into array
      fPointer = fopen("maze01.txt", "r");
      if (fPointer == NULL) {
        printf("Error in opening file\n");
        exit(1);                    // help check opening error
      }
      fscanf(fPointer, " %d %d %d %d %d %d \n", &rows, &columns, &rowStart, &columnStart, &rowEnd, &columnEnd);
    
      // Dynamically allocate memory
      mazeblock = (char **) malloc(sizeof(char *) * rows);
    
      // failure to allocate memory
      if (!mazeblock) {
        printf("memmory allocation failed");
      }
    
      for (i = 0; i < rows; i++) {
        mazeblock[i] = (char *) malloc(sizeof(char) * columns);
      }
    
      // to read in the maze
      for (r = 0; r < rows; r++) {
        for (c = 0; c < columns; c++) {
          fscanf(fPointer, "%c", &mazeblock[r][c]);
        }
      }
      fclose(fPointer);
    
      // Defining direction
      if (rowStart == 0) {
        orientation = 'S';
      } else if (rowStart == (rows - 1)) {
        orientation = 'N';
      } else if (columnStart == 0) {
        orientation = 'E';
      } else {
        orientation = 'W';
      }
    
      // to hold direction
      currentX = rowStart;
      currentY = columnStart;
    
      //
      // for going south rules
      while ((currentX != rowEnd) || (currentY != columnEnd)) {
        if (orientation == 'S') {
          if ((currentY > 0) && (mazeblock[currentX][currentY - 1] == ' ' || mazeblock[currentX][currentY - 1] == '#')) {
            mazeblock[currentX][currentY] = '#';
            --currentY;
            orientation = 'W';
          } else if ((currentX < (rows - 1))
                     && (mazeblock[currentX + 1][currentY] == ' ' || mazeblock[currentX + 1][currentY] == '#')) {
            mazeblock[currentX][currentY] = '#';
            ++currentX;
            orientation = 'S';
          } else if ((currentY < (columns - 1))
                     && (mazeblock[currentX][currentY + 1] == ' ' || mazeblock[currentX][currentY + 1] == '#')) {
            mazeblock[currentX][currentY] = '#';
            ++currentY;
            orientation = 'E';
          } else {
            mazeblock[currentX][currentY] = '#';
            --currentX;
            orientation = 'N';
          }
        }
        //for going North
        else if (orientation == 'N') {
          if ((currentY < (columns - 1))
              && (mazeblock[currentX][currentY + 1] == ' ' || mazeblock[currentX][currentY + 1] == '#')) {
            mazeblock[currentX][currentY] = '#';
            ++currentY;
            orientation = 'E';
          } else if ((currentX > 0)
                     && (mazeblock[currentX - 1][currentY] == ' ' || mazeblock[currentX - 1][currentY] == '#')) {
            mazeblock[currentX][currentY] = '#';
            --currentX;
            orientation = 'N';
          } else if ((currentY > 0)
                     && (mazeblock[currentX][currentY - 1] == ' ' || mazeblock[currentX][currentY - 1] == '#')) {
            mazeblock[currentX][currentY] = '#';
            --currentY;
            orientation = 'W';
          } else {
            mazeblock[currentX][currentY] = '#';
            currentX++;
            orientation = 'S';
          }
        }
        //for going West
        else if (orientation == 'W') {
          if ((currentX > 0) && (mazeblock[currentX - 1][currentY] == ' ' || mazeblock[currentX - 1][currentY] == '#')) {
            mazeblock[currentX][currentY] = '#';
            --currentX;
            orientation = 'N';
          } else if ((currentY > 0)
                     && (mazeblock[currentX][currentY - 1] == ' ' || mazeblock[currentX][currentY - 1] == '#')) {
            mazeblock[currentX][currentY] = '#';
            --currentY;
            orientation = 'W';
          } else if ((currentX < (rows - 1))
                     && (mazeblock[currentX + 1][currentY] == ' ' || mazeblock[currentX + 1][currentY] == '#')) {
            mazeblock[currentX][currentY] = '#';
            ++currentX;
            orientation = 'S';
          } else {
            mazeblock[currentX][currentY] = '#';
            currentY++;
            orientation = 'E';
          }
        }
        //For going east
        else {
          if ((currentX < (rows - 1))
              && (mazeblock[currentX + 1][currentY] == '0' || mazeblock[currentX + 1][currentY] == 'W')) {
            mazeblock[currentX][currentY] = 'W';
            ++currentX;
            orientation = 'S';
          } else if ((currentY < (columns - 1))
                     && (mazeblock[currentX][currentY + 1] == ' ' || mazeblock[currentX][currentY + 1] == '#')) {
            mazeblock[currentX][currentY] = '#';
            ++currentY;
            orientation = 'E';
          } else if ((currentX > 0)
                     && (mazeblock[currentX - 1][currentY] == ' ' || mazeblock[currentX - 1][currentY] == '#')) {
            mazeblock[currentX][currentY] = '#';
            --currentX;
            orientation = 'N';
          } else {
            mazeblock[currentX][currentY] = '#';
            currentY--;
            orientation = 'W';
          }
        }
      }
    
      mazeblock[currentX][currentY] = '# ';
      printf("\n");
    
      // output loop
      for (r = 0; r < rows; r++) {
        for (c = 0; c < columns; c++) {
          printf("%1c", mazeblock[r][c]);
        }
      }
    
      return 0;
    }
    The first problem is you don't even read in the maze properly.

    > fscanf(fPointer, "%c", &mazeblock[r][c]);
    This will fill your maze with \n characters from the input file.

    > mazeblock[currentX][currentY] = '#';
    Why are you filling in where you've been with the wall character?
    If you reach a dead end, how are you going to get back to a place where you had a choice?

    Add some constants to make the code more readable.
    Code:
    #define WALL '#'
    #define FLOOR ' '
    #define CRUMB '~' // mark where you've been
    #define CHOICE '+' // a point in the maze where there was a choice of next move
    So you can say things like
    Code:
    if ( maze[x][y] == FLOOR )
        maze[x][y] = CRUMB;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #2
    Registered User
    Join Date
    Mar 2019
    Posts
    15
    Thank you for the advice. we had not learned about constants yet so I was not sure if my professor would expect me to use it. I replace it(the wall) with a different sign(marker) or use a constant. And I will try to figure out back tracking when I hit a wall. And about reading in the maze. When I ran it IT looked fine to me so I didn't know that there was a problem with that. Thank you again for the advice.

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    15
    Is my reading in a syntax error? I looked at the fscanf in the loop but can't seem to figure out what I did wrong ? I made some changes to my code but I did not use constant. Added comments for what a wall and a space is. I'm not done making all the changes you said and I'm sorry.

    Code:
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    
    
    
    void print_maze(char** mazeblock, int r, int c, int columns, int rows );
    
    
     // void freeMem(char** mazeblock, int rows, int r,int c);
    int main()
    {
    
    
    
    
         // all variables declared
         int rows  , columns , r , c,i,j, rowStart, columnStart, rowEnd, columnEnd, currentX, currentY;
    
    
         char** mazeblock;
         char orientation;
    
    
         FILE *fPointer;// file pointer
    
    
         //read file into array
    
    
    
    
         fPointer = fopen("maze03.txt", "r");
         if(fPointer==NULL)
         {
            printf("Error in opening file\n");
            exit(1); // help check opening error
         }
    
    
    
    
        fscanf( fPointer, " %d %d %d %d %d %d\n ", &rows, &columns, &rowStart, &columnStart, &rowEnd, &columnEnd );// read the first line
    
    
    
    
    
    
    // Dynamically allocate memory
    
    
        mazeblock = (char**)malloc(sizeof(char*) * rows);
    
    
    // failure to allocate memory
        if(!mazeblock)
        {
         printf("memmory allocation failed");
        }
    
    
    
    
        for ( i= 0; i < rows; i++)
        {
            mazeblock[i] = (char*)malloc(sizeof(char) * columns);
        }
    
    
        // to read in the maze
        for ( r = 0; r < rows; r++)
        {
            for ( c = 0; c < columns; c++)
            {
                fscanf(fPointer,"%c",&mazeblock[r][c]);
    
    
            }
        }
    
    
    
    
        fclose(fPointer);
    
    
    
    
    
    
    
    
    
    
    
    
           // Defining direction
        if (rowStart  == 0)
        {
            orientation = 'S';
        }
        else if (rowStart == (rows - 1)){
            orientation = 'N';
        }
        else if (columnStart == 0){
            orientation = 'E';
        }
        else
        {
            orientation = 'W';
        }
    
    
    
    
    // to hold direction
        currentX = rowStart;
        currentY = columnStart;
    
    
    
    
    //
    
    
    
    
    // for going south rules
    //  mazeblock[currentX][currentY ]  == ' ' (IS A FLOOR)
     // mazeblock[currentX][currentY ]  == '+' (IS A MARKER)
        while ( !(currentX == rowEnd) && (currentY == columnEnd)){
            if ( orientation == 'S'){
                if ( (currentY > 0) && (mazeblock[currentX][currentY - 1]  == ' ' || mazeblock[currentX][currentY - 1]  == '+')){
                    mazeblock[currentX][currentY] = '+';
                    --currentY;
                    orientation = 'W';
                }
                else if ( (currentX < (rows - 1)) && (mazeblock[currentX + 1][currentY] == ' ' || mazeblock[currentX + 1][currentY] == '+')){
                    mazeblock[currentX][currentY] = '+';
                    ++currentX;
                    orientation = 'S';
                }
                else if ( (currentY < (columns - 1)) && (mazeblock[currentX][currentY + 1] == ' ' || mazeblock[currentX][currentY + 1] == '+')){
                    mazeblock[currentX][currentY] = '+';
                    ++currentY;
                    orientation = 'E';
                }
                else {
                    mazeblock[currentX][currentY] = '+';
                    --currentX;
                    orientation = 'N';
                }
            }
    
    
    //for going North
    
    
            else if ( orientation == 'N'){
                if ( (currentY < (columns - 1)) && (mazeblock[currentX][currentY + 1]  == ' ' || mazeblock[currentX][currentY + 1]  == '+')){
                    mazeblock[currentX][currentY] = '+';
                    ++currentY;
                    orientation = 'E';
                }
                else if ( (currentX > 0) && (mazeblock[currentX - 1][currentY] == ' '|| mazeblock[currentX - 1][currentY] == '+')){
                    mazeblock[currentX][currentY] = '+';
                    --currentX;
                    orientation = 'N';
                }
                else if ( (currentY > 0) && (mazeblock[currentX][currentY - 1] == ' ' || mazeblock[currentX][currentY - 1] == '+')){
                    mazeblock[currentX][currentY] = '+';
                    --currentY;
                    orientation = 'W';
                }
                else {
                    mazeblock[currentX][currentY] = '+';
                    currentX++;
                    orientation = 'S';
                }
            }
    
    
    
    
    //for going West
            else if ( orientation == 'W'){
                if ( (currentX > 0) && (mazeblock[currentX - 1][currentY]  == ' ' || mazeblock[currentX - 1][currentY]  == '+')){
                    mazeblock[currentX][currentY] = '+';
                    --currentX;
                    orientation = 'N';
                }
                else if ( (currentY > 0) && (mazeblock[currentX][currentY - 1] == ' ' || mazeblock[currentX][currentY - 1] =='+')){
                    mazeblock[currentX][currentY] = '+';
                    --currentY;
                    orientation = 'W';
                }
                else if ( (currentX < (rows - 1)) && (mazeblock[currentX + 1][currentY] == ' ' || mazeblock[currentX + 1][currentY] =='+')){
                    mazeblock[currentX][currentY] = '+';
                    ++currentX;
                    orientation = 'S';
                }
                else {
                    mazeblock[currentX][currentY] = '#';
                    currentY++;
                    orientation = 'E';
                }
            }
    
    
    
    
    //For going east
            else {
                if ( (currentX < (rows - 1)) && (mazeblock[currentX + 1][currentY]  == ' ' || mazeblock[currentX + 1][currentY]  =='+')){
                    mazeblock[currentX][currentY] = '+';
                    ++currentX;
                    orientation = 'S';
                }
                else if ( (currentY < (columns - 1)) && (mazeblock[currentX][currentY + 1] == ' ' || mazeblock[currentX][currentY + 1] =='+')){
                    mazeblock[currentX][currentY] = '+';
                    ++currentY;
                    orientation = 'E';
                }
                else if ( (currentX > 0) && (mazeblock[currentX - 1][currentY] == ' ' || mazeblock[currentX - 1][currentY] =='+')){
                    mazeblock[currentX][currentY] = '+';
                    --currentX;
                    orientation = 'N';
                }
                else {
                    mazeblock[currentX][currentY] = '+';
                    currentY--;
                    orientation = 'W';
                }
            }
        }
                mazeblock[currentX][currentY] = '+';
    
    
                printf("\n");
    
    
                printf(" The Coordiantes are row = %d , column = %d , Starting Row = %d, Starting Column = %d ,Ending Row = %d,  Ending Column = %d\n\n", rows, columns, rowStart, columnStart, rowEnd, columnEnd );
    
    
    
    
    
    
                print_maze(mazeblock, r, c,  columns, rows );
    
    
      //  freeMem(mazeblock, rows, r, c);
    
    
       // printf("test");
                printf("\n");
    
    
                if((currentX == rowEnd) && (currentY == columnEnd))
                {
                    printf("The maze has been solved");
                }
    
    
    
    
                return 0;
    
    
    
    
        }
    
    
        void print_maze(char** mazeblock, int r, int c, int columns, int rows )
    {
         for (r = 0; r < rows; r++)
            {
            for ( c = 0; c < columns; c++)
            {
    
    
                printf("%c", mazeblock[r][c]);
    
    
            }
    
    
            }
    }
    
    
      //  void freeMem(char** mazeblock, int rows, int r,int c)
    //{
    
    
        //for (r = 0; r < rows; r++){
    //        free ( mazeblock[r][c]);
        //}
    
    
        //free( mazeblock[r][c]);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 01-08-2011, 01:10 PM
  2. Tree traversal
    By recluse in forum C Programming
    Replies: 4
    Last Post: 12-05-2004, 04:00 PM
  3. Preorder Traversal
    By AmazingRando in forum C Programming
    Replies: 17
    Last Post: 10-29-2003, 03:22 AM
  4. graph traversal
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-10-2002, 11:47 AM
  5. Maze game, complete with maze editor and an example maze!
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-20-2002, 03:27 AM

Tags for this Thread