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