annotate lisp/games/hanoi.el @ 163:0132846995bd r20-3b8

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