Simple maze creation
I''m trying to create a 2D simple maze. Well, what i would like exactly is a 2D array with 0 (empty) and 1 (wall). Please help me. Any source code can be useful. thanks
Eric
Start off by trying simple, and working up in size and complexity
probably want to add some structure to this, and allocate some memory for the array before it leaves the alpha stages, but basically try something like this for a simple 8x8 maze
typedef struct {
int x,y;
//appropriate inline statements such as add, mult...
} Axis2;
bool maze[8][8]={0};
Axis2 Location;
Location.x=0;//start column
Location.y=0;//start row
//Load the maze from a file or hard code it if you want.
if (keys[VK_UP]) //if (user pushes up button)
{
if (maze[Location.x][Location.y+1])//something thier
{/*let user know they hit a wall and take appropriate action*/}
else
{Location.y++;/*anything else you might want*/}
}
if (keys[VK_DOWN])
{
if (maze[Location.x][Location.y-1])//something thier
{/*let user know they hit a wall and take appropriate action*/}
else
{Location.y--;/*anything else you might want*/}
}if (keys[VK_LEFT]) //if (user pushes up button)
{
if (maze[Location.x-1][Location.y])//something thier
{/*let user know they hit a wall and take appropriate action*/}
else
{Location.x--;/*anything else you might want*/}
}
if (keys[VK_RIGHT])
{
if (maze[Location.x+1][Location.y])//something thier
{/*let user know they hit a wall and take appropriate action*/}
else
{Location.x++;/*anything else you might want*/}
}
It will be really important to make sure that they can''t walk out of the maze, so it might be good to put walls all around the edges, or if they exit the maze space they win, and it takes appropritate action from their.
Shouldn''t be too hard, as far as I see, a simple 2D maze has the hardest part being to actually draw the maze. repost anything you think else might work here, or if you get it to work. thanks.
-Sulgurth
Sulgurth@hotmail.com
probably want to add some structure to this, and allocate some memory for the array before it leaves the alpha stages, but basically try something like this for a simple 8x8 maze
typedef struct {
int x,y;
//appropriate inline statements such as add, mult...
} Axis2;
bool maze[8][8]={0};
Axis2 Location;
Location.x=0;//start column
Location.y=0;//start row
//Load the maze from a file or hard code it if you want.
if (keys[VK_UP]) //if (user pushes up button)
{
if (maze[Location.x][Location.y+1])//something thier
{/*let user know they hit a wall and take appropriate action*/}
else
{Location.y++;/*anything else you might want*/}
}
if (keys[VK_DOWN])
{
if (maze[Location.x][Location.y-1])//something thier
{/*let user know they hit a wall and take appropriate action*/}
else
{Location.y--;/*anything else you might want*/}
}if (keys[VK_LEFT]) //if (user pushes up button)
{
if (maze[Location.x-1][Location.y])//something thier
{/*let user know they hit a wall and take appropriate action*/}
else
{Location.x--;/*anything else you might want*/}
}
if (keys[VK_RIGHT])
{
if (maze[Location.x+1][Location.y])//something thier
{/*let user know they hit a wall and take appropriate action*/}
else
{Location.x++;/*anything else you might want*/}
}
It will be really important to make sure that they can''t walk out of the maze, so it might be good to put walls all around the edges, or if they exit the maze space they win, and it takes appropritate action from their.
Shouldn''t be too hard, as far as I see, a simple 2D maze has the hardest part being to actually draw the maze. repost anything you think else might work here, or if you get it to work. thanks.
-Sulgurth
Sulgurth@hotmail.com
Thank you but it''s not exactly what i''m looking for. What i want is a algorithm to fill the array with good values for the walls.
111111111
100010001
111011101
101000001
101111101
100010001
101110111
100000001
111111111
something like this but randomly
Thanks
Eric
111111111
100010001
111011101
101000001
101111101
100010001
101110111
100000001
111111111
something like this but randomly
Thanks
Eric
Wow, haven''t seen much like that since the late ''80s / early ''90s. I know I''ve got some sample code hanging around here, I just have to find it.
Try ;-
http://www.chesworth.com/pv/games/random_maps.htm
for some random maze generator code.....
http://www.chesworth.com/pv/games/random_maps.htm
for some random maze generator code.....
Depends - do you want a maze with thin walls, or thick walls? I wrote a thick-walled one in assembler (turbo pascal for dos''s built-in assembler) once.
Send a couple of "ants" to dig tunnels through the maze - make them split, turn and whatnot, and don''t allow them to make any loops. To avoid several ants getting stuck against eachother and zig-zagging, don''t loop through all the ants - only update the most recent ant at each tick. Once that''s done, quickly check that the maze is a complete maze - ie, the first ant didn''t just double back on itself and get stuck - if this happens, just start again.
For a maze with a playing area of 76 by 46, this method exceeded the speed of the repeat rate of the keyboard on my K6-2/300.
Send a couple of "ants" to dig tunnels through the maze - make them split, turn and whatnot, and don''t allow them to make any loops. To avoid several ants getting stuck against eachother and zig-zagging, don''t loop through all the ants - only update the most recent ant at each tick. Once that''s done, quickly check that the maze is a complete maze - ie, the first ant didn''t just double back on itself and get stuck - if this happens, just start again.
For a maze with a playing area of 76 by 46, this method exceeded the speed of the repeat rate of the keyboard on my K6-2/300.
There is a very simple way to do this. Start with a grid full of 1s.
a) pick N random points on your grid. If you want an entry and exit point then add them accordingly. If you want only one entry and exit point, make sure no other points occur on grid edges (including those in step b). Set each of these points in your grid to 0 (signifying no wall) as you generate them.
b) if you don't want rooms, skip this step. But for a dungeon maze, draw a box around each point N of width W and height H (ie: fill those array elements with 0).
c) finally, plot a path from point N(x) to point N(x+1), N-1 times. You can do this going horizontally, then vertically or vice-versa. Make 0s at each plot.
Then just loop through your array and draw a wall for each 1 value remaining.
Edited by - Sydan on February 6, 2001 8:50:20 PM
a) pick N random points on your grid. If you want an entry and exit point then add them accordingly. If you want only one entry and exit point, make sure no other points occur on grid edges (including those in step b). Set each of these points in your grid to 0 (signifying no wall) as you generate them.
b) if you don't want rooms, skip this step. But for a dungeon maze, draw a box around each point N of width W and height H (ie: fill those array elements with 0).
c) finally, plot a path from point N(x) to point N(x+1), N-1 times. You can do this going horizontally, then vertically or vice-versa. Make 0s at each plot.
Then just loop through your array and draw a wall for each 1 value remaining.
Edited by - Sydan on February 6, 2001 8:50:20 PM
Maze generator and solver:
-Hans [home]
#include<stdio.h>#include<stdlib.h>int a[99999],p[]={80,1,-80,-1},e,d,o=-78,c;main(n){scanf("%d",&c);for(c=c*160-o;o>0|n;e=n?0:e){for(;n&&e++<2;a[o]=d+1)o+=p[d];n=o+p[d=rand()&3]*2;if(n%80<1|n<0|n>c||a[n])n=e++>99?0,o+=p[(a[o]+1)&3]*2,e=0:0;}for(a[o=c]=1;o>0;a[o]=5,o+=p[d&3])d=a[o]+1;for(;++e<c;printf(e%80?"%c":"\n","# ."[a[e]]));}
-Hans [home]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement