-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rkt
296 lines (266 loc) · 8.96 KB
/
main.rkt
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#lang racket/gui
(require "./stack.rkt")
(require "./queue.rkt")
(require "./maze.rkt")
(require "./maze-solver.rkt")
(require "./point.rkt")
(require "./a-star.rkt")
(require "./square-num.rkt")
; holds maze data (so it can restart without reloading)
(define maze-data "#####
#o.*#
#####")
; holds default maze
(define maze (make-maze maze-data))
; stepping procedure (if available)
(define step #f)
; name of solution type
(define solution-mode "")
; traceable path and its maze if found
(define path #f)
; render maze
(define square-size 20)
(define dot-diameter 6)
(define (render-maze canvas dc)
; set canvas size based on maze size
(send canvas min-width (* ((maze 'width)) square-size))
(send canvas min-height (* ((maze 'height)) square-size))
; set styles
(send dc clear)
(send dc set-pen "white" 1 'transparent)
; render maze
(for ([y (in-naturals)]
[row ((maze 'array))])
(for ([x (in-naturals)]
[square row])
(send dc set-brush ((square 'colour)) 'solid)
(send dc draw-rectangle
(* x square-size)
(* y square-size)
square-size
square-size)
(when ((square 'get) 'explored)
(send dc set-brush (make-color 0 0 0 0.5) 'solid)
(send dc draw-rectangle
(* x square-size)
(* y square-size)
square-size
square-size))))
; render path dots (if it exists)
(send dc set-brush (make-color 240 227 190) 'solid)
(define (iter prev-square)
(when prev-square
(let ((square
((maze 'get-square) (px prev-square) (py prev-square))))
(send dc draw-ellipse
(+ (* (px prev-square) square-size)
(/ (- square-size dot-diameter) 2))
(+ (* (py prev-square) square-size)
(/ (- square-size dot-diameter) 2))
dot-diameter
dot-diameter)
(iter ((square 'get) 'previous)))))
(when (and path (equal? (cdr path) maze))
(iter (car path))))
; sets maze to the parsed maze data from the given file path
(define (load-from file-name)
(set! maze-data
(port->string
(open-input-file file-name #:mode 'text)
#:close? #t))
(set! maze (make-maze maze-data))
(complement-teleporters)
(send status set-label "Loaded maze.")
(send canvas on-paint))
; sets complements of teleporters in maze;
; in case there's more than 2 teleporters, one will just point
; to the next arbitrarily
(define (complement-teleporters)
(let ((teleporters ((maze 'find-squares) 'teleport)))
(define (iter next-tps)
(let* ((next-tp (car next-tps))
(square ((maze 'get-square) (px next-tp) (py next-tp))))
(if (null? (cdr next-tps))
((square 'set!) 'complement (car teleporters))
(begin
((square 'set!) 'complement (cadr next-tps))
(iter (cdr next-tps))))))
(when (not (null? teleporters))
(iter teleporters))))
; when step button is called
(define (run-step)
(when step
(let ((result (step)))
(send status set-label
(if result
(let ((traceable-path (result)))
(set! step #f)
(set! path (cons traceable-path maze))
; NOTE: make sure this is the last statement
; here so it returns a string
(if traceable-path
"Solution complete: finish reachable"
"Solution complete: finish not reachable"))
(string-append solution-mode " in progress")))
(send canvas on-paint))))
; ---- GUI stuff ----
; make new frame
(define frame
(new frame%
[label "Maze"]))
; wrapper for loading file
(define load-row (new horizontal-panel% [parent frame]))
; make new text field for maze file name
(define text-field
(new text-field%
[parent load-row]
[label "Maze data file name"]
[callback
(lambda (field event)
(when (equal? (send event get-event-type) 'text-field-enter)
(load-from (send text-field get-value))))]))
(send text-field focus)
; make load button
(define load-btn
(new button%
[parent load-row]
[label "Load"]
[callback
(lambda (button event)
(load-from (send text-field get-value)))]))
; wrapper for the stack/queue buttons
(define solver-row (new horizontal-panel% [parent frame]))
; start (stack) button: uses a stack to determine whether there's a path to finish
(define start-stack-btn
(new button%
[parent solver-row]
[label "Start (stack)"]
[callback
(lambda (button event)
(set! maze (make-maze maze-data))
(complement-teleporters)
(set! step (maze-solver maze (make-stack)))
(set! solution-mode "Stack-based solution")
(run-step))]))
; start (queue) button: uses a queue to determine whether there's a path to finish
(define start-queue-btn
(new button%
[parent solver-row]
[label "Start (queue)"]
[callback
(lambda (button event)
(set! maze (make-maze maze-data))
(complement-teleporters)
(set! step (maze-solver maze (make-queue)))
(set! solution-mode "Queue-based solution")
(run-step))]))
; wrapper for the A* buttons
(define a-start-row (new horizontal-panel% [parent frame]))
; zero button: uses A* but with h(X) = 0
; so it's basically as good as one of the above two
(define zero-btn
(new button%
[parent a-start-row]
[label "Zero"]
[callback
(lambda (button event)
(set! maze (make-maze maze-data))
(complement-teleporters)
(set! step (a-star maze (lambda (x y) 0)))
(set! solution-mode "A* with h(X) = 0")
(run-step))]))
; euclidean button: uses A* with h(X) = E(X)
; THAT IS, it uses euclidean distance (pythagorean theorem)
(define euclidean-btn
(new button%
[parent a-start-row]
[label "Euclidean"]
[callback
(lambda (button event)
(set! maze (make-maze maze-data))
(complement-teleporters)
(define finish-loc ((maze 'find-square) 'finish))
(set! step
(a-star maze
(lambda (x y)
(sqrt (+ (square (- (px finish-loc) x))
(square (- (py finish-loc) y)))))))
(set! solution-mode "A* with h(X) = E(X)")
(run-step))]))
; manhattan button: uses A* with h(X) = M(X)
; THAT IS, it uses manhattan distance
(define manhattan-btn
(new button%
[parent a-start-row]
[label "Manhattan"]
[callback
(lambda (button event)
(set! maze (make-maze maze-data))
(complement-teleporters)
(define finish-loc ((maze 'find-square) 'finish))
(set! step
(a-star maze
(lambda (x y)
(+ (abs (- (px finish-loc) x))
(abs (- (py finish-loc) y))))))
(set! solution-mode "A* with h(X) = M(X)")
(run-step))]))
; proximity button: uses A* with h(X) = M(X) < 8 ? M(X) : 8
; so it can only see up to a certain distance, simulating a "proximity sensor"
(define proximity-btn
(new button%
[parent a-start-row]
[label "Proximity"]
[callback
(lambda (button event)
(set! maze (make-maze maze-data))
(complement-teleporters)
(define finish-loc ((maze 'find-square) 'finish))
(set! step
(a-star maze
(lambda (x y)
(min (+ (abs (- (px finish-loc) x))
(abs (- (py finish-loc) y)))
8))))
(set! solution-mode "A* with h(X) = M(X) but only up to 8")
(run-step))]))
; wrapper for the playback buttons
(define playback-row (new horizontal-panel% [parent frame]))
; make step button
(define step-btn
(new button%
[parent playback-row]
[label "Step"]
[callback (lambda (button event) (run-step))]))
; initialize current timer
(define timer #f)
; make toggle animation button
(define toggle-anim-btn
(new button%
[parent playback-row]
[label "Enable animation"]
[callback
(lambda (button event)
(send toggle-anim-btn set-label
(if timer "Enable animation" "Disable animation"))
(if timer
(begin
(send timer stop)
(set! timer #f))
(set! timer (new timer%
[notify-callback run-step]
[interval 50]
[just-once? #f]))))]))
; make status text
(define status
(new message%
[parent frame]
[label "Default maze"]
[auto-resize #t]))
; make canvas where the maze will be rendered
(define canvas
(new canvas%
[parent frame]
[paint-callback render-maze]))
; show frame
(send frame show #t)