Advertisement
zbeucler

grid-search.ss

Feb 18th, 2021
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. ; grid-search.ss
  2.  
  3. (define none -1)
  4. (define n 0)
  5. (define s 1)
  6. (define e 2)
  7. (define w 3)
  8.  
  9. (define search
  10. (lambda (grid stop-count)
  11. (search2 grid 1 stop-count)))
  12.  
  13. (define search2
  14. (lambda (grid count stop-count)
  15. (pause pause-num)
  16. (let ((x (robot-x))
  17. (y (robot-y)))
  18. (display count)
  19. (newline)
  20. ; (draw-visited x y)
  21. (move-robot grid x y 0)
  22. (draw-moved-robot (robot-x) (robot-y))
  23. (if (or
  24. (and (= x (robot-x)) (= y (robot-y)))
  25. (equal? robot goal)
  26. (>= count stop-count))
  27. #f
  28. ;else
  29. (search2 grid (+ count 1) stop-count)))))
  30.  
  31.  
  32. (define move-robot
  33. (lambda (grid x y count)
  34. (let ((dir (random 4)))
  35. (cond
  36. ((and (= dir n) (> x 0) (< (get-node grid (- x 1) y) obstacle))
  37. (set! robot (list (- x 1) y)))
  38. ((and (= dir s) (< x (- num-col-row 1)) (< (get-node grid (+ x 1) y) obstacle))
  39. (set! robot (list (+ x 1) y)))
  40. ((and (= dir w) (> y 0) (< (get-node grid x (- y 1)) obstacle))
  41. (set! robot (list x (- y 1))))
  42. ((and (= dir e) (< y (- num-col-row 1)) (< (get-node grid x (+ y 1)) obstacle))
  43. (set! robot (list x (+ y 1))))
  44. ((> count 100)
  45. (move-any-dir grid x y))
  46. (else
  47. (move-robot grid x y (+ count 1)))))))
  48.  
  49. (define move-any-dir
  50. (lambda (grid x y)
  51. (cond
  52. ((and (> x 0) (< (get-node grid (- x 1) y) obstacle))
  53. (set! robot (list (- x 1) y)))
  54. ((and (< x (- num-col-row 1)) (< (get-node grid (+ x 1) y) obstacle))
  55. (set! robot (list (+ x 1) y)))
  56. ((and (> y 0) (< (get-node grid x (- y 1)) obstacle))
  57. (set! robot (list x (- y 1))))
  58. ((and (< y (- num-col-row 1)) (< (get-node grid x (+ y 1)) obstacle))
  59. (set! robot (list x (+ y 1))))
  60. (else
  61. (display "no move")))))
  62.  
  63. (define pause
  64. (lambda (count)
  65. (if (<= count 0)
  66. 0
  67. ;else
  68. (pause (- count 1)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement