maze creation

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

maze creation

Postby bluatigro » Apr 29, 2012 17:34

i want to create a maze at random

i have this :

Code: Select all

const as integer maxx = 10
const as integer maxy = 10
dim shared as integer maze( maxx , maxy )
dim as integer x , y
for 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 y
next x

''createmaze

for y = 0 to maxy
  for x = 0 to maxx
    if maze( x , y ) then
      print "#" ;
   else
      print " " ;
   end if
  next x
  print
next y


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

Re: maze creation

Postby gothon » May 02, 2012 5:16

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, roomy
end type

const as integer maxx = 10
const as integer maxy = 10
dim shared as integer maze( maxx , maxy )
dim shared as wall walls( maxx * maxy )

dim as integer x , y , maxwall

for x = 0 to maxx
  for y = 0 to maxy
    maze( x , y ) = not 0
  next y
next x

randomize

walls( 0 ).roomx = 1
walls( 0 ).roomy = 1
walls( 0 ).wallx = 0
walls( 0 ).wally = 1
do
  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 -= 1
loop until maxwall = -1

maze( maxx , maxy - 1 ) = 0

''createmaze

for y = 0 to maxy
  for x = 0 to maxx
    if maze( x , y ) then
      print "#" ;
   else
      print " " ;
   end if
  next x
  print
next y
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: maze creation

Postby bluatigro » May 03, 2012 10:31

thans for help

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

what i did whit it

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 4 guests