hanoi.el 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ;
  2. ; hanoi - towers of hanoi in GNUmacs
  3. ;
  4. ; Author (a) 1985, Damon Anton Permezel
  5. ;
  6. ;;;
  7. ;;; hanoi-topos - direct cursor addressing
  8. ;;;
  9. (defun hanoi-topos (row col)
  10. (goto-line row)
  11. (beginning-of-line)
  12. (forward-char col))
  13. ;;;
  14. ;;; hanoi - user callable Towers of Hanoi
  15. ;;;
  16. (defun hanoi (nrings)
  17. "Towers of Hanoi diversion"
  18. (interactive
  19. (list (if (null current-prefix-arg)
  20. 3
  21. (prefix-numeric-value current-prefix-arg))))
  22. (if (<= nrings 0) (error "Negative number of rings"))
  23. (let (pole-spacing
  24. floor-row
  25. fly-row
  26. (window-height (window-height (selected-window)))
  27. (window-width (window-width (selected-window))))
  28. (let ((h (+ nrings 2))
  29. (w (+ (* (1- nrings) 6) 2 5)))
  30. (if (not (and (>= window-width h)
  31. (> window-width w)))
  32. (progn
  33. (delete-other-windows)
  34. (if (not (and (>= (setq window-height
  35. (window-height (selected-window))) h)
  36. (> (setq window-width
  37. (window-width (selected-window))) w)))
  38. (error "Screen is too small (need at least %dx%d)" w h))))
  39. (setq pole-spacing (/ window-width 6))
  40. (if (not (zerop (logand pole-spacing 1)))
  41. ;; must be even
  42. (setq pole-spacing (1+ pole-spacing)))
  43. (setq floor-row (if (> (- window-height 3) h)
  44. (- window-height 3) window-height)))
  45. (let ((fly-row (- floor-row nrings 1))
  46. ;; pole: column . fill height
  47. (pole-1 (cons pole-spacing floor-row))
  48. (pole-2 (cons (* 3 pole-spacing) floor-row))
  49. (pole-3 (cons (* 5 pole-spacing) floor-row))
  50. (rings (make-vector nrings nil)))
  51. ;; construct the ring list
  52. (let ((i 0))
  53. (while (< i nrings)
  54. ;; ring: [pole-number string empty-string]
  55. (aset rings i (vector nil
  56. (make-string (+ i i 3) (+ ?0 i))
  57. (make-string (+ i i 3) ?\ )))
  58. (setq i (1+ i))))
  59. ;;
  60. ;; init the screen
  61. ;;
  62. (switch-to-buffer "*Hanoi*")
  63. (setq buffer-read-only nil)
  64. (buffer-flush-undo (current-buffer))
  65. (erase-buffer)
  66. (let ((i 0))
  67. (while (< i floor-row)
  68. (setq i (1+ i))
  69. (insert-char ?\ (1- window-width))
  70. (insert ?\n)))
  71. (insert-char ?= (1- window-width))
  72. (let ((n 1))
  73. (while (< n 6)
  74. (hanoi-topos fly-row (* n pole-spacing))
  75. (setq n (+ n 2))
  76. (let ((i fly-row))
  77. (while (< i floor-row)
  78. (setq i (1+ i))
  79. (next-line 1)
  80. (insert ?\|)
  81. (delete-char 1)
  82. (backward-char 1)))))
  83. ;(sit-for 0)
  84. ;;
  85. ;; now draw the rings in their initial positions
  86. ;;
  87. (let ((i 0)
  88. ring)
  89. (while (< i nrings)
  90. (setq ring (aref rings (- nrings 1 i)))
  91. (aset ring 0 (- floor-row i))
  92. (hanoi-topos (cdr pole-1)
  93. (- (car pole-1) (- nrings i)))
  94. (hanoi-draw-ring ring t nil)
  95. (setcdr pole-1 (1- (cdr pole-1)))
  96. (setq i (1+ i))))
  97. (setq buffer-read-only t)
  98. (sit-for 0)
  99. ;;
  100. ;; do it!
  101. ;;
  102. (hanoi0 (1- nrings) pole-1 pole-2 pole-3)
  103. (goto-char (point-min))
  104. (message "Done")
  105. (setq buffer-read-only t)
  106. (set-buffer-modified-p (buffer-modified-p))
  107. (sit-for 0))))
  108. ;;;
  109. ;;; hanoi0 - work horse of hanoi
  110. ;;;
  111. (defun hanoi0 (n from to work)
  112. (cond ((input-pending-p)
  113. (signal 'quit (list "I can tell you've had enough")))
  114. ((< n 0))
  115. (t
  116. (hanoi0 (1- n) from work to)
  117. (hanoi-move-ring n from to)
  118. (hanoi0 (1- n) work to from))))
  119. ;;;
  120. ;;; hanoi-move-ring - move ring 'n' from 'from' to 'to'
  121. ;;;
  122. ;;;
  123. (defun hanoi-move-ring (n from to)
  124. (let ((ring (aref rings n)) ; ring <- ring: (ring# . row)
  125. (buffer-read-only nil))
  126. (let ((row (aref ring 0)) ; row <- row ring is on
  127. (col (- (car from) n 1)) ; col <- left edge of ring
  128. (dst-col (- (car to) n 1)) ; dst-col <- dest col for left edge
  129. (dst-row (cdr to))) ; dst-row <- dest row for ring
  130. (hanoi-topos row col)
  131. (while (> row fly-row) ; move up to the fly row
  132. (hanoi-draw-ring ring nil t) ; blank out ring
  133. (previous-line 1) ; move up a line
  134. (hanoi-draw-ring ring t nil) ; redraw
  135. (sit-for 0)
  136. (setq row (1- row)))
  137. (setcdr from (1+ (cdr from))) ; adjust top row
  138. ;;
  139. ;; fly the ring over to the right pole
  140. ;;
  141. (while (not (equal dst-col col))
  142. (cond ((> dst-col col) ; dst-col > col: right shift
  143. (end-of-line 1)
  144. (delete-backward-char 2)
  145. (beginning-of-line 1)
  146. (insert ?\ ?\ )
  147. (sit-for 0)
  148. (setq col (1+ (1+ col))))
  149. ((< dst-col col) ; dst-col < col: left shift
  150. (beginning-of-line 1)
  151. (delete-char 2)
  152. (end-of-line 1)
  153. (insert ?\ ?\ )
  154. (sit-for 0)
  155. (setq col (1- (1- col))))))
  156. ;;
  157. ;; let the ring float down
  158. ;;
  159. (hanoi-topos fly-row dst-col)
  160. (while (< row dst-row) ; move down to the dest row
  161. (hanoi-draw-ring ring nil (> row fly-row)) ; blank out ring
  162. (next-line 1) ; move down a line
  163. (hanoi-draw-ring ring t nil) ; redraw ring
  164. (sit-for 0)
  165. (setq row (1+ row)))
  166. (aset ring 0 dst-row)
  167. (setcdr to (1- (cdr to)))))) ; adjust top row
  168. ;;;
  169. ;;; draw-ring - draw the ring at point, leave point unchanged
  170. ;;;
  171. ;;; Input:
  172. ;;; ring
  173. ;;; f1 - flag: t -> draw, nil -> erase
  174. ;;; f2 - flag: t -> erasing and need to draw ?\|
  175. ;;;
  176. (defun hanoi-draw-ring (ring f1 f2)
  177. (save-excursion
  178. (let* ((string (if f1 (aref ring 1) (aref ring 2)))
  179. (len (length string)))
  180. (delete-char len)
  181. (insert string)
  182. (if f2
  183. (progn
  184. (backward-char (/ (+ len 1) 2))
  185. (delete-char 1) (insert ?\|))))))