-
Notifications
You must be signed in to change notification settings - Fork 8
/
Brute Force Pathfinding Lookup.monkey2
222 lines (200 loc) · 4.62 KB
/
Brute Force Pathfinding Lookup.monkey2
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
212
213
214
215
216
217
218
219
220
221
222
#Import "<std>"
#Import "<mojo>"
Using std..
Using mojo..
' Large maps require super CPU!! (2020)
Global mapwidth:Int=8
Global mapheight:Int=8
Global tilewidth:Int,tileheight:Int
Global maxpathlen:Int=12
Global path:Int[] = New Int[maxpathlen]
Global map:Int[,]
Class p
Field path:Int[] = New Int[maxpathlen]
Method New(p:Int[])
For Local i:Int=0 Until maxpathlen
path[i] = p[i]
Next
End Method
End Class
Class MyWindow Extends Window
Field lupath:List<p> = New List<p>
Field sx:Int,sy:Int
Field ex:Int,ey:Int
Field ms:Int
Field pathed:Bool=False
Method New()
Title="Lookup Table Brute Force Pathfinding"
tilewidth=Width/mapwidth
tileheight=Height/mapheight
sx = 2
sy = 2
ex = 7
ey = 7
map = New int[mapwidth,mapheight]
Local cnt:Int=0
path = New Int[maxpathlen]
For Local i:Int=0 Until maxpathlen
path[i]=-1
Next
Local exitloop:Bool=False
While exitloop=False
lupath.Add(New p(path))
exitloop = True
For Local i:Int=1 Until maxpathlen-1
If path[i] = -1 Then
exitloop=False
Exit
End If
Next
incpath()
'cnt+=1
If Rnd(15000)<1 Then Print Millisecs()
Wend
End Method
Method OnRender( canvas:Canvas ) Override
App.RequestRender() ' Activate this method
drawgrid(canvas)
If pathed = True Then drawpath(canvas)
If Keyboard.KeyReleased(Key.Space)
ms = Millisecs()
Local cnt:Int=0
For Local i:=Eachin lupath
For Local ii:Int=0 Until maxpathlen
path[ii] = i.path[ii]
Next
Local s:Bool=pathfound()
If s=True Then
Print "Milliseconds taken : "+(Millisecs()-ms)
pathed = True
Exit
End If
cnt+=1
' timeout after xxxxx millisecs
If ms+10000<Millisecs() Then
Print "failed - timeout"
pathed=False
Exit
End If
Next
End If
Local tx:Int=Mouse.X/tilewidth
Local ty:Int=Mouse.Y/tileheight
If Mouse.ButtonReleased(MouseButton.Middle)
If (tx=sx And ty=sy) Or (tx=ex And ty=ey)
Else
If map[tx,ty] = 1
map[tx,ty] = 0
Else
map[tx,ty] = 1
End If
End if
End If
If Mouse.ButtonReleased(MouseButton.Left)
If map[tx,ty] = 0
sx = tx
sy = ty
pathed=False
End If
End If
If Mouse.ButtonReleased(MouseButton.Right)
If map[tx,ty] = 0
ex = tx
ey = ty
pathed=False
End If
End If
canvas.Color = Color.Black
canvas.DrawText("LMB(start)/RMB(end)/SPACE(findpath)MMB(wall)",0,0)
If Keyboard.KeyReleased(Key.Escape) Then App.Terminate()
End Method
Method pathfound:Bool()
Local cx:Int=sx
Local cy:Int=sy
For Local i:Int=maxpathlen-1 Until 0 Step -1
Select path[i]
Case 0'up
cy-=1
Case 1'right
cx+=1
Case 2'down
cy+=1
Case 3'left
cx-=1
Default'exit
Return False
End Select
If cx<0 Then Return False
If cx>=mapwidth Then Return False
If cy<0 Then Return False
If cy>=mapheight Then Return False
If map[cx,cy] = 1 Then Return False
If ex = cx And ey = cy Then
Print "found"
Return True
Endif
Next
Return False
End Method
Method incpath()
path[maxpathlen-1]+=1
For Local i:Int=maxpathlen-1 Until 1 Step-1
If path[i] > 3 Then
path[i] = 0
path[i-1] += 1
Endif
Next
End Method
Method drawgrid(canvas:Canvas)
For Local y:Int=0 Until mapheight
For Local x:Int=0 Until mapwidth
canvas.Color = Color.Black
canvas.DrawRect(x*tilewidth,y*tileheight,tilewidth,tileheight)
canvas.Color = Color.White
canvas.DrawRect(x*tilewidth+1,y*tileheight+1,tilewidth-1,tileheight-1)
If map[x,y] = 1 Then
canvas.Color = Color.Magenta
canvas.DrawRect(x*tilewidth,y*tileheight,tilewidth,tileheight)
End If
If x=sx And y = sy Then
canvas.Color = Color.Green
canvas.DrawRect(x*tilewidth,y*tileheight,tilewidth,tileheight)
canvas.Color = Color.Red
canvas.DrawText("start LMB",x*tilewidth,y*tileheight)
End If
If x=ex And y = ey Then
canvas.Color = Color.Yellow
canvas.DrawRect(x*tilewidth,y*tileheight,tilewidth,tileheight)
canvas.Color = Color.Red
canvas.DrawText("end RMB",x*tilewidth,y*tileheight)
End If
Next
Next
End Method
Method drawpath(canvas:Canvas)
canvas.Color = Color.Red
Local cx:Int=sx
Local cy:Int=sy
For Local i:Int=maxpathlen-1 Until 0 Step -1
Select path[i]
Case 0'up
cy-=1
Case 1'right
cx+=1
Case 2'down
cy+=1
Case 3'left
cx-=1
End Select
canvas.DrawCircle(cx*tilewidth+tilewidth/2,cy*tileheight+tileheight/2,10)
If ex = cx And ey = cy Then
Return
Endif
Next
End Method
End Class
Function Main()
New AppInstance
New MyWindow
App.Run()
End Function