-
Notifications
You must be signed in to change notification settings - Fork 8
/
Example - Breath First grid search.monkey
211 lines (198 loc) · 5.17 KB
/
Example - Breath First grid search.monkey
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'Is this the breath first search? Not to familiar with it.
Import mojo
Class search
Field screenwidth:Int,screenheight:Int
Field mapwidth:Int,mapheight:Int
Field tilewidth:Float,tileheight:Float
Field sx:Int,sy:Int
Field ex:Int,ey:Int
Field map:Int[][]
Field myopenlist:List<node>
Field myclosedlist:List<node>
Field mypath:List<path>
Field mx:Int[]=[0,-1,1,0]
Field my:Int[]=[-1,0,0,1]
Method New(screenwidth:Int,screenheight:Int,mapwidth:Int,mapheight:Int)
Self.screenwidth = screenwidth
Self.screenheight = screenheight
Self.mapwidth = mapwidth
Self.mapheight = mapheight
Self.tilewidth = Float(screenwidth) / Float(mapwidth)
Self.tileheight = Float(screenheight) / Float(mapheight)
map = New Int[mapwidth][]
For Local i:Int=0 Until mapwidth
map[i] = New Int[mapheight]
Next
makemap()
End Method
Method makemap:Void()
For Local i:Int=0 Until mapwidth/3
Local x1:Float=Rnd(0,mapwidth)
Local y1:Float=Rnd(0,mapheight)
Local angle:Int=Rnd(360)
Local dist:Int=Rnd(3,7)
For Local ii:Int=0 Until dist
x1+=Cos(angle)*1
y1+=Sin(angle)*1
If x1<0 Or y1<0 Or x1>=mapwidth Or y1>=mapheight Then Exit
map[x1][y1] = 1
Next
Next
End Method
Method search:Bool(sx:Int,sy:Int,ex:Int,ey:Int)
If sx=ex And sy=ey Then Return False
If map[sx][sy] <> 0 Or map[ex][ey] <> 0 Then Return False
Self.sx = sx
Self.sy = sy
Self.ex = ex
Self.ey = ey
myopenlist = New List<node>
myclosedlist = New List<node>
mypath = New List<path>
myopenlist.AddFirst(New node(sx,sy,sx,sy))
' the search
While Not myopenlist.IsEmpty
Local cx:Int=myopenlist.First.x
Local cy:Int=myopenlist.First.y
Local px:Int=myopenlist.First.parentx
Local py:Int=myopenlist.First.parenty
myclosedlist.AddFirst(New node(cx,cy,px,py))
If cx = ex And cy = ey Then
findpathback()
Return True
End If
myopenlist.RemoveFirst()
For Local i:Int=0 Until mx.Length
Local nx:Int=cx+mx[i]
Local ny:Int=cy+my[i]
If nx<0 Or ny<0 Or nx>=mapwidth Or ny>=mapheight Then Continue
If map[nx][ny] = 0 'if the map is not obstructed
If isonclosedlist(nx,ny) = False And isonopenlist(nx,ny) = False
myopenlist.AddLast(New node(nx,ny,cx,cy))
End If
End If
Next
Wend
Return False
End Method
' Here we calculate back from the end back to the
' start and create the path list.
Method findpathback:Bool()
Local x:Int=ex
Local y:Int=ey
mypath.AddFirst(New path(x,y))
Repeat
For Local i:=Eachin myclosedlist
If i.x = x And i.y = y
x = i.parentx
y = i.parenty
mypath.AddFirst(New path(x,y))
End If
Next
If x = sx And y = sy Then Return True
Forever
End Method
Method drawpath()
If Not mypath Then Return
For Local i:=Eachin mypath
SetColor 255,0,0
DrawOval i.x*tilewidth+(tilewidth/4),i.y*tileheight,tilewidth/4,tileheight/2
Next
End Method
Method drawclosedlist()
If Not myclosedlist Then Return
For Local i:=Eachin myclosedlist
DrawText "loc:"+i.x+","+i.y, i.x*tilewidth,i.y*tileheight+15
DrawText "par:"+i.parentx+","+i.parenty, i.x*tilewidth,i.y*tileheight+30
Next
End Method
Method isonopenlist:Bool(x:Int,y:Int)
For Local i:=Eachin myopenlist
If x = i.x And y = i.y Then
Return True
End If
Next
Return False
End Method
Method isonclosedlist:Bool(x:Int,y:Int)
For Local i:=Eachin myclosedlist
If x = i.x And y = i.y Then
Return True
End If
Next
Return False
End Method
Method draw()
SetColor 255,255,255
For Local y:Int = 0 Until mapheight
For Local x:Int = 0 Until mapheight
If map[x][y] = 1
SetColor 155,155,155
DrawRect x*tilewidth,y*tileheight,tilewidth,tileheight
End If
If sx = x And sy = y
SetColor 255,0,0
DrawOval x*tilewidth+10,y*tileheight,10,10
End If
If ex = x And ey = y
SetColor 255,255,0
DrawOval x*tilewidth+10,y*tileheight,10,10
End If
Next
Next
End Method
End Class
Class node
Field x:Int
Field y:Int
Field parentx:Int
Field parenty:Int
Method New(x:Int,y:Int,parentx:Int,parenty:Int)
Self.x = x
Self.y = y
Self.parentx = parentx
Self.parenty = parenty
End Method
End Class
Class path
Field x:Int
Field y:Int
Method New(x:Int,y:Int)
Self.x = x
Self.y = y
End Method
End Class
Class MyGame Extends App
Field pathfound:Bool=False
Field mysearch:search
Field cnt:Int=0
Method OnCreate()
Seed = GetDate[5] * GetDate[4]
SetUpdateRate(10)
mysearch = New search(DeviceWidth,DeviceHeight,10,10)
pathfound = mysearch.search(2,2,6,6)
End Method
Method OnUpdate()
cnt+=1
If KeyHit(KEY_SPACE) Or cnt>15
pathfound=False
cnt=0
Local w:Int=Rnd(10,50)
mysearch = New search(DeviceWidth,DeviceHeight,w,w)
pathfound = mysearch.search(Rnd(w),Rnd(w),Rnd(w),Rnd(w))
End If
End Method
Method OnRender()
Cls 0,0,0
mysearch.drawclosedlist
mysearch.draw
mysearch.drawpath
SetColor 255,255,255
If pathfound
DrawText "Path Found",0,0
End If
End Method
End Class
Function Main()
New MyGame()
End Function