Isoraqathedh

Piece

Jun 3rd, 2014
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.53 KB | None | 0 0
  1. (defun directionp (thing)
  2.   (member thing *all-dirs*))
  3.  
  4. (defun piece-function (x y)
  5.   #'(lambda (&rest dirs-and-signatures)
  6.       (make-piece x y
  7.                   (remove-if-not #'directionp dirs-and-signatures)
  8.                   (remove-if #'directionp dirs-and-signatures))))
  9.  
  10. (defparameter *atoms*
  11.   '((:wazir 1 0)
  12.     (:ferz 1 1)
  13.     (:dabbabah 2 0)
  14.     (:knight 2 1)
  15.     (:alfil 2 2)
  16.     (:threeleaper 3 0)
  17.     (:camel 3 1)
  18.     (:zebra 3 2)
  19.     (:tripper 3 3)
  20.     (:fourleaper 4 0)
  21.     (:giraffe 4 1)
  22.     (:doubleknight 4 2)
  23.     (:antelope 4 3))
  24.   "List that holds definitions for all chess pieces.")
  25.  
  26. (defparameter *piece-functions*
  27.   (let ((hash-table (make-hash-table)))
  28.     (dolist (piece *atoms* hash-table)
  29.       (destructuring-bind (piece-name x y) piece
  30.         (setf (gethash piece-name hash-table) (piece-function x y))))
  31.     hash-table)
  32.   "Hash table that holds lambdas for all chess pieces.")
  33.  
  34. (defun piece (name &rest dirs-and-signatures)
  35.   "Creates a simple chess piece based on its name argument.
  36. The names of the basic items or a two-number list is accepted.
  37. Additionally, a single number n is interpreted as the combination of '(n n) and '(n 0)."
  38.     (cond ((member name *atoms* :key #'first)
  39.            (apply (gethash name *piece-functions*) dirs-and-signatures))
  40.           ((numberp name)
  41.            (append (apply (piece-function name 0) dirs-and-signatures)
  42.                    (apply (piece-function name name) dirs-and-signatures)))
  43.           (t (piece-function (first name) (second name)))))
Advertisement
Add Comment
Please, Sign In to add comment