ich programmiere im Moment an einer PureBasic-Umsetzung des Deep First Search-Algorithmuses (http://en.wikipedia.org/wiki/Maze_gener ... rst_search). Allerdings hänge ich im Moment an der Umsetzung. Denn immer wenn ich den Algorithmus starte, dann "generiert" er mir lediglich eine gerade Linie. Allerdings finde ich den Fehler, weshalb, nicht.
Code: Alles auswählen
Structure MazeCells
visited.b
isWall.b
EndStructure
Structure Param
startX.l
startY.l
EndStructure
Global NewMap Maze.MazeCells()
NewMap MazeBackup.MazeCells()
#MazeWidth = 25
#MazeHeight = 19
;+++++++++++++++++++++++++++++++
;+ Deep First Search Algorithm +
;+++++++++++++++++++++++++++++++
Procedure DeepFirstSearch(*Params.Param)
Protected curX, curY
curX = *Params\startX
curY = *Params\startY
Repeat
If curX < 1 Or curY < 1 Or curX >= #MazeWidth Or curY >= #MazeHeight
ProcedureReturn 0
EndIf
If curX % 2 = 0 Or curY % 2 = 0
ProcedureReturn 0
EndIf
Debug "x: " : Debug curX
Debug "y: " : Debug curY
Repeat
success.b = 0
direction = Random(3) ; anscheinend kommt hier immer die Zahl 3 aus, da der Weg von links nach rechts führt.
Select direction
Case 0 ; oben
If curX = 1 Or Maze(Str(curX)+"|"+Str(curY - 2))\visited = 1
Continue
ElseIf curY >= 3
Maze(Str(curX)+"|"+Str(curY - 1))\isWall = 0
Maze()\visited = 1
Maze(Str(curX)+"|"+Str(curY - 2))\isWall = 0
Maze()\visited = 1
nextX = curX
nextY = curY - 2
success = 1
EndIf
Case 1 ; links
If curY = 1 Or Maze(Str(curX - 2)+"|"+Str(curY))\visited = 1
Continue
ElseIf curX >= 3
Maze(Str(curX - 1)+"|"+Str(curY))\isWall = 0
Maze()\visited = 1
Maze(Str(curX - 2)+"|"+Str(curY))\isWall = 0
Maze()\visited = 1
nextX = curX - 2
nextY = curY
success = 1
EndIf
Case 2 ; unten
If curY = #MazeHeight - 1 Or Maze(Str(curX)+"|"+Str(curY + 2))\visited = 1
Continue
ElseIf curY > #MazeHeight - 3
Maze(Str(curX)+"|"+Str(curY + 1))\isWall = 0
Maze()\visited = 1
Maze(Str(curX)+"|"+Str(curY + 2))\isWall = 0
Maze()\visited = 1
nextX = curX
nextY = curY + 2
success = 1
EndIf
Case 3 ; rechts
If curY = #MazeWidth - 1 Or Maze(Str(curX + 2)+"|"+Str(curY))\visited = 1
Continue
ElseIf curX <= #MazeWidth - 3
Maze(Str(curX + 1)+"|"+Str(curY))\isWall = 0
Maze()\visited = 1
Maze(Str(curX + 2)+"|"+Str(curY))\isWall = 0
Maze()\visited = 1
nextX = curX + 2
nextY = curY
success = 1
EndIf
EndSelect
Until success = 1
curX = nextX
curY = nextY
Until curY >= #MazeHeight Or curX >= #MazeWidth
ProcedureReturn 1
EndProcedure
;+++++++++++++++++++++++++
;+ Main code starts here +
;+++++++++++++++++++++++++
;{
OpenConsole()
PrintN("Initialising DirectX...")
If InitSprite() = 0 Or InitKeyboard() = 0
MessageRequester("MazeAlgorithm", "Could not initialize DirectX!")
End
EndIf
PrintN("Filling maze...")
For i = 0 To #MazeHeight - 1
For j = 0 To #MazeWidth - 1
Maze(Str(j)+"|"+Str(i))\visited = 0
Maze()\isWall = 1
Next
Next
PrintN("Open screen...")
screen = OpenScreen(1920, 1080, 32, "MazeAlgorithm", #PB_Screen_SmartSynchronization, 60)
If screen
Repeat
ExamineKeyboard()
ClearScreen(#Black)
If StartDrawing(ScreenOutput())
DrawText(#MazeWidth * 31 + 10, 5, "Keys:")
DrawText(#MazeWidth * 31 + 10, 35, "D: Deep First Search-Algorithm")
Line(0, #MazeHeight * 31 + 1, #MazeWidth * 31 + 1, 1, #White)
Line(#MazeWidth * 31 + 1, 0, 1, #MazeHeight * 31 + 1, #White)
StopDrawing()
EndIf
If KeyboardReleased(#PB_Key_D)
startTime = ElapsedMilliseconds()
*Params.Param = AllocateMemory(SizeOf(Param))
*Params\startX = 1
*Params\startY = 1
PrintN("Generating maze with deep first search alogrithm....")
CopyMap(Maze(), MazeBackup())
thread = CreateThread(@DeepFirstSearch(), *Params)
; If thread = 0
; PrintN("Generated maze in " + Str(ElapsedMilliseconds() - startTime) + " seconds")
; EndIf
EndIf
;+++++++++++
;+ Rendern +
;+++++++++++
If IsScreenActive()
If StartDrawing(ScreenOutput())
If IsThread(thread)
ForEach MazeBackup()
If MazeBackup()\isWall = 0
Box(Val(StringField(MapKey(MazeBackup()), 1, "|")) * 31, Val(StringField(MapKey(MazeBackup()), 2, "|")) * 31, 31, 31, #White)
EndIf
Next
Else
ForEach Maze()
If Maze()\isWall = 0
Box(Val(StringField(MapKey(Maze()), 1, "|")) * 31, Val(StringField(MapKey(Maze()), 2, "|")) * 31, 31, 31, #White)
EndIf
Next
EndIf
StopDrawing()
EndIf
FlipBuffers()
EndIf
Until KeyboardPushed(#PB_Key_LeftControl) And KeyboardPushed(#PB_Key_Q)
CloseScreen()
PrintN("Finished!")
PrintN("Press any key...")
EndIf
While Inkey() = "" : Wend
;}
TheCreepyProgramer