comparison lisp/games/hanoi.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children b82b59fe008d
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;;; hanoi.el --- towers of hanoi in GNUmacs
2
3 ;; Author: Damon Anton Permezel
4 ;; Maintainer: FSF
5 ;; Keywords: games
6
7 ; Author (a) 1985, Damon Anton Permezel
8 ; This is in the public domain
9 ; since he distributed it without copyright notice in 1985.
10
11 ;;; Synched up with: FSF 19.30.
12
13 ;;; Commentary:
14
15 ;; Solves the Towers of Hanoi puzzle while-U-wait.
16 ;;
17 ;; The puzzle: Start with N rings, decreasing in sizes from bottom to
18 ;; top, stacked around a post. There are two other posts. Your mission,
19 ;; should you choose to accept it, is to shift the pile, stacked in its
20 ;; original order, to another post.
21 ;;
22 ;; The challenge is to do it in the fewest possible moves. Each move
23 ;; shifts one ring to a different post. But there's a rule; you can
24 ;; only stack a ring on top of a larger one.
25 ;;
26 ;; The simplest nontrivial version of this puzzle is N = 3. Solution
27 ;; time rises as 2**N, and programs to solve it have long been considered
28 ;; classic introductory exercises in the use of recursion.
29 ;;
30 ;; The puzzle is called `Towers of Hanoi' because an early popular
31 ;; presentation wove a fanciful legend around it. According to this
32 ;; myth (uttered long before the Vietnam War), there is a Buddhist
33 ;; monastery at Hanoi which contains a large room with three time-worn
34 ;; posts in it surrounded by 21 golden discs. Monks, acting out the
35 ;; command of an ancient prophecy, have been moving these disks, in
36 ;; accordance with the rules of the puzzle, once every day since the
37 ;; monastery was founded over a thousand years ago. They are said
38 ;; believe that when the last move of the puzzle is completed, the
39 ;; world will end in a clap of thunder. Fortunately, they are nowhere
40 ;; even close to being done...
41
42 ;;; Code:
43
44 ;;;
45 ;;; hanoi-topos - direct cursor addressing
46 ;;;
47 (defun hanoi-topos (row col)
48 (goto-line row)
49 (beginning-of-line)
50 (forward-char col))
51
52 ;;;
53 ;;; hanoi - user callable Towers of Hanoi
54 ;;;
55 ;;;###autoload
56 (defun hanoi (nrings)
57 "Towers of Hanoi diversion. Argument is number of rings."
58 (interactive
59 (list (if (null current-prefix-arg)
60 3
61 (prefix-numeric-value current-prefix-arg))))
62 (if (<= nrings 0) (error "Negative number of rings"))
63 (let* (floor-row
64 fly-row
65 (window-height (window-height (selected-window)))
66 (window-width (window-width (selected-window)))
67
68 ;; This is the unit of spacing to use between poles. It
69 ;; must be even. We round down, since rounding up might
70 ;; cause us to draw off the edge of the window.
71 (pole-spacing (logand (/ window-width 6) (lognot 1))))
72 (let (
73 ;; The poles are (1+ NRINGS) rows high; we also want an
74 ;; empty row at the top for the flying rings, a base, and a
75 ;; blank line underneath that.
76 (h (+ nrings 4))
77
78 ;; If we have NRINGS rings, we label them with the numbers 0
79 ;; through NRINGS-1. The width of ring i is 2i+3; it pokes
80 ;; out i spaces on either side of the pole. Rather than
81 ;; checking if the window is wide enough to accommodate this,
82 ;; we make sure pole-spacing is large enough, since that
83 ;; works even when we have decremented pole-spacing to make
84 ;; it even.
85 (w (1+ nrings)))
86 (if (not (and (>= window-height h)
87 (> pole-spacing w)))
88 (progn
89 (delete-other-windows)
90 (if (not (and (>= (setq window-height
91 (window-height (selected-window)))
92 h)
93 (> (setq pole-spacing
94 (logand (/ window-width 6) (lognot 1)))
95 w)))
96 (error "Screen is too small (need at least %dx%d)" w h))))
97 (setq floor-row (if (> (- window-height 3) h)
98 (- window-height 3) window-height)))
99 (let ((fly-row (- floor-row nrings 1))
100 ;; pole: column . fill height
101 (pole-1 (cons pole-spacing floor-row))
102 (pole-2 (cons (* 3 pole-spacing) floor-row))
103 (pole-3 (cons (* 5 pole-spacing) floor-row))
104 (rings (make-vector nrings nil)))
105 ;; construct the ring list
106 (let ((i 0))
107 (while (< i nrings)
108 ;; ring: [pole-number string empty-string]
109 (aset rings i (vector nil
110 (make-string (+ i i 3) (+ ?0 i))
111 (make-string (+ i i 3) ?\ )))
112 (setq i (1+ i))))
113 ;;
114 ;; init the screen
115 ;;
116 (switch-to-buffer "*Hanoi*")
117 (setq buffer-read-only nil)
118 (buffer-disable-undo (current-buffer))
119 (erase-buffer)
120 (let ((i 0))
121 (while (< i floor-row)
122 (setq i (1+ i))
123 (insert-char ?\ (1- window-width))
124 (insert ?\n)))
125 (insert-char ?= (1- window-width))
126
127 (let ((n 1))
128 (while (< n 6)
129 (hanoi-topos fly-row (* n pole-spacing))
130 (setq n (+ n 2))
131 (let ((i fly-row))
132 (while (< i floor-row)
133 (setq i (1+ i))
134 (next-line 1)
135 (insert ?\|)
136 (delete-char 1)
137 (backward-char 1)))))
138 ;(sit-for 0)
139 ;;
140 ;; now draw the rings in their initial positions
141 ;;
142 (let ((i 0)
143 ring)
144 (while (< i nrings)
145 (setq ring (aref rings (- nrings 1 i)))
146 (aset ring 0 (- floor-row i))
147 (hanoi-topos (cdr pole-1)
148 (- (car pole-1) (- nrings i)))
149 (hanoi-draw-ring ring t nil)
150 (setcdr pole-1 (1- (cdr pole-1)))
151 (setq i (1+ i))))
152 (setq buffer-read-only t)
153 (sit-for 0)
154 ;;
155 ;; do it!
156 ;;
157 (hanoi0 (1- nrings) pole-1 pole-2 pole-3)
158 (goto-char (point-min))
159 (message "Done")
160 (setq buffer-read-only t)
161 (force-mode-line-update)
162 (sit-for 0))))
163
164 ;;;
165 ;;; hanoi0 - work horse of hanoi
166 ;;;
167 (defun hanoi0 (n from to work)
168 (cond ((input-pending-p)
169 (signal 'quit (list "I can tell you've had enough")))
170 ((< n 0))
171 (t
172 (hanoi0 (1- n) from work to)
173 (hanoi-move-ring n from to)
174 (hanoi0 (1- n) work to from))))
175
176 ;;;
177 ;;; hanoi-move-ring - move ring 'n' from 'from' to 'to'
178 ;;;
179 ;;;
180 (defun hanoi-move-ring (n from to)
181 (let ((ring (aref rings n)) ; ring <- ring: (ring# . row)
182 (buffer-read-only nil))
183 (let ((row (aref ring 0)) ; row <- row ring is on
184 (col (- (car from) n 1)) ; col <- left edge of ring
185 (dst-col (- (car to) n 1)) ; dst-col <- dest col for left edge
186 (dst-row (cdr to))) ; dst-row <- dest row for ring
187 (hanoi-topos row col)
188 (while (> row fly-row) ; move up to the fly row
189 (hanoi-draw-ring ring nil t) ; blank out ring
190 (previous-line 1) ; move up a line
191 (hanoi-draw-ring ring t nil) ; redraw
192 (sit-for 0)
193 (setq row (1- row)))
194 (setcdr from (1+ (cdr from))) ; adjust top row
195 ;;
196 ;; fly the ring over to the right pole
197 ;;
198 (while (not (equal dst-col col))
199 (cond ((> dst-col col) ; dst-col > col: right shift
200 (end-of-line 1)
201 (delete-backward-char 2)
202 (beginning-of-line 1)
203 (insert ?\ ?\ )
204 (sit-for 0)
205 (setq col (1+ (1+ col))))
206 ((< dst-col col) ; dst-col < col: left shift
207 (beginning-of-line 1)
208 (delete-char 2)
209 (end-of-line 1)
210 (insert ?\ ?\ )
211 (sit-for 0)
212 (setq col (1- (1- col))))))
213 ;;
214 ;; let the ring float down
215 ;;
216 (hanoi-topos fly-row dst-col)
217 (while (< row dst-row) ; move down to the dest row
218 (hanoi-draw-ring ring nil (> row fly-row)) ; blank out ring
219 (next-line 1) ; move down a line
220 (hanoi-draw-ring ring t nil) ; redraw ring
221 (sit-for 0)
222 (setq row (1+ row)))
223 (aset ring 0 dst-row)
224 (setcdr to (1- (cdr to)))))) ; adjust top row
225
226 ;;;
227 ;;; draw-ring - draw the ring at point, leave point unchanged
228 ;;;
229 ;;; Input:
230 ;;; ring
231 ;;; f1 - flag: t -> draw, nil -> erase
232 ;;; f2 - flag: t -> erasing and need to draw ?\|
233 ;;;
234 (defun hanoi-draw-ring (ring f1 f2)
235 (save-excursion
236 (let* ((string (if f1 (aref ring 1) (aref ring 2)))
237 (len (length string)))
238 (delete-char len)
239 (insert string)
240 (if f2
241 (progn
242 (backward-char (/ (+ len 1) 2))
243 (delete-char 1) (insert ?\|))))))
244
245 (provide 'hanoi)
246
247 ;;; hanoi.el ends here