[ create a new paste ] login | about

Link: http://codepad.org/gp0b6Q0X    [ raw code | output | fork ]

C++, pasted on Feb 19:
/**
 * Simple maze by randomized DFS.
 *
 * Wall is "1", corridor is "0". Code relies heavily on various things no one
 * should ever rely on. It's more fun to write it this way.
 *
 * (c) L. Diener 2012
 */

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

#define MAZE_HEIGHT 50
#define MAZE_WIDTH 50

int maze[MAZE_WIDTH][MAZE_HEIGHT];

// "Can I place a corridor tile at (x,y) when coming from (fromx, fromy)?"
int canPlaceCorridor(int x, int y, int fromx, int fromy) {
	// The edge of the maze is all walls (helps avoid special cases, too)
	if(x <= 0 || x >= MAZE_WIDTH-1 || y <= 0 || y >= MAZE_HEIGHT-1) {
		return 0;
	}

	// Of all tiles around (x,y) that are not in the direction of (fromx,
	// fromy), none can be a corridor. Also, (x,y) itself should be a wall.
	int corridorCount = 0;
	for(int xd = -(fromx >= x); xd <= (fromx <= x); xd++) {
		for(int yd = -(fromy >= y); yd <= (fromy <= y); yd++) {
			corridorCount += !maze[x+xd][y+yd];
		}
	}

	return (corridorCount == 0);
}

// Build maze by recursion.
void buildMazeRecursive(int x, int y) {
	int triedDir = 0;
	while(triedDir != 15) {
		int dir = rand() % 4;
		int xd = (dir < 2) ? 0 : dir == 2 ? -1 : 1;
		int yd = (dir >= 2) ? 0 : dir == 0 ? -1 : 1;
		if((triedDir & (1 << dir)) == 0) {
			triedDir = triedDir | (1 << dir);
			if(canPlaceCorridor(x+xd,y+yd,x,y)) {
				maze[x+xd][y+yd] = 0;
				buildMazeRecursive(x+xd,y+yd);
			}
		}
	}
	return;
}

// Set the whole maze to walls.
void clearMaze() {
	for(int x = 0; x < MAZE_WIDTH; x++ ) {
		for(int y = 0; y < MAZE_HEIGHT; y++ ) {
			maze[x][y] = 1;
		}
	}
}

// Build maze, create exit and entry points.
void buildMaze() {
	// Start fresh.
	clearMaze();

	// Randomize.
	srand(time(0));

	// Actually build maze, starting from tile (0,1)
	buildMazeRecursive(0,1);

	// Make tile (0, 1) te entry point.
	maze[0][1] = 0;

	// Make the first tile that fits in the right wall the exit point.
	for(int y = MAZE_HEIGHT - 1; y > 0; y--) {
		if(maze[MAZE_WIDTH-2][y] == 0) {
			maze[MAZE_WIDTH-1][y] = 0;
			break;
		}
	}
}

// Print out the maze, # is wall, space is free
void printMaze() {
	for(int y = 0; y < MAZE_HEIGHT; y++ ) {
		for(int x = 0; x < MAZE_WIDTH; x++ ) {
			printf("%c", " #"[maze[x][y]]);
		}
		printf("\n");
	}
}

int main(int argc, char** argv) {
	buildMaze();
	printMaze();
}


Output:
##################################################
   #     # #    ## ##  ##    # #   #   #    #   ##
##   ### #   ##  # #  ##  ##   # #   # # ##   #  #
######   ## #### #   ##  ##### # ### #   ### ### #
#    # ###  ##   # #    ## # # #  ##### ## ### # #
# ##   # # ##  ### ######      ##     ###   #  # #
# ######   ## ## #     #### ####  # #   ###   ## #
#   ## # ###  #  # ###    #  #   ######   ## ##  #
###  #   #   ###     #### ## ######   # #  #  # ##
# ## # # ## ###  ###  ##  #     #   # # ## ## #  #
#    # #  #   ##  ###  # ###### # ### # #  #  ## #
### ## ## # #  ##   ## # #  ##  # #   # # ###    #
##  #  ## ####  ######## ##  ##   # ##### ########
#  #####  ## ## #    #   ###  #####    #   ###   #
# ##     ##  #  # ##   #   ##  # ##### # #  #  # #
# ## # #### ## ## ### ######  ##     # # ##   ## #
#  # #  #   #  #    ###    ## #  # ### #### #### #
## #### # ### ### #  #  ## #    ##  ##  ##    #  #
## ##   #   #   #### # ##  ### ####  ## #  # ## ##
#     ##### ###  #   #  # ###   ####    # ####   #
#######     # ## ### ## #   ###  # ###### ##   # #
#    #  ###    #   #    # #  ###   #   ####  ### #
# ## ## #   ## ### # ####### # ###   #    # ###  #
#  #    ###  ###   #  ##     #   # # ## #   #   ##
## #######  ##   #### #  ##### #   ###### ########
#  #       ##  ####   # ##   # #####   #   ##   ##
# ### ### ##  ##  ## ##  # #   ##    # #####  #  #
#   ####  ## #### #  ### # # ###  ####    ## ### #
# #    ##  #  #   # ## # #####   ### ####    # # #
# ####  ## ## # # #  # #   ##  ###     ####### # #
#    ##  #### # # ## # ###  # ## # ## ##    #  # #
#### ###    # ###    #  ### # #    #   ## # # ## #
#  # # ## ###  ### ### ##   # ## # # #    #   #  #
##   #  #   ##   #   # #  # #  ### ### ###### # ##
## #### ## ##### ### # # ## ##   # ##  #   ## #  #
#     #  #  #  # ##    #  #  ###   #  ## #  #### #
##### ## ##   ##  #### ## ##  ###### ##  ## #    #
# ##      ###  ##   ## ##  ##     #   # ##  # ## #
#  ##### ## ##  ###  ###  ######### ###  ##   #  #
##   #   #  ###   ##  #  ##   ##      ##  ##### ##
#  #   ### ## # ##### ##    #    ####  ## ##  #  #
# ######   #  #     #  #### ######    ##  #  ### #
# # #  ###   ### ## ##    ####   ######  ## ## # #
#   ##     #  ## #   ####      #  #     ##     # #
###  ### # ####  #####  #########   ###### # ### #
# ##     #  #   ##     ## # # # ######     ###   #
#  ####### ## ###  # ###  #        ##  # ###   ###
# ##    #   #  ## ##   ##   ######  # ##  #  #   #
#    ##   # ##     # #    #      ##    ##   ## #  
##################################################


Create a new paste based on this one


Comments: