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