changeset 1663:a5eec33a8f44

Tue Dec 1 22:34:42 1992 Jim Blandy (jimb@totoro.cs.oberlin.edu) * hanoi.el (hanoi): If pole-spacing is odd, round down, not up. To see if the window is wide enough, just check if one half of a ring will fit inside pole-spacing.
author Jim Blandy <jimb@redhat.com>
date Sun, 06 Dec 1992 22:41:11 +0000
parents ee7f627ef26e
children 5345766220da
files lisp/play/hanoi.el
diffstat 1 files changed, 31 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/play/hanoi.el	Sun Dec 06 22:40:25 1992 +0000
+++ b/lisp/play/hanoi.el	Sun Dec 06 22:41:11 1992 +0000
@@ -29,26 +29,40 @@
 	     3
 	     (prefix-numeric-value current-prefix-arg))))  
   (if (<= nrings 0) (error "Negative number of rings"))
-  (let (pole-spacing
-	floor-row
-	fly-row
-	(window-height (window-height (selected-window)))
-	(window-width (window-width (selected-window))))
-    (let ((h (+ nrings 2))
-	  (w (+ (* (1- nrings) 6) 2 5)))
-      (if (not (and (>= window-width h)
-		    (> window-width w)))
+  (let* (floor-row
+	 fly-row
+	 (window-height (window-height (selected-window)))
+	 (window-width (window-width (selected-window)))
+
+	 ;; This is the unit of spacing to use between poles.  It
+	 ;; must be even.  We round down, since rounding up might
+	 ;; cause us to draw off the edge of the window.
+	 (pole-spacing (logand (/ window-width 6) (lognot 1))))
+    (let (
+	  ;; The poles are (1+ NRINGS) rows high; we also want an
+	  ;; empty row at the top for the flying rings, a base, and a
+	  ;; blank line underneath that.
+	  (h (+ nrings 4))
+
+	  ;; If we have NRINGS rings, we label them with the numbers 0
+	  ;; through NRINGS-1.  The width of ring i is 2i+3; it pokes
+	  ;; out i spaces on either side of the pole.  Rather than
+	  ;; checking if the window is wide enough to accomodate this,
+	  ;; we make sure pole-spacing is large enough, since that
+	  ;; works even when we have decremented pole-spacing to make
+	  ;; it even.
+	  (w (1+ nrings)))
+      (if (not (and (>= window-height h)
+		    (> pole-spacing w)))
 	  (progn
 	    (delete-other-windows)
 	    (if (not (and (>= (setq window-height
-				    (window-height (selected-window))) h)
-			  (> (setq window-width
-				   (window-width (selected-window))) w)))
+				    (window-height (selected-window)))
+			      h)
+			  (> (setq pole-spacing
+				   (logand (/ window-width 6) (lognot 1)))
+			     w)))
 		(error "Screen is too small (need at least %dx%d)" w h))))
-      (setq pole-spacing (/ window-width 6))
-      (if (not (zerop (logand pole-spacing 1)))
-	  ;; must be even
-	  (setq pole-spacing (1+ pole-spacing)))
       (setq floor-row (if (> (- window-height 3) h)
 			  (- window-height 3) window-height)))
     (let ((fly-row (- floor-row nrings 1))
@@ -197,4 +211,4 @@
 	    (backward-char (/ (+ len 1) 2))
 	    (delete-char 1) (insert ?\|))))))
 
-;;; hanoi.el
\ No newline at end of file
+;;; hanoi.el