labyrinth

Discussion of Common Lisp
Post Reply
boussarhane
Posts: 1
Joined: Fri Jan 06, 2012 5:39 am

labyrinth

Post by boussarhane » Fri Jan 06, 2012 5:47 am

hi , first of all im not a native english speaker

i wanna know ho to implement a labyrinth in lisp?

think u

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: labyrinth

Post by edgar-rft » Fri Jan 06, 2012 6:42 pm

A simple labyrinth (maze) like this:

Code: Select all

        north
  +-----------+---+
  |           |   |
  |   +-------+   |       Lisp direction-keywords
  |   |           | 
  |   +   +-------+       :n = north
w |       |       | e
e |   +---+   +   | a     :s = south
s |   |       |   | s
t |   +---+---+   | t     :e = east
  |       |       |
  +---+   |   +   |       :w = west
  |       |   |   |
  |   +---+   |   |
  |           |   |
  +-----------+---+
        south
could be implemented as a 2-dimensional array containing lists of direction-keywords:

Code: Select all

(defparameter *maze*
  (make-array '(7 4) :initial-contents
    '(((:s :e)    (:e :w)  (:w)     (:s))
      ((:n :s)    (:s :e)  (:e :w)  (:n :w))
      ((:n :s :e) (:n :w)  (:s :e)  (:s :w))
      ((:n :s)    (:e)     (:n :w)  (:n :s))
      ((:n :e)    (:s :w)  (:s :e)  (:n :s :w))
      ((:s :e)    (:n :w)  (:n :s)  (:n :s))
      ((:n :e)    (:e :w)  (:n :w)  (:n)))))
to find out if it is possible to move from a given field in a given direction you could use a function like this:

Code: Select all

(defun move-possible-p (y x direction-keyword)
  (find direction-keyword (aref *maze* y x)))
Is this what you wanted?

- edgar

abu
Posts: 7
Joined: Thu Feb 18, 2010 3:51 am
Location: Augsburg, Germany
Contact:

Re: labyrinth

Post by abu » Thu Jan 12, 2012 10:08 am


Post Reply