-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze-solver.rkt
41 lines (37 loc) · 1.52 KB
/
maze-solver.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
#lang racket
(require "./point.rkt")
(define (maze-solver maze agenda)
((agenda 'add!) (cons #f ((maze 'find-square) 'start)))
; return step procedure; it will return #f if it is not done
; or a lambda resolving to #t (reachable) or #f
; (too used to JS promises lol)
(lambda ()
(if ((agenda 'empty?)) (lambda () #f)
(let* ((entry ((agenda 'remove!)))
(loc (cdr entry))
(square ((maze 'get-square) (px loc) (py loc))))
(cond (((square 'get) 'explored) #f)
((equal? ((square 'type)) 'finish)
(lambda () (car entry)))
(else
((square 'set!) 'previous (car entry))
(define (consider-next x y)
(when
(not
(equal?
((((maze 'get-square) x y)
'type))
'wall))
((agenda 'add!)
(cons loc (point x y)))))
(for-each
(lambda (offset)
(consider-next (+ (px loc) (px offset))
(+ (py loc) (py offset))))
'((1 . 0) (-1 . 0) (0 . 1) (0 . -1)))
(when (equal? ((square 'type)) 'teleport)
(let ((next-loc ((square 'get) 'complement)))
(consider-next (px next-loc) (py next-loc))))
((square 'set!) 'explored #t)
#f))))))
(provide maze-solver)