Date: 02-16-2022
Return to Index
'Compilable Example: (Jose Includes)
created by gbSnippets
#Compiler PBWin 10
#Compile Exe
#Dim All
%Unicode = 1
#Include "Win32API.inc"
%IDC_Graphic = 500
Global hDlg As Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Maze Generator",300,300,415,270, %WS_OverlappedWindow To hDlg
Control Add Graphic, hDlg, %IDC_Graphic,"Push", 0,0,410,270,%SS_Notify
Graphic Attach hDlg, %IDC_Graphic, ReDraw
GenerateMaze %Blue, 10, %Yellow, 39,25
Dialog Show Modal hDlg, Call DlgProc
End Function
CallBack Function DlgProc() As Long
If Cb.Msg = %WM_Command And Cb.Ctl = %IDC_Graphic Then GenerateMaze %Blue, 10, %Yellow, 39,25
End Function
Sub GenerateMaze(WallColor As Long, WallSize As Long, BGColor As Long, MazeX As Long, MazeY As Long)
Local cN, cS, cDir() As Point, intDir, intDone, blnBlocked, blnMaze() As Long
ReDim blnMaze(41,35) As Long, cDir(3)
Randomize Timer : Reset blnMaze() : Graphic Clear
Graphic Box (WallSize,WallSize)-((MazeX+1)*WallSize,(MazeY+1)*WallSize),,WallColor,WallColor
Do
cS.X = 2 + (Int(((MazeX - 1) * Rnd) / 2) * 2) ' this code is used to make sure the numbers are odd
cS.Y = 2 + (Int(((MazeY - 1) * Rnd) / 2) * 2) ' this code is used to make sure the numbers are odd
If intDone = 0 Then
blnMaze(cS.X, cS.Y) = %True ' first one is free!
Graphic Box (cS.x*WallSize,cS.y*WallSize)-(cS.x*WallSize+WallSize,cS.y*WallSize+WallSize),,BGColor,BGColor
End If
If blnMaze(cS.X, cS.Y) Then
Do
Reset cDir()
Select Case Rnd(0,3) ' four possible sets of directions
Case 0 : cDir(0).X = -1: cDir(1).X = 1 : cDir(2).Y = -1: cDir(3).Y = 1
Case 1 : cDir(3).X = -1: cDir(2).X = 1 : cDir(1).Y = -1: cDir(0).Y = 1
Case 2 : cDir(2).X = -1: cDir(3).X = 1 : cDir(0).Y = -1: cDir(1).Y = 1
Case 3 : cDir(1).X = -1: cDir(0).X = 1 : cDir(3).Y = -1: cDir(2).Y = 1
End Select
blnBlocked = %True ' loop through order of directions
For intDir = 0 To 3
cN.X = cS.X + (cDir(intDir).X * 2) ' work out where this direction is
cN.Y = cS.Y + (cDir(intDir).Y * 2) ' work out where this direction is
If (cN.X < MazeX And cN.X > 1 And cN.Y < MazeY And cn.Y > 1) And (blnMaze(cN.x,cN.y)=%False) Then
blnMaze(cN.X, cN.Y) = %True ' create a path
Graphic Box (cn.x*WallSize,cn.y*WallSize)-(cn.x*WallSize+WallSize,cn.y*WallSize+WallSize),,BGColor,BGColor
blnMaze(cS.X + cDir(intDir).X, cS.Y + cDir(intDir).Y) = %True ' and the square inbetween
Graphic Box ((cS.X + cDir(intDir).X)*WallSize,(cS.Y + cDir(intDir).Y)*WallSize)-((cS.X + cDir(intDir).X)*WallSize+WallSize,(cS.Y + cDir(intDir).Y)*WallSize+WallSize),,BGColor, BGColor
cS.X = cN.X : cS.Y = cN.Y ' this is now the current square
blnBlocked = %False
intDone = intDone + 1 ' increment paths created
Exit For
End If
Next
Loop Until blnBlocked ' loop until a path was created
End If
Loop While intDone + 1 < ((MazeX - 1) * (MazeY - 1)) / 4 ' create enough paths to fill the whole grid
Graphic Box (2*WallSize,1*WallSize)-(2*WallSize+WallSize,1*WallSize+WallSize),,%Green,%Green
Graphic Box ((MazeX-1)*WallSize,MazeY*WallSize)-((MazeX-1)*WallSize+WallSize,MazeY*WallSize+WallSize),,%Red,%Red
Graphic ReDraw
End Sub
'gbs_01460
'Date: 10-17-2014
http://www.garybeene.com/sw/gbsnippets.htm