0

Ok, to the point as suggested. Second semester CS assignment to write program that reads maze info in from txt file to create maze a solves the maze recusively. I've got a dynamically allocated 2D array wtih each cell containing number that detemines the walls by bit values and it prints ok. To solve the maze, I create a second maze to store 'O' and 'X'. (Didn't post my getMazedata function as it works ok.

I've been over old maze post but am still having trouble. I'd appreciate any help on the following: 1) Can't figure out why my takeStep function doesn't work. 2) How do I merge and print the maze with the wall info with the maze that has my path info? 3) How do I backtrack from a deadend?

int main(int argc, char** argv)
   {   	
      FILE* dataptr;
   	
      msT* ms = (msT*)malloc (sizeof(msT));
   	
      char** mspth = (char**)malloc (sizeof (char**));
   	   	
      dataptr = fopen (argv [1], "r");
      if (!dataptr)
      {
         printf("Could not open file.\n\n");
         return EXIT_FAILURE;
      }
   		 	
      ms = getMazedata (dataptr, ms);
   	
      solveMaze (ms, mspth);
    	  
      return 0;
   }


void solveMaze (msT* ms, char** mspth)
   {
      int i;
      int pp_row = ms->xs;
      int pp_col = ms->ys; 
   
      mspth = malloc(sizeof(char*) * ms->N);
      for (i=0; i < ms->N; i++)
      {
         mspth[i] = malloc(sizeof(char) * ms->M);		
      }
   
      mspth->mspth[ms->xs][ms->ys] = 'S';     //Loads start point.
   
      mspth->mspth[ms->xe][ms->ye] = 'F';     //Loads end point.
   
   
      takeStep(ms, mspth, pp_row, pp_col);
   
      return;
   
   }


   void takeStep(msT* ms, char** mspth, int pp_row, int pp_col)
   {
      int get;
   
      if((pp_row != 0) && (ms->maze[pp_row][pp_col] & north != 0) && (mspth [pp_row - 1][pp_col]!= 'X'))
      {
         pp_row --;
      }
      
      else
      {
         if((pp_col != ms->M) && (ms->maze[pp_row][pp_col] & east != 0) && (mspth [pp_row][pp_col + 1]!= 'X'))
         {
            pp_col ++;
         }
         else
         {
            if((pp_row != ms->N) && (ms->maze[pp_row][pp_col] & south != 0) && (mspth [pp_row + 1][pp_col]!= 'X'))
            {
               pp_row ++;
            }
            else
            {
               if((pp_col != 0) && (ms->maze[pp_row][pp_col] & west != 0) && (mspth [pp_row][pp_col - 1]!= 'X'))
               {
                  pp_col --;
               }
            }   
         }     
      }   
   	 
   	 
      printf("print maze here\n\n");  // planed location for print maze function
   	 
      if (mspth[pp_row][pp_col] == "F") printf("Maze Solved\n\n");
      else
      {
         mspth[pp_row][pp_col] = 'O';          
      }
   		
      get = getc(stdin);
   	 
      takeStep(ms, mspth, pp_row, pp_col);
   	 
   }
2
Contributors
2
Replies
6
Views
6 Years
Discussion Span
Last Post by FloridaJoe55
0

I think you have to maintain a stack to keep track of moves and pop it repeatedly when you have to backtrack. I worked on a similar problem and i did the same thing.

Happy coding!!

0

Thanks Anirudh Rb for the input. Being quite new to C programming, I have limited tools at my disposal right now. Ended up getting the project done by brut force. Wasn't elegant, wasn't pretty, but went through the demo with one minor glich.

Cheers

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.