Skip to content

Instantly share code, notes, and snippets.

@Jacajack
Last active June 16, 2017 18:25
Show Gist options
  • Save Jacajack/34badab7ade6d0d7afcc7b439c989ddd to your computer and use it in GitHub Desktop.
Save Jacajack/34badab7ade6d0d7afcc7b439c989ddd to your computer and use it in GitHub Desktop.
Fast maze generator
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define VERSION "v0.5"
typedef struct
{
int x, y;
void *parent;
char c;
int dirs;
} Node;
struct
{
Node *nodes;
int width, height;
} map;
int initMap( )
{
int i, j;
Node *n;
if ( ( map.nodes = calloc( map.width * map.height, sizeof( Node ) ) ) == NULL ) return 1;
for ( i = 0; i < map.width; i++ )
{
for ( j = 0; j < map.height; j++ )
{
n = &map.nodes[i + j * map.width];
if ( i * j % 2 )
{
n->x = i;
n->y = j;
n->dirs = 15;
n->c = ' ';
}
else
{
n->c = '#';
}
}
}
return 0;
}
Node *linkNode( Node *n )
{
int x, y;
int dir;
Node *dest;
if ( n == NULL ) return NULL;
n->c = ' ';
while ( n->dirs )
{
dir = ( 1 << ( rand( ) % 4 ) );
if ( ~n->dirs & dir ) continue;
n->dirs &= ~dir;
switch ( dir )
{
case 1:
if ( n->x + 2 < map.width )
{
x = n->x + 2;
y = n->y;
}
else continue;
break;
case 2:
if ( n->y + 2 < map.height )
{
x = n->x;
y = n->y + 2;
}
else continue;
break;
case 4:
if ( n->x - 2 >= 0 )
{
x = n->x - 2;
y = n->y;
}
else continue;
break;
case 8:
if ( n->y - 2 >= 0 )
{
x = n->x;
y = n->y - 2;
}
else continue;
break;
}
dest = map.nodes + x + y * map.width;
if ( dest->c == ' ' )
{
if ( dest->parent != NULL ) continue;
dest->parent = n;
map.nodes[n->x + ( x - n->x ) / 2 + ( n->y + ( y - n->y ) / 2 ) * map.width].c = ' ';
return dest;
}
}
return n->parent;
}
void mapDraw( )
{
int i, j;
for ( i = 0; i < map.height; i++ )
{
for ( j = 0; j < map.width; j++ )
{
printf( "%c", map.nodes[j + i * map.width].c );
}
printf( "\n" );
}
}
int main( int argc, char **argv )
{
int i, badarg;
long seed;
Node *start, *last;
seed = time( NULL );
map.width = 41;
map.height = 21;
for ( i = 1; i < argc; i++ )
{
badarg = 1;
if ( !strcmp( argv[i], "-h" ) || !strcmp( argv[i], "--help" ) )
{
fprintf( stderr, "%s - pretty fast DFS maze generator " VERSION "\n", argv[0] );
fprintf( stderr, "\t-h - display this help message\n" );
fprintf( stderr, "\t-x <width> - maze width\n" );
fprintf( stderr, "\t-y <height> - maze height\n" );
fprintf( stderr, "\t-s <seed> - random generator seed\n" );
badarg = 0;
exit( 0 );
}
if ( !strcmp( argv[i], "-x" ) )
if ( ++i >= argc || ( badarg = 0, !sscanf( argv[i], "%d", &map.width ) ) )
fprintf( stderr, "%s: bad value for '%s'\n", argv[0], argv[i - 1] ), exit( 1 );
if ( !strcmp( argv[i], "-y" ) )
if ( ++i >= argc || ( badarg = 0, !sscanf( argv[i], "%d", &map.height ) ) )
fprintf( stderr, "%s: bad value for '%s'\n", argv[0], argv[i - 1] ), exit( 1 );
if ( !strcmp( argv[i], "-s" ) )
if ( ++i >= argc || ( badarg = 0, !sscanf( argv[i], "%ld", &seed ) ) )
fprintf( stderr, "%s: bad value for '%s'\n", argv[0], argv[i - 1] ), exit( 1 );
if ( badarg )
{
fprintf( stderr, "%s: unrecognized option '%s'\n", argv[0], argv[i] );
exit( 1 );
}
}
if ( !( map.width % 2 ) || !( map.height % 2 ) )
{
fprintf( stderr, "%s: dimensions must be odd!\n", argv[0] );
exit( 1 );
}
if ( initMap( ) )
{
fprintf( stderr, "%s: out of memory!\n", argv[0] );
exit( 1 );
}
start = &map.nodes[1 + 1 * map.width];
start->parent = start;
last = start;
while ( ( last = linkNode( last ) ) != start );
mapDraw( );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment