0

HW help! 1. Make a 50x50 maze. 2. Have it count time and steps. 3. Have a user navigate through the maze. 4. The maze needs to be random, but have 2 entrances, 2 exits, and at least 1 path from any entrance to any exit. 5. Use pointers.

Main problem rn: Trying to make the maze solvable - I've tried a recursive depth-first approach, but it seems to not work and I'm not sure how to fix it (or what is going wrong).

I've been struggling with this assignment all week... I think I've managed to almost complete it - just need help with making a path through the maze (i.e. a path with no X's from the starting position to an exit. I have not tried to use pointers in it yet... thought I would write the code till I could get it to work, then use pointers to make it neater where I can (not comfortable with using them yet). Any advice would be greatly appreciated!

#include <stdio.h>
#include <stdlib.h>  // for the code to refresh screen
#include <time.h>     // for the code to track time 


int x =50 ;  //SIZE rows
int y =50  ;  //SIZE columns

void print(char a[x][y]);   //print the maze
void drawoutline(char b[x][y]);   // draw the outline of the maze
void initial(char b[x][y]);   //initial the maze to all 'X'
void walk(char b[x][y]);      //maze solvin  starting

void recursive(int r, int c, int b[x][y]);  // idea for 'depth-first algorithim' to setting a clear path 



int main(void) // MAIN function start:
{
  srand(time(NULL));
  char b[x][y];

  initial(b);               // draw the maze

  drawoutline(b);      //draw boundary for the maze

  recursive(15,0,b);
  print(b);                 

 walk(b); 
  return 0;

}   // end MAIN


void print(char a[x][y])
{

  system("@cls||clear");//refresh screen

  for(int i=0;i<x;i++)//using loop to print maze characters 
  {
    for(int j=0;j<y;j++)
    {
      printf("%c ",a[i][j]);
    }
    printf("\n");
  }
  printf("\n"); // new line to serpeate maze from text 

} // end print



void drawoutline(char b[x][y]) // function draw maze
{
   for(int i=0;i<y-1;i++)//draw first -
  {
    b[0][i]='-';
  }
  for(int i=0;i<y-1;i++)//draw last -
  {
    b[x-1][i]='-';
  }
    for(int i=0;i<x;i++)//draw left | wall
  {
    b[i][0]='|';
  }
    for(int i=0;i<x;i++)//draw right |
  {
    b[i][y-1]='|';
  }

  // possible to make lines 79-91 neater/with pointers?
  b[14][0] = ' ';     // 2 Start gates  ( 2 entrances to maze)
  b[15][0] = ' ';
  b[16][0] = ' ';

  b[34][0] = ' ';
  b[35][0] = ' ';
  b[36][0] = ' ';

                      // 2 ending gates
  b[14][49] = ' ';
  b[15][49] = ' ';
  b[16][49] = ' ';

  b[34][49] = ' ';
  b[35][49] = ' ';
  b[36][49] = ' ';
} // end function drawoutline


void initial(char b[x][y])  //set maze to  'X' or space
{

  for(int i=0;i<x;i++)   // random number geeneator?? (to make maze desgin, Xs are walls, empty space are walkways )
  // This seems to work for genrating the maze wuthot the solution path, but maybe better way if recurvive clear path isnt working ???

  {
    for(int j=0;j<y;j++) 
    {
      if (rand() % 2 == 0){
      b[i][j]=' ';
      }
      else{
        b[i][j]='X';
      }
    }
  }
  b[15][0] = 0;


} // end function intital   RECURSIVE attempting:



 void recursive(int r, int c, int b[x][y])
 {

   int Direction = 1 + (rand() % 4) ; // creates random number 1,2,3 or 4

  while ( (r != 34) && (r != 35) && ( r != 36) && (r != 14) && (r != 15) && ( r != 16)  &&  (c != 49 ) ) {

  // while  POSTION in NOT on EXIT (one of the two exits) 

          if (Direction == 1){    // Up, two cells -x 
                  b[r-2][c] = ' ';
                  b[r-1][c] = ' ';
                  recursive(r - 2, c,b);
             }

             else if ( Direction == 2){  // Right, two cells +y

                 b[r][c + 2] = ' ';
                 b[r][c + 1] = ' ';
                 recursive(r, c + 2,b);
             }

             else if( Direction == 3){ // Down +x

                 b[r+2][c] = ' ';
                 b[r+1][c] = ' ';
                 recursive(r + 2, c,b);
             }


              else{           // LEFT -y

                 b[r][c - 2] = ' ';
                 b[r][c - 1] = ' ';
                 recursive(r, c - 2,b);
             }
          }

     }




void walk(char b[x][y])
{
  time_t start = time(NULL);

  int xx=15,yy=0;       //start point
  char player = '0';   //player symbol

  char choice;//choice to move (W,A,S,D) or '1' to quit.
  char temp;//to record symbol for current location
  temp = b[xx][yy];//record symbol for current location

  b[xx][yy]=player;//set current location to player symbol ( 0 means player position.

  print(b);//print
  int sum = 0;
  while(choice !='1')//quit if choice == 1
    {
      time_t end = time(NULL);
      printf("Time elapsed: %d seconds\n", (end - start));
      printf("Steps taken: %d\n",sum);
      printf("Type w,a,s, or d then 'enter' to move the cursor '0' to the end. You can't pass through walls (X's) \n");

      scanf("%c",&choice);    //get choice for swtich

      switch  (choice) 

        {
        case 'w':
          if((xx-1)>=0 && b[xx-1][yy]==' ')//(xx-1)>=0 to test if it would walk to the outside of map. b[xx-1][yy]==' ' makes sure that the player can't pass through the wall
        {
          b[xx][yy]=temp;//current location changes to the record saved in temp
          temp = b[xx-1][yy];//use temp to record next location
          b[xx-1][yy]=choice;//next location changes to player symbol
          xx=xx-1;//change x value for current position
          sum++;
         }
      break;

      case 'a':
      if((yy-1)>=0 && b[xx][yy-1]==' ')//(yy-1)>=0 to test if it would walk to the outside of map. b[xx][yy-1]==' ' makes sure that the player can't pass through the wall
      {
        b[xx][yy]=temp;
        temp = b[xx][yy-1];
        b[xx][yy-1]=choice;
        yy=yy-1;
        sum++;
      }
      break;

      case 's':
      if((xx+1)<x && b[xx+1][yy]==' ')//same as above
      {
        b[xx][yy]=temp;
        temp = b[xx+1][yy];
        b[xx+1][yy]=choice;
        xx=xx+1;
        sum++;
      }
      break;

      case 'd':
      if((yy+1)<y && b[xx][yy+1]==' ')//same as above
      {
        b[xx][yy]=temp;
        temp = b[xx][yy+1];
        b[xx][yy+1]=choice;
        yy=yy+1;
        sum++;
      }
      break;

      default:printf("try again\n");// defualt to 'no correct input' 
    }

    b[xx][yy]=player;
    print(b);                //print after each move
  }

  b[xx][yy]=temp;      //reset current postion symbol to '0'

} // end function walk
wolfwoof
  • 21
  • 1
  • 2
  • 3
    It is better to start with a very small maze with a single valid path that you know can be solved, and then move to two or more paths of different length. In your case you won't get the same maze twice, so it will be very hard to debug. At least comment out `srand(time(NULL));` for now, so that the maze is repeatable. – Weather Vane Mar 21 '20 at 20:54
  • 1
    As for making a solvable maze, see [here (wiki)](https://en.wikipedia.org/wiki/Maze_generation_algorithm) and [here (SO)](https://stackoverflow.com/questions/38502/whats-a-good-algorithm-to-generate-a-maze). I suggest to use odd dimensions, so that the all-odd coords are the cells, the all-even coords are always walls and the odd/even and even/odd coords are either walls or passages. – M Oehm Mar 21 '20 at 21:19

0 Answers0