## maze creation

New to FreeBASIC? Post your questions here.
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

### maze creation

i want to create a maze at random

i have this :

Code: Select all

`const as integer maxx = 10const as integer maxy = 10dim shared as integer maze( maxx , maxy )dim as integer x , yfor x = 0 to maxx  for y = 0 to maxy    maze( x , y ) = not 0    if x mod 2 and y mod 2 then maze( x , y ) = 0  next ynext x''createmazefor y = 0 to maxy  for x = 0 to maxx    if maze( x , y ) then      print "#" ;   else      print " " ;   end if  next x  printnext y`

i need this for a opengl fb game
gothon
Posts: 224
Joined: Apr 11, 2011 22:22

### Re: maze creation

This is a good start. Here I have completed the maze generator by maintaining a list of reachable walls (edges) and repeatedly busting down a randomly selected wall from the list, but only if that wall leads to an unvisited area. As always, it is best to approach maze generation using graph theoretic concepts. This technique effectively creates a spanning tree, so there are no cycles. If you want, you can make cycles by destroying additional walls.

I hope this helps.

Code: Select all

`type wall  as integer wallx, wally, roomx, roomyend typeconst as integer maxx = 10const as integer maxy = 10dim shared as integer maze( maxx , maxy )dim shared as wall walls( maxx * maxy )dim as integer x , y , maxwallfor x = 0 to maxx  for y = 0 to maxy    maze( x , y ) = not 0  next ynext xrandomizewalls( 0 ).roomx = 1walls( 0 ).roomy = 1walls( 0 ).wallx = 0walls( 0 ).wally = 1do  x = int(rnd * (1 + maxwall))  if walls( x ).roomx >= 0 and walls( x ).roomx <= maxx and walls( x ).roomy >= 0 and walls( x ).roomy <= maxy then    if maze( walls( x ).roomx , walls( x ).roomy ) then      maze( walls( x ).roomx , walls( x ).roomy ) = 0      maze( walls( x ).wallx , walls( x ).wally ) = 0            walls( maxwall + 1 ).wallx = walls( x ).roomx - 1      walls( maxwall + 1 ).wally = walls( x ).roomy      walls( maxwall + 1 ).roomx = walls( x ).roomx - 2      walls( maxwall + 1 ).roomy = walls( x ).roomy            walls( maxwall + 2 ).wallx = walls( x ).roomx      walls( maxwall + 2 ).wally = walls( x ).roomy + 1      walls( maxwall + 2 ).roomx = walls( x ).roomx      walls( maxwall + 2 ).roomy = walls( x ).roomy + 2            walls( maxwall + 3 ).wallx = walls( x ).roomx      walls( maxwall + 3 ).wally = walls( x ).roomy - 1      walls( maxwall + 3 ).roomx = walls( x ).roomx      walls( maxwall + 3 ).roomy = walls( x ).roomy - 2            walls( maxwall + 4 ).wallx = walls( x ).roomx + 1      walls( maxwall + 4 ).wally = walls( x ).roomy      walls( maxwall + 4 ).roomx = walls( x ).roomx + 2      walls( maxwall + 4 ).roomy = walls( x ).roomy            maxwall += 4    end if  end if    walls( x ).wallx = walls( maxwall ).wallx  walls( x ).wally = walls( maxwall ).wally  walls( x ).roomx = walls( maxwall ).roomx  walls( x ).roomy = walls( maxwall ).roomy  maxwall -= 1loop until maxwall = -1maze( maxx , maxy - 1 ) = 0''createmazefor y = 0 to maxy  for x = 0 to maxx    if maze( x , y ) then      print "#" ;   else      print " " ;   end if  next x  printnext y`
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

### Re: maze creation

thans for help

see this forum | programming | game dev | a simple maze

what i did whit it