(defun hanoi-topos (row col)
(goto-line row)
(beginning-of-line)
(forward-char col))
(defun hanoi (nrings)
"Towers of Hanoi diversion. Argument is number of rings."
(interactive "p")
(if (<= nrings 1) (setq nrings 7))
(let* (floor-row
fly-row
(window-height (1- (window-height (selected-window))))
(window-width (window-width (selected-window)))
(pole-spacing (/ window-width 6)))
(if (not (and (> window-height (1+ nrings))
(> pole-spacing nrings)))
(progn
(delete-other-windows)
(if (not (and (> (setq window-height
(1- (window-height (selected-window))))
(1+ nrings))
(> (setq pole-spacing (/ window-width 6))
nrings)))
(error "Window is too small (need at least %dx%d)"
(* 6 (1+ nrings)) (+ 2 nrings)))))
(setq floor-row (if (> (- window-height 3) (1+ nrings))
(- window-height 3) window-height))
(let ((fly-row (- floor-row nrings 1))
(pole-1 (cons (1- pole-spacing) floor-row))
(pole-2 (cons (1- (* 3 pole-spacing)) floor-row))
(pole-3 (cons (1- (* 5 pole-spacing)) floor-row))
(rings (make-vector nrings nil)))
(let ((i 0))
(while (< i nrings)
(aset rings i (vector nil
(make-string (+ i i 3) (+ ?0 (% i 10)))
(make-string (+ i i 3) ?\ )))
(setq i (1+ i))))
(switch-to-buffer "*Hanoi*")
(setq buffer-read-only nil)
(buffer-disable-undo (current-buffer))
(erase-buffer)
(let ((i 0))
(while (< i floor-row)
(setq i (1+ i))
(insert-char ?\ (1- window-width))
(insert ?\n)))
(insert-char ?= (1- window-width))
(let ((n 1))
(while (< n 6)
(hanoi-topos fly-row (1- (* n pole-spacing)))
(setq n (+ n 2))
(let ((i fly-row))
(while (< i floor-row)
(setq i (1+ i))
(next-line 1)
(insert ?\|)
(delete-char 1)
(backward-char 1)))))
(let ((i 0)
ring)
(while (< i nrings)
(setq ring (aref rings (- nrings 1 i)))
(aset ring 0 (- floor-row i))
(hanoi-topos (cdr pole-1)
(- (car pole-1) (- nrings i)))
(hanoi-draw-ring ring t nil)
(setcdr pole-1 (1- (cdr pole-1)))
(setq i (1+ i))))
(setq buffer-read-only t)
(sit-for 0)
(let ((line-number-mode nil)
(column-number-mode nil))
(hanoi0 (1- nrings) pole-1 pole-2 pole-3))
(goto-char (point-min))
(message "Done")
(setq buffer-read-only t)
(force-mode-line-update)
(sit-for 0))))
(defun hanoi0 (n from to work)
(cond ((input-pending-p)
(signal 'quit (list "I can tell you've had enough")))
((< n 0))
(t
(hanoi0 (1- n) from work to)
(hanoi-move-ring n from to)
(hanoi0 (1- n) work to from))))
(defun hanoi-move-ring (n from to)
(let ((ring (aref rings n)) (buffer-read-only nil))
(let ((row (aref ring 0)) (col (- (car from) n 1)) (dst-col (- (car to) n 1)) (dst-row (cdr to))) (hanoi-topos row col)
(while (> row fly-row) (hanoi-draw-ring ring nil t) (previous-line 1) (hanoi-draw-ring ring t nil) (sit-for 0)
(setq row (1- row)))
(setcdr from (1+ (cdr from))) (while (not (equal dst-col col))
(cond ((> dst-col col) (end-of-line 1)
(delete-backward-char 2)
(beginning-of-line 1)
(insert ?\ ?\ )
(sit-for 0)
(setq col (1+ (1+ col))))
((< dst-col col) (beginning-of-line 1)
(delete-char 2)
(end-of-line 1)
(insert ?\ ?\ )
(sit-for 0)
(setq col (1- (1- col))))))
(hanoi-topos fly-row dst-col)
(while (< row dst-row) (hanoi-draw-ring ring nil (> row fly-row)) (next-line 1) (hanoi-draw-ring ring t nil) (sit-for 0)
(setq row (1+ row)))
(aset ring 0 dst-row)
(setcdr to (1- (cdr to))))))
(defun hanoi-draw-ring (ring f1 f2)
(save-excursion
(let* ((string (if f1 (aref ring 1) (aref ring 2)))
(len (length string)))
(delete-char len)
(insert string)
(if f2
(progn
(backward-char (/ (+ len 1) 2))
(delete-char 1) (insert ?\|))))))
(provide 'hanoi)