how to make a maze array in C?

81 views Asked by At

I'm trying to write a C program to generate a 2d array for a maze with 1 as the wall and 0 as a path with the recursive backtracking algorithm. I want all four boundaries to be the wall with only the starting point. However, when I print the array, there are always two boundaries used as a path. How can I solve this problem?

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

#define MAX_SIZE 20

void generateMaze(int maze[MAX_SIZE][MAX_SIZE], int row, int col, int width, int height);
void printMaze(int maze[MAX_SIZE][MAX_SIZE], int width, int height);

int main() {
    int width = 20;  // Width of the maze
    int height = 20; // Height of the maze

    int maze[MAX_SIZE][MAX_SIZE];

    // Initialize maze with walls
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            maze[i][j] = 1;
        }
    }

    srand(time(NULL));  // Seed the random number generator

    generateMaze(maze, 1, 0, width, height);  // Generate the maze starting from position (0, 0)
    // start position must avoid the four corner
    
    printMaze(maze, width, height);           // Print the generated maze

    return 0;
}

void generateMaze(int maze[MAX_SIZE][MAX_SIZE], int row, int col, int width, int height) {
    int directions[4][2] = {{0, 2}, {2, 0}, {0, -2}, {-2, 0}};  // Right, Down, Left, Up
    int shuffle[4] = {0, 1, 2, 3};

    // Shuffle the directions randomly
    for (int i = 3; i > 0; i--) {
        int j = rand() % (i + 1);
        int temp = shuffle[i];
        shuffle[i] = shuffle[j];
        shuffle[j] = temp;
    }

    // Mark the current cell as empty space
    maze[row][col] = 0;

    for (int i = 0; i < 4; i++) {
        int dx = directions[shuffle[i]][0];
        int dy = directions[shuffle[i]][1];
        int newRow = row + dy;  // Neighbor row
        int newCol = col + dx;  // Neighbor column
        int midRow = row + dy / 2;  // Midpoint row
        int midCol = col + dx / 2;  // Midpoint column

        // Check if the neighbour coordinates are within the maze boundaries
        if (newRow >= 0 && newRow < height && newCol >= 0 && newCol < width && maze[newRow][newCol] == 1) {
            // Carve a path between the current cell and the neighbour
            maze[midRow][midCol] = 0;
            generateMaze(maze, newRow, newCol, width, height);  // Recursively generate the maze from the neighbor cell
        }
    }
}

void printMaze(int maze[MAX_SIZE][MAX_SIZE], int width, int height) {
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (maze[i][j] == 1) {
                printf("# ");  // Print wall
            } else {
                printf("  ");  // Print empty space
            }
        }
        printf("\n");
    }
}
the output is as follows

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

0

There are 0 answers