# HG changeset patch # User Steve Losh # Date 1463781596 0 # Node ID e63077fb7d6a4f348a516153bef9c0ae9209e246 # Parent 800b4dcae88c8a7402bc91cbdd29a601b22edf47 Binary tree generation diff -r 800b4dcae88c -r e63077fb7d6a package.lisp --- a/package.lisp Fri May 20 10:52:46 2016 +0000 +++ b/package.lisp Fri May 20 21:59:56 2016 +0000 @@ -6,7 +6,9 @@ (:export #:dividesp #:in-context + #:random-elt #:zap% + #:full-list #:%)) (defpackage #:mazes.fps @@ -35,12 +37,18 @@ #:cell-linked-east-p #:cell-linked-west-p #:cell-neighbors + #:cell-north + #:cell-south + #:cell-east + #:cell-west #:grid #:grid-ref #:make-grid #:grid-size #:grid-map-cells #:grid-map-rows + #:grid-loop-cells + #:grid-loop-rows #:grid-size #:grid-random-cell)) @@ -49,13 +57,16 @@ #:cl #:mazes.quickutils #:mazes.utils - #:mazes.grid)) + #:mazes.grid) + (:export + #:gen-binary-tree)) (defpackage #:mazes.demo (:use #:cl #:sketch #:mazes.grid + #:mazes.generation #:mazes.quickutils #:mazes.utils #:mazes.fps)) diff -r 800b4dcae88c -r e63077fb7d6a src/demo.lisp --- a/src/demo.lisp Fri May 20 10:52:46 2016 +0000 +++ b/src/demo.lisp Fri May 20 21:59:56 2016 +0000 @@ -135,4 +135,7 @@ ;;;; Run +(defparameter g (make-grid 7 5)) +(gen-binary-tree g) + ; (defparameter *demo* (make-instance 'demo)) diff -r 800b4dcae88c -r e63077fb7d6a src/generation.lisp --- a/src/generation.lisp Fri May 20 10:52:46 2016 +0000 +++ b/src/generation.lisp Fri May 20 21:59:56 2016 +0000 @@ -1,1 +1,8 @@ (in-package #:mazes.generation) + +(defun gen-binary-tree (grid) + (grid-loop-cells cell grid + (let ((other (random-elt (full-list (cell-north cell) + (cell-east cell))))) + (when other + (cell-link cell other))))) diff -r 800b4dcae88c -r e63077fb7d6a src/grid.lisp --- a/src/grid.lisp Fri May 20 10:52:46 2016 +0000 +++ b/src/grid.lisp Fri May 20 21:59:56 2016 +0000 @@ -49,7 +49,7 @@ (defun cell-neighbors (cell) (with-slots (north south east west) cell - (remove-if #'null (list north south east west)))) + (full-list north south east west))) (defmethod print-object ((cell cell) stream) diff -r 800b4dcae88c -r e63077fb7d6a src/utils.lisp --- a/src/utils.lisp Fri May 20 10:52:46 2016 +0000 +++ b/src/utils.lisp Fri May 20 21:59:56 2016 +0000 @@ -36,3 +36,12 @@ "Return whether `n` is evenly divisible by `divisor`." (zerop (mod n divisor))) + +(defun random-elt (seq) + (let ((length (length seq))) + (if (zerop length) + (values nil nil) + (values (elt seq (random length)) t)))) + +(defun full-list (&rest args) + (remove-if #'null args))