e63077fb7d6a

Binary tree generation
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 20 May 2016 21:59:56 +0000
parents 800b4dcae88c
children 901e9fc8958d
branches/tags (none)
files package.lisp src/demo.lisp src/generation.lisp src/grid.lisp src/utils.lisp

Changes

--- 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))
--- 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))
--- 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)))))
--- 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)
--- 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))