Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
30
Task/Maze-generation/Racket/maze-generation-1.rkt
Normal file
30
Task/Maze-generation/Racket/maze-generation-1.rkt
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#lang racket
|
||||
|
||||
;; the structure representing a maze of size NxM
|
||||
(struct maze (N M tbl))
|
||||
|
||||
;; managing cell properties
|
||||
(define (connections tbl c) (dict-ref tbl c '()))
|
||||
|
||||
(define (connect! tbl c n)
|
||||
(dict-set! tbl c (cons n (connections tbl c)))
|
||||
(dict-set! tbl n (cons c (connections tbl n))))
|
||||
|
||||
(define (connected? tbl a b) (member a (connections tbl b)))
|
||||
|
||||
;; Returns a maze of a given size
|
||||
;; build-maze :: Index Index -> Maze
|
||||
(define (build-maze N M)
|
||||
(define tbl (make-hash))
|
||||
(define (visited? tbl c) (dict-has-key? tbl c))
|
||||
(define (neigbours c)
|
||||
(filter
|
||||
(match-lambda [(list i j) (and (<= 0 i (- N 1)) (<= 0 j (- M 1)))])
|
||||
(for/list ([d '((0 1) (0 -1) (-1 0) (1 0))]) (map + c d))))
|
||||
; generate the maze
|
||||
(let move-to-cell ([c (list (random N) (random M))])
|
||||
(for ([n (shuffle (neigbours c))] #:unless (visited? tbl n))
|
||||
(connect! tbl c n)
|
||||
(move-to-cell n)))
|
||||
; return the result
|
||||
(maze N M tbl))
|
||||
19
Task/Maze-generation/Racket/maze-generation-2.rkt
Normal file
19
Task/Maze-generation/Racket/maze-generation-2.rkt
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
;; Shows a maze
|
||||
(define (show-maze m)
|
||||
(match-define (maze N M tbl) m)
|
||||
(for ([i N]) (display "+---"))
|
||||
(displayln "+")
|
||||
(for ([j M])
|
||||
(display "|")
|
||||
(for ([i (- N 1)])
|
||||
(if (connected? tbl (list i j) (list (+ 1 i) j))
|
||||
(display " ")
|
||||
(display " |")))
|
||||
(display " |")
|
||||
(newline)
|
||||
(for ([i N])
|
||||
(if (connected? tbl (list i j) (list i (+ j 1)))
|
||||
(display "+ ")
|
||||
(display "+---")))
|
||||
(displayln "+"))
|
||||
(newline))
|
||||
Loading…
Add table
Add a link
Reference in a new issue