Advertisement
krpec

Hanoi towers

Mar 2nd, 2012
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 3.40 KB | None | 0 0
  1. ;;
  2. ;; Hanoi Towers (@author: krpec), made for fun
  3. ;;
  4.  
  5. (defclass hanoi-towers ()
  6.   ((tower1 :initform '())
  7.    (tower2 :initform '())
  8.    (tower3 :initform '())
  9.    (disc-count :initform 0)))
  10.  
  11. (defmethod tower1 ((hanoi-towers hanoi-towers))
  12.   (slot-value hanoi-towers 'tower1))
  13.  
  14. (defmethod set-tower1 ((h hanoi-towers) list)
  15.   (unless (typep list 'list)
  16.     (error "tower1 value should be a list."))
  17.   (setf (slot-value h 'tower1) list)
  18.   h)
  19.  
  20. (defmethod tower2 ((hanoi-towers hanoi-towers))
  21.   (slot-value hanoi-towers 'tower2))
  22.  
  23. (defmethod set-tower2 ((h hanoi-towers) list)
  24.   (unless (typep list 'list)
  25.     (error "tower2 value should be a list."))
  26.   (setf (slot-value h 'tower2) list)
  27.   h)
  28.  
  29. (defmethod tower3 ((hanoi-towers hanoi-towers))
  30.   (slot-value hanoi-towers 'tower3))
  31.  
  32. (defmethod set-tower3 ((h hanoi-towers) list)
  33.   (unless (typep list 'list)
  34.     (error "tower3 value should be a list."))
  35.   (setf (slot-value h 'tower3) list)
  36.   h)
  37.  
  38. (defmethod get-tower ((h hanoi-towers) tower)
  39.   (cond ((= tower 1) (tower1 h))
  40.         ((= tower 2) (tower2 h))
  41.         ((= tower 3) (tower3 h))))
  42.  
  43. (defmethod set-tower ((h hanoi-towers) tower list)
  44.   (cond ((= tower 1) (set-tower1 h list))
  45.         ((= tower 2) (set-tower2 h list))
  46.         ((= tower 3) (set-tower3 h list))))
  47.  
  48. (defmethod disc-count ((h hanoi-towers))
  49.   (slot-value h 'disc-count))
  50.  
  51. (defmethod set-disc-count ((h hanoi-towers) count)
  52.   (unless (typep count 'number)
  53.     (error "Disc count should be a number."))
  54.   (if (< count 3)
  55.       (error "Minimal disc count should be 3.")
  56.     (setf (slot-value h 'disc-count) count))
  57.     h)
  58.  
  59. ;Prepares game "board" with chosen "difficulty" - number of discs
  60. (defmethod prepare-game ((h hanoi-towers) discs)
  61.   (set-disc-count h discs)
  62.   (let ((tower '()))
  63.     (dotimes (x discs)
  64.       (setf tower (append tower
  65.                           (list (+ x 1)))))
  66.     (set-tower1 h tower)
  67.     (set-tower2 h nil)
  68.     (set-tower3 h nil))
  69.   (show-board h))
  70.  
  71. ;prints out game "board"
  72. (defmethod show-board ((h hanoi-towers))
  73.   (let ((t1 (tower1 h))
  74.         (t2 (tower2 h))
  75.         (t3 (tower3 h)))
  76.     (format t
  77.             "~%[1] : ~s~%[2] : ~s~%[3] : ~s~%"
  78.             (if (null t1)
  79.                 "-"
  80.               t1)
  81.             (if (null t2)
  82.                 "-"
  83.               t2)
  84.             (if (null t3)
  85.                 "-"
  86.               t3)))
  87.     h)
  88.  
  89. ;made moves
  90. (defmethod move ((h hanoi-towers) from to)
  91.   (unless (typep from 'number)
  92.     (error "'From' should be a number of a source tower."))
  93.   (unless (typep to 'number)
  94.     (error "'To' should be a number of a destination tower."))
  95.   (let ((source (get-tower h from))
  96.         (dest (get-tower h to)))
  97.     (if (or (null dest)
  98.             (< (car source)
  99.                (car dest)))
  100.         (and (set-tower h to (append (list (car source))
  101.                                      dest))
  102.           (set-tower h from (cdr source)))
  103.       (format t "~%Illegal move.")))
  104.   (show-board h)
  105.   (check-state h))
  106.  
  107. ;checks end of the game
  108. (defmethod check-state ((h hanoi-towers))
  109.   (if (and (= (list-length (tower1 h)) 0)
  110.            (or (= (list-length (tower2 h))
  111.                   (disc-count h))
  112.                (= (list-length (tower3 h))
  113.                   (disc-count h))))
  114.       (format t "~%Game over, congratulations!~%"))
  115.   h)
  116.  
  117. #|
  118. Usage:
  119. (setf g (make-instance 'hanoi-towers))
  120. (prepare-game g 4)
  121. (move g 1 2)
  122. |#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement