Minggu, 20 September 2009

MAZES ALGORITMA (4)

THE CODE

Pada edisi yang lalu saya telah menjelaskan algoritma bagaimana membuat sebuah mazes yang bersifat acak dan perfect. Penjelasan ini tentulah tidak ada gunanya bila kita tidak memberikan contoh dalam bentuk code program.


Graphics 640,480,16,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Global img_path=LoadAnimImage("back.png",40,40,0,16)

;const dir
Const north=8 ;1000
Const east=2 ;0010
Const west=1 ;0001
Const south=4 ;0100

Dim dir(4)
dir(1)=north
dir(2)=east
dir(3)=west
dir(4)=south

;const map
Const mapx=11
Const mapy=12
Const mapmax=mapx*mapy

Dim map(mapx,mapy)

;const node
Const xx=1
Const yy=2
Const stat=3
Global node_ctr=0
Dim node(mapx*mapy,3)

make_maze()
pacmaze()

While Not KeyHit(1)
Cls
draw_map()
Flip
Wend
End

Function make_maze()

;clear map
For x=1 To mapx
For y=1 To mapy
map(x,y)=0
Next
Next

;first node
map(4,4)=east
map(5,4)=west

While True

node_ctr=0

For x = 1 To mapx
For y = 1 To mapy

If map(x,y)=0

find=False

;up
If (y>1)
If (map(x,y-1))
node_ctr=node_ctr+1
node(node_ctr,xx)=x
node(node_ctr,yy)=y
find=True
EndIf
EndIf

;right
If find=False
If (x<mapx)
If (map(x+1,y))
node_ctr=node_ctr+1
node(node_ctr,xx)=x
node(node_ctr,yy)=y
find=True
EndIf
EndIf
EndIf

;left
If find=False
If (x>1)
If (map(x-1,y))
node_ctr=node_ctr+1
node(node_ctr,xx)=x
node(node_ctr,yy)=y
find=True
EndIf
EndIf
EndIf

;down
If find=False
If (y<mapy)
If (map(x,y+1))
node_ctr=node_ctr+1
node(node_ctr,xx)=x
node(node_ctr,yy)=y
find=True
EndIf
EndIf
EndIf

EndIf

Next
Next

;no more open
If node_ctr=0 Then Return

;select random
rctr=Rand(1,node_ctr)

;random path
For x= 1 To 10
b=Rand(1,4)
c=Rand(1,4)

d=dir(b)
dir(b)=dir(c)
dir(c)=d
Next

;create path
For x = 1 To 4
If dir(x)=north
If node(rctr,yy)>1
If map(node(rctr,xx),node(rctr,yy)-1)
map(node(rctr,xx),node(rctr,yy)) = map(node(rctr,xx),node(rctr,yy)) Or north
map(node(rctr,xx),node(rctr,yy)-1)= map(node(rctr,xx),node(rctr,yy)-1) Or south
Exit
EndIf
EndIf

ElseIf dir(x)=south
If node(rctr,yy)<mapy
If map(node(rctr,xx),node(rctr,yy)+1)
map(node(rctr,xx),node(rctr,yy))= map(node(rctr,xx),node(rctr,yy)) Or south
map(node(rctr,xx),node(rctr,yy)+1)= map(node(rctr,xx),node(rctr,yy)+1) Or north
Exit
EndIf
EndIf

ElseIf dir(x)=east
If node(rctr,xx)<mapx
If map(node(rctr,xx)+1,node(rctr,yy))
map(node(rctr,xx),node(rctr,yy))=map(node(rctr,xx),node(rctr,yy)) Or east
map(node(rctr,xx)+1,node(rctr,yy))= map(node(rctr,xx)+1,node(rctr,yy)) Or west
Exit
EndIf
EndIf

ElseIf dir(x)=west
If node(rctr,xx)>1
If map(node(rctr,xx)-1,node(rctr,yy))
map(node(rctr,xx),node(rctr,yy))= map(node(rctr,xx),node(rctr,yy)) Or west
map(node(rctr,xx)-1,node(rctr,yy))= map(node(rctr,xx)-1,node(rctr,yy)) Or east
Exit
EndIf
EndIf

EndIf
Next

Cls
draw_map()
Flip
;WaitKey()

Wend
End Function

Function draw_map()
For x=1 To mapx
For y=1 To mapy
DrawImage img_path,x*40-40,y*40-40,map(x,y)
Next
Next

End Function

Function pacmaze()
While True
finish=True

For x=1 To mapx
For y=1 To mapy
If map(x,y)=north
If y<mapy
map(x,y+1)=map(x,y+1) Or north
map(x,y) = map(x,y) Or south
finish=False
EndIf

ElseIf map(x,y)=south
If y>1
map(x,y) =map(x,y) Or north
map(x,y-1) = map(x,y-1) Or south
finish=False
EndIf

ElseIf map(x,y)=east
If x>1
map(x,y) = map(x,y) Or west
map(x-1,y) = map(x-1,y) Or east
finish=False
EndIf

ElseIf map(x,y)=west
If x<mapx
map(x,y) = map(x,y) Or east
map(x+1,y) = map(x+1,y) Or west
finish=False
EndIf

EndIf
Next
Next

Cls
draw_map()
Flip

If finish Then Return

Wend
End Function

kode ini berjalan pada Blitz3D (download disini)
kodenya sengaja dibuat sangat sederhana sekali, dengan harapan bisa di porting ke berbagai bentuk kode dari bahasa yang lain.



Tulisan Sebelumnya:
MAZES ALGORITMA (3)

0 komentar: