1 ;;; hanoi.el --- towers of hanoi in GNUmacs
3 ; Author (a) 1985, Damon Anton Permezel
6 ;;; hanoi-topos - direct cursor addressing
8 (defun hanoi-topos (row col
)
14 ;;; hanoi - user callable Towers of Hanoi
18 "Towers of Hanoi diversion. Argument is number of rings."
20 (list (if (null current-prefix-arg
)
22 (prefix-numeric-value current-prefix-arg
))))
23 (if (<= nrings
0) (error "Negative number of rings"))
27 (window-height (window-height (selected-window)))
28 (window-width (window-width (selected-window))))
29 (let ((h (+ nrings
2))
30 (w (+ (* (1- nrings
) 6) 2 5)))
31 (if (not (and (>= window-width h
)
34 (delete-other-windows)
35 (if (not (and (>= (setq window-height
36 (window-height (selected-window))) h
)
38 (window-width (selected-window))) w
)))
39 (error "Screen is too small (need at least %dx%d)" w h
))))
40 (setq pole-spacing
(/ window-width
6))
41 (if (not (zerop (logand pole-spacing
1)))
43 (setq pole-spacing
(1+ pole-spacing
)))
44 (setq floor-row
(if (> (- window-height
3) h
)
45 (- window-height
3) window-height
)))
46 (let ((fly-row (- floor-row nrings
1))
47 ;; pole: column . fill height
48 (pole-1 (cons pole-spacing floor-row
))
49 (pole-2 (cons (* 3 pole-spacing
) floor-row
))
50 (pole-3 (cons (* 5 pole-spacing
) floor-row
))
51 (rings (make-vector nrings nil
)))
52 ;; construct the ring list
55 ;; ring: [pole-number string empty-string]
56 (aset rings i
(vector nil
57 (make-string (+ i i
3) (+ ?
0 i
))
58 (make-string (+ i i
3) ?\
)))
63 (switch-to-buffer "*Hanoi*")
64 (setq buffer-read-only nil
)
65 (buffer-disable-undo (current-buffer))
68 (while (< i floor-row
)
70 (insert-char ?\
(1- window-width
))
72 (insert-char ?
= (1- window-width
))
76 (hanoi-topos fly-row
(* n pole-spacing
))
79 (while (< i floor-row
)
87 ;; now draw the rings in their initial positions
92 (setq ring
(aref rings
(- nrings
1 i
)))
93 (aset ring
0 (- floor-row i
))
94 (hanoi-topos (cdr pole-1
)
95 (- (car pole-1
) (- nrings i
)))
96 (hanoi-draw-ring ring t nil
)
97 (setcdr pole-1
(1- (cdr pole-1
)))
99 (setq buffer-read-only t
)
104 (hanoi0 (1- nrings
) pole-1 pole-2 pole-3
)
105 (goto-char (point-min))
107 (setq buffer-read-only t
)
108 (set-buffer-modified-p (buffer-modified-p))
112 ;;; hanoi0 - work horse of hanoi
114 (defun hanoi0 (n from to work
)
115 (cond ((input-pending-p)
116 (signal 'quit
(list "I can tell you've had enough")))
119 (hanoi0 (1- n
) from work to
)
120 (hanoi-move-ring n from to
)
121 (hanoi0 (1- n
) work to from
))))
124 ;;; hanoi-move-ring - move ring 'n' from 'from' to 'to'
127 (defun hanoi-move-ring (n from to
)
128 (let ((ring (aref rings n
)) ; ring <- ring: (ring# . row)
129 (buffer-read-only nil
))
130 (let ((row (aref ring
0)) ; row <- row ring is on
131 (col (- (car from
) n
1)) ; col <- left edge of ring
132 (dst-col (- (car to
) n
1)) ; dst-col <- dest col for left edge
133 (dst-row (cdr to
))) ; dst-row <- dest row for ring
134 (hanoi-topos row col
)
135 (while (> row fly-row
) ; move up to the fly row
136 (hanoi-draw-ring ring nil t
) ; blank out ring
137 (previous-line 1) ; move up a line
138 (hanoi-draw-ring ring t nil
) ; redraw
141 (setcdr from
(1+ (cdr from
))) ; adjust top row
143 ;; fly the ring over to the right pole
145 (while (not (equal dst-col col
))
146 (cond ((> dst-col col
) ; dst-col > col: right shift
148 (delete-backward-char 2)
149 (beginning-of-line 1)
152 (setq col
(1+ (1+ col
))))
153 ((< dst-col col
) ; dst-col < col: left shift
154 (beginning-of-line 1)
159 (setq col
(1- (1- col
))))))
161 ;; let the ring float down
163 (hanoi-topos fly-row dst-col
)
164 (while (< row dst-row
) ; move down to the dest row
165 (hanoi-draw-ring ring nil
(> row fly-row
)) ; blank out ring
166 (next-line 1) ; move down a line
167 (hanoi-draw-ring ring t nil
) ; redraw ring
170 (aset ring
0 dst-row
)
171 (setcdr to
(1- (cdr to
)))))) ; adjust top row
174 ;;; draw-ring - draw the ring at point, leave point unchanged
178 ;;; f1 - flag: t -> draw, nil -> erase
179 ;;; f2 - flag: t -> erasing and need to draw ?\|
181 (defun hanoi-draw-ring (ring f1 f2
)
183 (let* ((string (if f1
(aref ring
1) (aref ring
2)))
184 (len (length string
)))
189 (backward-char (/ (+ len
1) 2))
190 (delete-char 1) (insert ?\|
))))))