Isoraqathedh

betza wip 3

Mar 6th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 9.66 KB | None | 0 0
  1. ;;;; Betza Notation
  2.  
  3. ;;; ========
  4. ;;; Classes
  5. ;;; ========
  6.  
  7. ;; Range
  8. ;; ======
  9. ;; Range moves are moves that repeat themselves. For instance
  10.  
  11. (defclass range ()
  12.   ((base-vector :initarg :destination
  13.         :initform '(0 . 0)
  14.         :reader base-vector)
  15.    (length :initarg :max-steps
  16.        :initform :infinite
  17.        :reader max-steps)))
  18.  
  19. (defmethod print-object ((object range) stream)
  20.   (print-unreadable-object (object stream :type t)
  21.     (with-slots (base-vector length) object
  22.       (flet ((multiplier (number)
  23.            (cond
  24.          ((= number -1) (format nil "-n"))
  25.          ((= number  0) (format nil "0"))
  26.          ((= number  1) (format nil "n"))
  27.          (t             (format nil "~dn" number)))))
  28.     (format stream "~a, ~a --|~a"
  29.         (multiplier (car base-vector))
  30.         (multiplier (cdr base-vector))
  31.         length)))))
  32.  
  33. (defgeneric range= (range-1 range-2)
  34.   (:documentation "Checks if two ranges are equal."))
  35.  
  36. (defmethod range= ((range-1 range) (range-2 range))
  37.   (with-slots ((base-vector-1 base-vector) (length-1 length)) range-1
  38.     (with-slots ((base-vector-2 base-vector) (length-2 length)) range-2
  39.       (and (equal base-vector-1 base-vector-2)
  40.        (= length-1 length-2)))))
  41.  
  42. (defgeneric range-include (range-or-single-square range-2)
  43.   (:documentation "Checks if one range completely eclipses another, or if a single-square is included in a range"))
  44.  
  45. (defmethod range-include ((range-or-single-square range) (range-2 range))
  46.   "Checks if the two ranges overlap each other, return T if it does.
  47.   Additionally, returns :second-eclipse-first if the second one wholly contains the first one,
  48.   :first-eclipse-second if the first one wholly contains the second one,
  49.   and :identity if both of them are the same thing (equivalent to a T from range=)."
  50.   ;; for range (an, bn --> f) and (pn, qn --> g)
  51.   ;; if this is true:
  52.   ;;   a = p, b = q, f < g
  53.   ;; then (an, bn --> f) is in (pn, qn --> g)
  54.   (let ((a (car (base-vector range-or-single-square)))
  55.     (b (cdr (base-vector range-or-single-square)))
  56.     (f (max-steps range-or-single-square))
  57.     (p (car (base-vector range-2)))
  58.     (q (cdr (base-vector range-2)))
  59.     (g (max-steps range-2)))
  60.     (if (and (= a p) (= b q))
  61.     (cond
  62.       ((or (and (not (eql f :infinite)) (eql g :infinite)) (< f g)) (values T :second-eclipse-first))
  63.       ((or (and (not (eql g :infinite)) (eql f :infinite)) (> f g)) (values T :first-eclipse-second))
  64.       (t (values T :identity))))))
  65. )
  66.   ;; for cell (a, b) in range (pn, qn)
  67.   ;; (a, b) is in (pn, qn)1  ;; if any of these are true:
  68.   ;;   a = b = p = q = 0
  69.   ;;   a = p = 0 and b mod q = 0 and b/q > 0
  70.   ;;   b = q = 0 and a mod p = 0 and a/p > 0
  71.   ;;   None of a, b, p or q = 0 and a mod p = 0 and b mod q = 0 and a/p = b/q and a/p > 0 and b/q > 0
  72.  
  73. (defgeneric get-nth-in-range (range nth)
  74.   (:documentation "Gets the nth square of a range."))
  75.  
  76. (defmethod get-nth-in-range ((range range) (nth number))
  77.   (with-slots (base-vector length) range
  78.     (if (and (not (eql length :infinite)) (< length nth))
  79.     (restart-case (error (format nil "Range is not long enough to get to cell number ~d" nth))
  80.       (use-value (value) value)
  81.       (clamp-to-max ()
  82.         :report "Return the furthest permissible destination."
  83.         (get-nth-in-range range length))
  84.       (return-nil ()
  85.         :report "Return NIL."
  86.         nil)
  87.       (return-zero ()
  88.         :report "Return the destination of a Zero."
  89.         (dest 0 0)))
  90.     (dest (* nth (car base-vector)) (* nth (cdr base-vector))))))
  91.  
  92.  
  93. ;;;------
  94.  
  95. (defun dest (x y)
  96.   (if (and (numberp x)
  97.        (numberp y))
  98.       (cons x y)
  99.       (error "Destination must be a number")))
  100.  
  101. ;;;------
  102.  
  103. (defun zero () (dest 0 0))
  104. ;; Orthogonally-jumping pieces
  105. ;; =====
  106.  
  107. ;; Generic orthogonal
  108. (defun orthogonal-jump (&key (directions '(:f :b :r :l)) (jump-distance 1))
  109.   (let ((vectors (list :r (dest (+ jump-distance) 0)
  110.                :l (dest (- jump-distance) 0)
  111.                :f (dest 0 (+ jump-distance))
  112.                :b (dest 0 (- jump-distance)))))
  113.     (mapcar #'(lambda (x) (getf vectors x)) directions)))
  114.  
  115. ;; Specialized orthogonal
  116. (defmacro def-orthogonal-jump (name dist)
  117.   `(defun ,name (&key (directions '(:f :b :r :l)))
  118.      (orthogonal-jump :directions directions :jump-distance ,dist)))
  119. (def-orthogonal-jump wazir       1)
  120. (def-orthogonal-jump dabbabah    2)
  121. (def-orthogonal-jump threeleaper 3)
  122.  
  123. ;; Generic rider orthogonal
  124. (defun orthogonal-ride (&key (distance :infinite) (directions '(:f :b :r :l)) (jump-distance 1))
  125.   (let ((used-vectors (orthogonal-jump :directions directions :jump-distance jump-distance)))
  126.     (if (eql distance 1)
  127.     used-vectors
  128.     (mapcar #'(lambda (x) (make-instance 'range :max-steps distance :destination x)) used-vectors))))
  129.  
  130. ;; Specialized rider orthogonal
  131. (defmacro def-orthogonal-range (name dist)
  132.   `(defun ,name (&key (distance :infinite) (directions '(:f :b :r :l)))
  133.      (orthogonal-ride :distance distance :directions directions :jump-distance ,dist)))
  134. (def-orthogonal-range rook              1)
  135. (def-orthogonal-range dabbabah-rider    2)
  136. (def-orthogonal-range threeleaper-rider 3)
  137.  
  138. ;; Diagonally-jumping pieces
  139. ;; =====
  140.  
  141. ;; Generic diagonal
  142. (defun diagonal-jump (&key (directions '(:f :b :r :l)) (jump-distance 1))
  143.   (flet ((expand-diagonals (dir) ;keyword shorthand expansion
  144.        (loop
  145.           for i in dir
  146.           append (case i
  147.                (:f  (list :fl :fr))
  148.                (:b  (list :bl :br))
  149.                (:l  (list :fl :bl))
  150.                (:r  (list :fr :br))
  151.                (:fl (list :fl))
  152.                (:bl (list :bl))
  153.                (:br (list :br))
  154.                (:fr (list :fr))))))
  155.     (let ((vectors (list :fr (dest (+ jump-distance) (+ jump-distance))
  156.              :fl (dest (- jump-distance) (+ jump-distance))
  157.              :br (dest (+ jump-distance) (- jump-distance))
  158.              :bl (dest (- jump-distance) (- jump-distance)))))
  159.       (remove-duplicates (mapcar #'(lambda (x) (getf vectors x)) (expand-diagonals directions))))))
  160.  
  161. ;; Specialized diagonal
  162. (defmacro def-diagonal-jump (name dist)
  163.   `(defun ,name (&key (directions '(:f :b :r :l)))
  164.      (diagonal-jump :directions directions :jump-distance ,dist)))
  165. (def-diagonal-jump ferz    1)
  166. (def-diagonal-jump alfil   2)
  167. (def-diagonal-jump tripper 3)
  168.  
  169. ;; Generic rider diagonal
  170. (defun diagonal-ride (&key (distance :infinite) (directions '(:f :b :r :l)) (jump-distance 1))
  171.   (let ((used-vectors (diagonal-jump :directions directions :jump-distance jump-distance)))
  172.     (if (eql distance 1)
  173.     used-vectors
  174.     (mapcar #'(lambda (x) (make-instance 'range :max-steps distance :destination x)) used-vectors))))
  175.  
  176. ;; Specialized rider orthogonal
  177. (defmacro def-diagonal-range (name dist)
  178.   `(defun ,name (&key (distance :infinite) (directions '(:f :b :r :l)))
  179.      (diagonal-ride :distance distance :directions directions :jump-distance ,dist)))
  180. (def-diagonal-range bishop        1)
  181. (def-diagonal-range alfil-rider   2)
  182. (def-diagonal-range tripper-rider 3)
  183.  
  184. ;; Hippogonal pieces
  185. ;; =====
  186.  
  187. ;; Generic hippogonal
  188. (defun hippo (long-side short-side &key (directions '(:f :b :r :l)))
  189.   (cond
  190.     ;; degenerate cases
  191.     ((< long-side short-side) (hippo short-side long-side :directions directions))
  192.     ((= long-side short-side) (diagonal-jump :directions directions :jump-distance long-side))
  193.     ((zerop short-side)     (orthogonal-jump :directions directions :jump-distance long-side))
  194.     ;; the real thing
  195.     (t (flet ((expand-hippogonals (dir) ;keyword shorthand expansion
  196.         (loop for i in dir
  197.            append (case i
  198.                 (:f   (list :ffr :ffl :fsr :fsl))
  199.                 (:b   (list :bbr :bbl :bsr :bsl))
  200.                 (:r   (list :ffr :fsr :bsr :bbr))
  201.                 (:l   (list :ffl :fsl :bsl :bbl))
  202.                 (:fr  (list :ffr :fsr))
  203.                 (:fl  (list :ffl :fsl))
  204.                 (:br  (list :bbr :bsr))
  205.                 (:bl  (list :bbl :bsl))
  206.                 (:ff  (list :ffr :ffl))
  207.                 (:fs  (list :fsr :fsl))
  208.                 (:bb  (list :bbr :bbl))
  209.                 (:bs  (list :bsr :bsl))
  210.                 (:ll  (list :bsl :fsl))
  211.                 (:lv  (list :bbl :ffl))
  212.                 (:rr  (list :bsr :fsr))
  213.                 (:rv  (list :bbr :ffr))
  214.                 (:fb  (list :ffl :ffr :bbl :bbr))
  215.                 (:rl  (list :fsl :fsr :bsl :bsr))
  216.                 (:ffr (list :ffr))
  217.                 (:fsr (list :fsr))
  218.                 (:bsr (list :bsr))
  219.                 (:bbr (list :bbr))
  220.                 (:bbl (list :bll))
  221.                 (:bsl (list :bsl))
  222.                 (:fsl (list :fsl))
  223.                 (:ffl (list :ffl))))))
  224.      (let ((vectors (list :ffr (dest (+ short-side) (+ long-side) )
  225.                   :fsr (dest (+ long-side)  (+ short-side))
  226.                   :bsr (dest (+ long-side)  (- short-side))
  227.                   :bbr (dest (+ short-side) (- long-side) )
  228.                   :bbl (dest (- short-side) (- long-side) )
  229.                   :bsl (dest (- long-side)  (- short-side))
  230.                   :fsl (dest (- long-side)  (+ short-side))
  231.                   :ffl (dest (- short-side) (+ long-side)))))
  232.        (remove-duplicates (mapcar #'(lambda (x) (getf vectors x)) (expand-hippogonals directions))))))))
  233.        
  234.  
  235. ;; Specialized hippogonal
  236. (defmacro defhippo (name long short)
  237.   `(defun ,name (&key (directions '(:f :b :r :l)))
  238.      (hippo ,long ,short :directions directions)))
  239. (defhippo knight   2 1)
  240. (defhippo camel    3 1)
  241. (defhippo zebra    3 2)
  242. (defhippo giraffe  4 1)
  243. (defhippo antelope 4 3)
  244.  
  245. ;; Generalized hippogonal ride
  246. (defun hippogonal-ride (long-side short-side &key (distance :infinite) (directions '(:f :b :r :l)))
  247.   (let ((used-vectors (hippo long-side short-side :directions directions)))
  248.     (if (eql distance 1)
  249.     used-vectors
  250.     (mapcar #'(lambda (x) (make-instance 'range :max-steps distance :destination x)) used-vectors))))
  251.  
  252. ;; Specialized rider orthogonal
  253. (defmacro def-hippogonal-range (name long short)
  254.   `(defun ,name (&key (distance :infinite) (directions '(:f :b :r :l)))
  255.      (hippogonal-ride ,long ,short :distance distance :directions directions)))
  256. (def-hippogonal-range nightrider 2 1)
  257. (def-hippogonal-range camelrider 3 1)
  258. (def-hippogonal-range zebrarider 3 2)
Advertisement
Add Comment
Please, Sign In to add comment