(FILECREATED "21-AUG-83 03:30:51" {PHYLUM}<LISPUSERS>HANOI.;26 16011  

      changes to:  (FNS MOVEDIS)

      previous date: " 2-JUN-83 23:41:31" {PHYLUM}<LISPUSERS>HANOI.;25)


(* Copyright (c) 1982, 1983 by Xerox Corporation)

(PRETTYCOMPRINT HANOICOMS)

(RPAQQ HANOICOMS ((FNS DISPLAYPEGSANDRINGS DOHANOI FINDOTHER HANOI HANOIDEMO MOVEDIS MOVERING 
		       RINGSHADE SETUPRINGBITMAPS TRACK WHANOI XHANOI)
		  (RECORDS PEG RING)
		  (CONSTANTS XRINGSHADE ORINGSHADE XPEGSHADE)
		  (VARS EVENRINGSHADE ODDRINGSHADE PEGSHADE)
		  (CONSTANTS PEGMIN HANOIMARGIN (MAXVERTSPEED 17)
			     (MAXHORIZSPEED 22))
		  (VARS (CHANGESPEEDFLG T))
		  (VARS (HANOIWINDOW))
		  (P (MOVD? (QUOTE NILL)
			    (QUOTE BLOCK)))
		  (MACROS PEGN)))
(DEFINEQ

(DISPLAYPEGSANDRINGS
  [LAMBDA (PEGS W)                                           (* lmm " 8-MAR-82 16:52")
                                                             (* displays the pegs and the rings on them.)
    (for PEG in PEGS
       do (CLEAR W PEGSHADE (fetch PEGREGION of PEG))
	  (for RING in (fetch RINGS of PEG)
	     do (CLEAR W (RINGSHADE (fetch RINGNUMBER of RING))
		       (fetch RINGREGION of RING))
		(COND
		  ((fetch RINGLABEL of RING)
		    (CENTERPRINTINREGION (fetch RINGLABEL of RING)
					 (fetch RINGREGION of RING)
					 W])

(DOHANOI
  [LAMBDA (N SRC DST W)                                      (* lmm " 8-MAR-82 12:05")
    (COND
      ((EQ N 1)
	(MOVERING SRC DST W))
      (T (DOHANOI (SUB1 N)
		  SRC
		  (FINDOTHER SRC DST)
		  W)
	 (MOVERING SRC DST W)
	 (DOHANOI (SUB1 N)
		  (FINDOTHER SRC DST)
		  DST W])

(FINDOTHER
  [LAMBDA (S D)                                             (* bas: "10-DEC-80 14:01")
    (for Z from 1 to 3 thereis (NOT (OR (EQ Z S)
					(EQ Z D])

(HANOI
  [LAMBDA (NRINGS WINDOW FONT ONCE)                          (* lmm " 9-MAR-82 09:52")
    (WHANOI NRINGS WINDOW FONT ONCE])

(HANOIDEMO
  [LAMBDA NIL                                                (* rrb " 1-JUN-82 16:36")
    (PROG (CHANGESPEEDFLG)
          (WHANOI 7
		  [COND
		    ((TYPENAMEP HANOIWINDOW (QUOTE WINDOW))
		      HANOIWINDOW)
		    (T (SETQ HANOIWINDOW
			 (CREATEW (create REGION
					  LEFT ← 4
					  BOTTOM ← 340
					  WIDTH ← 500
					  HEIGHT ← 300]
		  NIL T])

(MOVEDIS
  [LAMBDA (RING DY SX DX W)                                  (* edited: "21-AUG-83 03:30")

          (* moves RING from its position on the source peg whose left is SX to the peg whose left is DX at a height of DY)


    (PROG ((RINGREGION (fetch RINGREGION of RING))
	   RINGWIDTH HORIZWIDTH MOVERIGHTFLG)
          [COND
	    (CHANGESPEEDFLG (GETMOUSESTATE)                  (* IPLUS 16 is because wind cursor can go negative.)
			    (SETQ VERTSPEED (IMIN (IMAX (IDIFFERENCE 17 (IQUOTIENT LASTMOUSEY 50))
							1)
						  MAXVERTSPEED))
			    (SETQ HORIZSPEED (IMIN (IMAX (ADD1 (IQUOTIENT LASTMOUSEX 50))
							 1)
						   MAXHORIZSPEED]
          (SETUPRINGBITMAPS RING (SETQ RINGWIDTH (fetch WIDTH of RINGREGION))
			    (SETQ MOVERIGHTFLG (IGREATERP DX SX))
			    W)                               (* PROG is because FOR loop bug.)
          (PROG ((I (fetch BOTTOM of RINGREGION))
		 (TOPLIMIT (IDIFFERENCE PEGTOP VERTSPEED)))
	    LP  (COND
		  ((IGREATERP TOPLIMIT I)
		    (BITBLT UPRINGBM 0 0 W SX I RINGWIDTH (IPLUS RINGHEIGHT VERTSPEED)
			    (QUOTE INPUT)
			    (QUOTE REPLACE))
		    (SETQ I (IPLUS VERTSPEED I))
		    (GO LP)))
	        (BITBLT UPRINGBM 0 (IDIFFERENCE I TOPLIMIT)
			W SX I RINGWIDTH (IPLUS RINGHEIGHT VERTSPEED)
			(QUOTE INPUT)
			(QUOTE REPLACE)))
          (BITBLT TOPUPRINGBM 0 0 W SX PEGTOP RINGWIDTH (IPLUS RINGHEIGHT VERTSPEED)
		  (QUOTE INPUT)
		  (QUOTE REPLACE))
          (SETQ HORIZWIDTH (IPLUS RINGWIDTH HORIZSPEED))
          (for I from (COND
			(MOVERIGHTFLG SX)
			(T (IDIFFERENCE SX HORIZSPEED)))
	     to (COND
		  (MOVERIGHTFLG (SUB1 (IDIFFERENCE DX HORIZSPEED)))
		  (T (ADD1 DX)))
	     by (ITIMES (COND
			  ((IGREATERP DX SX)
			    1)
			  (T -1))
			HORIZSPEED)
	     do (BITBLT HORIZRINGBM 0 0 W I (IPLUS PEGTOP VERTSPEED)
			HORIZWIDTH RINGHEIGHT (QUOTE INPUT)
			(QUOTE REPLACE)))
          (BITBLT HORIZRINGBM 0 0 W (COND
		    (MOVERIGHTFLG (IDIFFERENCE DX HORIZSPEED))
		    (T DX))
		  (IPLUS PEGTOP VERTSPEED)
		  HORIZWIDTH NIL (QUOTE INPUT)
		  (QUOTE REPLACE))                           (* Update the ring region's left)
          (replace LEFT of RINGREGION with (IPLUS (fetch LEFT of RINGREGION)
						  (IDIFFERENCE DX SX)))
          (for I from PEGTOP to (SUB1 (IDIFFERENCE PEGTOP RINGHEIGHT)) by (IMINUS VERTSPEED)
	     do (BITBLT TOPDOWNRINGBM NIL NIL W DX I RINGWIDTH (IPLUS RINGHEIGHT VERTSPEED)
			(QUOTE INPUT)
			(QUOTE REPLACE)))
          (BITBLT TOPDOWNRINGBM NIL NIL W DX (IDIFFERENCE PEGTOP RINGHEIGHT)
		  RINGWIDTH
		  (IPLUS RINGHEIGHT VERTSPEED)
		  (QUOTE INPUT)
		  (QUOTE REPLACE))
          (PROG [(I (IDIFFERENCE PEGTOP (IPLUS VERTSPEED RINGHEIGHT]
	    LP  (COND
		  ((IGREATERP DY I)                          (* blt last ring image)
		    (BITBLT DOWNRINGBM 0 0 W DX DY RINGWIDTH (COND
			      ((IGREATERP VERTSPEED RINGHEIGHT)
				(IDIFFERENCE (IPLUS RINGHEIGHT VERTSPEED)
					     (IDIFFERENCE DY I)))
			      (T (IPLUS RINGHEIGHT VERTSPEED)))
			    (QUOTE INPUT)
			    (QUOTE REPLACE))
		    (RETURN)))
	        (BITBLT DOWNRINGBM 0 0 W DX I RINGWIDTH (IPLUS RINGHEIGHT VERTSPEED)
			(QUOTE INPUT)
			(QUOTE REPLACE))
	        (SETQ I (IDIFFERENCE I VERTSPEED))
	        (GO LP))
          (replace BOTTOM of RINGREGION with DY)
          (RETURN RING])

(MOVERING
  [LAMBDA (SRC DST W)                                        (* rrb " 2-AUG-82 17:41")
    (PROG ([X (fetch RINGREGION of (CAR (fetch RINGS of (PEGN DST]
	   RING)
          (push (fetch RINGS of (PEGN DST))
		(MOVEDIS [SETQ RING (pop (fetch RINGS of (PEGN SRC]
			 (IPLUS (fetch BOTTOM of X)
				(fetch HEIGHT of X))
			 (TRACK SRC (fetch RINGREGION of RING))
			 (TRACK DST (fetch RINGREGION of RING))
			 W))
          (BLOCK])

(RINGSHADE
  [LAMBDA (RINGN)                                           (* rrb " 9-JUN-81 15:11")
    (COND
      ((EQ RINGN (QUOTE BASE))
	PEGSHADE)
      ((ZEROP (LOGAND RINGN 1))
	EVENRINGSHADE)
      (T ODDRINGSHADE])

(SETUPRINGBITMAPS
  [LAMBDA (RING RINGWIDTH MOVERIGHTFLG W)                    (* lmm " 9-MAR-82 09:47")

          (* sets up the ring bitmaps. There are 5 ring bitmaps: up while on peg, up above peg, horizontal, down above peg 
	  and down while on peg.)


    (PROG ((PEGOFFSET (IQUOTIENT (IDIFFERENCE RINGWIDTH PEGWIDTH)
				 2))
	   (RINGREGION (fetch RINGREGION of RING))
	   (RINGN (fetch RINGNUMBER of RING)))
          (AND FONT (DSPFONT FONT RDEST))
          (DSPOPERATION (QUOTE ERASE)
			RDEST)
          [PROGN (CLEAR UPRINGBM)
		 (BITBLT NIL NIL NIL UPRINGBM 0 VERTSPEED RINGWIDTH RINGHEIGHT (QUOTE TEXTURE)
			 (QUOTE REPLACE)
			 (RINGSHADE RINGN))                  (* put in peg)
		 (BITBLT NIL NIL NIL UPRINGBM PEGOFFSET 0 PEGWIDTH VERTSPEED (QUOTE TEXTURE)
			 (QUOTE REPLACE)
			 PEGSHADE)
		 (COND
		   ((fetch RINGLABEL of RING)                (* print in label if there is one.)
		     (DSPDESTINATION UPRINGBM RDEST)
		     (CENTERPRINTINAREA (fetch RINGLABEL of RING)
					0 VERTSPEED RINGWIDTH RINGHEIGHT RDEST]
          [PROGN (CLEAR TOPUPRINGBM)
		 (BITBLT NIL NIL NIL TOPUPRINGBM 0 VERTSPEED RINGWIDTH RINGHEIGHT (QUOTE TEXTURE)
			 (QUOTE REPLACE)
			 (RINGSHADE RINGN))
		 (COND
		   ((fetch RINGLABEL of RING)                (* print in label if there is one.)
		     (DSPDESTINATION TOPUPRINGBM RDEST)
		     (CENTERPRINTINAREA (fetch RINGLABEL of RING)
					0 VERTSPEED RINGWIDTH RINGHEIGHT RDEST]
          (PROGN (CLEAR DOWNRINGBM)
		 (BITBLT NIL NIL NIL DOWNRINGBM 0 0 RINGWIDTH RINGHEIGHT (QUOTE TEXTURE)
			 (QUOTE REPLACE)
			 (RINGSHADE RINGN))
		 (COND
		   ((fetch RINGLABEL of RING)                (* print in label if there is one.)
		     (DSPDESTINATION DOWNRINGBM RDEST)
		     (CENTERPRINTINAREA (fetch RINGLABEL of RING)
					0 0 RINGWIDTH RINGHEIGHT RDEST)))
                                                             (* put in peg)
		 (BITBLT NIL NIL NIL DOWNRINGBM PEGOFFSET RINGHEIGHT PEGWIDTH VERTSPEED (QUOTE 
											  TEXTURE)
			 (QUOTE REPLACE)
			 PEGSHADE))
          [PROGN (CLEAR TOPDOWNRINGBM)
		 (BITBLT NIL NIL NIL TOPDOWNRINGBM 0 0 RINGWIDTH RINGHEIGHT (QUOTE TEXTURE)
			 (QUOTE REPLACE)
			 (RINGSHADE RINGN))
		 (COND
		   ((fetch RINGLABEL of RING)                (* print in label if there is one.)
		     (DSPDESTINATION TOPDOWNRINGBM RDEST)
		     (CENTERPRINTINAREA (fetch RINGLABEL of RING)
					0 0 RINGWIDTH RINGHEIGHT RDEST]
          [PROGN (CLEAR HORIZRINGBM)
		 (BITBLT NIL NIL NIL HORIZRINGBM (COND
			   (MOVERIGHTFLG HORIZSPEED)
			   (T 0))
			 0 RINGWIDTH RINGHEIGHT (QUOTE TEXTURE)
			 (QUOTE REPLACE)
			 (RINGSHADE RINGN))
		 (COND
		   ((fetch RINGLABEL of RING)                (* print in label if there is one.)
		     (DSPDESTINATION HORIZRINGBM RDEST)
		     (CENTERPRINTINAREA (fetch RINGLABEL of RING)
					(COND
					  (MOVERIGHTFLG HORIZSPEED)
					  (T 0))
					0 RINGWIDTH RINGHEIGHT RDEST]
          (RETURN])

(TRACK
  [LAMBDA (PN REGION)                                        (* lmm " 8-MAR-82 12:10")
                                                             (* returns the track offset for ring movement on a peg.)
    (IPLUS HANOIMARGIN (IPLUS (ITIMES RINGLARGEST (SUB1 PN))
			      (IQUOTIENT (IDIFFERENCE RINGLARGEST (fetch WIDTH of REGION))
					 2])

(WHANOI
  [LAMBDA (RINGS W FONT ONCE)                                (* rrb " 1-JUN-82 16:12")
                                                             (* runs hanoi in a region of a displaystream)
    (PROG ([REGION (DSPCLIPPINGREGION NIL (SETQ HANOIWINDOW (SETQ W (COND
					    ((NULL W)
					      (OR HANOIWINDOW (CREATEW)))
					    ((WINDOWP W))
					    (T (CREATEW W]
	   [NRINGS (COND
		     ((NUMBERP RINGS)
		       RINGS)
		     (T (LENGTH RINGS]
	   (HORIZSPEED 21)
	   (VERTSPEED 17)
	   PEGS RINGBM TOPUPRINGBM RINGLARGEST TOPDOWNRINGBM PEGWIDTH BASEWIDTH RINGHEIGHT 
	   MOVEMENTHEIGHT BASEHEIGHT PEGTOP RINGDISPLAYSTREAM HANOIWINDOW RINGDELTA UPRINGBM 
	   HORIZRINGBM DOWNRINGBM (RDEST (DSPCREATE)))
          (DECLARE (SPECVARS . T))
          (PROG (IMAGEHEIGHT)
	        (SETQ BASEWIDTH (IDIFFERENCE (fetch WIDTH of REGION)
					     (ITIMES HANOIMARGIN 2)))
	        (SETQ RINGLARGEST (IQUOTIENT BASEWIDTH 3))   (* RINGDELTA is the difference in peg size on each 
							     side.)
	        (COND
		  ([ZEROP (SETQ RINGDELTA (IQUOTIENT (IDIFFERENCE RINGLARGEST PEGMIN)
						     (ADD1 (ITIMES NRINGS 2]
		    (HELP "Not enough width for a display.")))

          (* leave one ring width for base, one for top of peg and two above peg for movement. Doesn't really use two 
	  heights at top, only one plus VERTSPEED)


	        (SETQ RINGHEIGHT (IQUOTIENT (SETQ IMAGEHEIGHT (IDIFFERENCE (fetch HEIGHT
									      of REGION)
									   (ITIMES HANOIMARGIN 2)))
					    (IPLUS NRINGS 4)))
	        (COND
		  ((ZEROP RINGHEIGHT)
		    (HELP "Not enough height for display.")))
	        (SETQ PEGWIDTH (IQUOTIENT (IDIFFERENCE RINGLARGEST (ITIMES RINGDELTA (SUB1 NRINGS)
									   2))
					  3))                (* put extra in base if it comes out closer to 
							     pegwidth.)
	        (COND
		  [(IGREATERP PEGWIDTH RINGHEIGHT)
		    (SETQ BASEHEIGHT (IMIN PEGWIDTH (IPLUS RINGHEIGHT (IDIFFERENCE
							     IMAGEHEIGHT
							     (ITIMES (IPLUS NRINGS 4)
								     RINGHEIGHT]
		  (T (SETQ BASEHEIGHT RINGHEIGHT)))
	        (SETQ MOVEMENTHEIGHT (IPLUS [SETQ PEGTOP (IPLUS HANOIMARGIN BASEHEIGHT
								(ITIMES RINGHEIGHT (ADD1 NRINGS]
					    VERTSPEED))
	        (DSPFONT FONT RDEST)
	        (DSPFONT FONT W)
	        (DSPOPERATION (QUOTE ERASE)
			      RDEST)
	        (DSPOPERATION (QUOTE ERASE)
			      W))
          [PROG ((BASE (create REGION
			       LEFT ← HANOIMARGIN
			       BOTTOM ← HANOIMARGIN
			       WIDTH ← BASEWIDTH
			       HEIGHT ← BASEHEIGHT)))
	        (SETQ PEGS (for PLEFT from (IPLUS HANOIMARGIN (IQUOTIENT (IDIFFERENCE RINGLARGEST 
										      PEGWIDTH)
									 2))
			      by RINGLARGEST as I from 1 to 3
			      collect (create PEG
					      PEGREGION ←(create REGION
								 LEFT ← PLEFT
								 BOTTOM ←(IPLUS BASEHEIGHT 
										HANOIMARGIN)
								 WIDTH ← PEGWIDTH
								 HEIGHT ←(ITIMES RINGHEIGHT
										 (ADD1 NRINGS)))
					      RINGS ←(LIST (create RING
								   RINGREGION ← BASE
								   RINGNUMBER ←(QUOTE BASE]
          [PROG [(SOURCEPEG (PEGN 1))
		 (RINGLABELS (COND
			       ((LISTP RINGS)
				 (REVERSE RINGS))
			       (T                            (* collect n NILs as lables.)
				  (for I from 1 to RINGS collect NIL]
	        (for RINGBOTTOM from (IPLUS HANOIMARGIN BASEHEIGHT) by RINGHEIGHT as RINGLEFT
		   from (IPLUS HANOIMARGIN (ITIMES RINGLARGEST (SUB1 1))) by RINGDELTA as I
		   from 0 to (SUB1 NRINGS) as LABEL in RINGLABELS
		   do (push (fetch RINGS of SOURCEPEG)
			    (create RING
				    RINGREGION ←(create REGION
							LEFT ← RINGLEFT
							BOTTOM ← RINGBOTTOM
							WIDTH ←(IDIFFERENCE RINGLARGEST
									    (ITIMES I 2 RINGDELTA))
							HEIGHT ← RINGHEIGHT)
				    RINGNUMBER ←(ADD1 (IDIFFERENCE NRINGS I))
				    RINGLABEL ← LABEL)))     (* allocate bitmaps for ring movement)
	        (SETQ HORIZRINGBM (BITMAPCREATE (IPLUS RINGLARGEST MAXHORIZSPEED)
						RINGHEIGHT))
	        (SETQ UPRINGBM (BITMAPCREATE RINGLARGEST (IPLUS RINGHEIGHT MAXVERTSPEED)))
	        (SETQ DOWNRINGBM (BITMAPCREATE RINGLARGEST (IPLUS RINGHEIGHT MAXVERTSPEED)))
	        (SETQ TOPUPRINGBM (BITMAPCREATE RINGLARGEST (IPLUS RINGHEIGHT MAXVERTSPEED)))
	        (SETQ TOPDOWNRINGBM (BITMAPCREATE RINGLARGEST (IPLUS RINGHEIGHT MAXVERTSPEED]
          (CLEAR W)
          (DISPLAYPEGSANDRINGS PEGS W)
          (bind (HERE ← 1)
		(THERE ← 3)
	     do (DOHANOI NRINGS HERE THERE W)
		(COND
		  (ONCE (RETURN)))
		(DISMISS 2000)
		(SETQ HERE (PROG1 THERE (SETQ THERE (FINDOTHER HERE THERE])

(XHANOI
  [LAMBDA NIL                                                (* lmm " 8-MAR-82 15:59")
    (PROG ((EVENRINGSHADE XRINGSHADE)
	   (ODDRINGSHADE ORINGSHADE)
	   (PEGSHADE XPEGSHADE))
          (WHANOI (QUOTE (X E R O X))
		  (QUOTE (0 0 400 280))
		  (FONTCREATE (QUOTE LOGO)
			      24])
)
[DECLARE: EVAL@COMPILE 

(RECORD PEG (PEGREGION RINGS))

(RECORD RING (RINGREGION RINGNUMBER RINGLABEL))
]
(DECLARE: EVAL@COMPILE 

(RPAQQ XRINGSHADE 42405)

(RPAQQ ORINGSHADE 60375)

(RPAQQ XPEGSHADE 65535)

(CONSTANTS XRINGSHADE ORINGSHADE XPEGSHADE)
)

(RPAQQ EVENRINGSHADE 42405)

(RPAQQ ODDRINGSHADE 61375)

(RPAQQ PEGSHADE 65535)
(DECLARE: EVAL@COMPILE 

(RPAQQ PEGMIN 2)

(RPAQQ HANOIMARGIN 5)

(RPAQQ MAXVERTSPEED 17)

(RPAQQ MAXHORIZSPEED 22)

(CONSTANTS PEGMIN HANOIMARGIN (MAXVERTSPEED 17)
	   (MAXHORIZSPEED 22))
)

(RPAQQ CHANGESPEEDFLG T)

(RPAQQ HANOIWINDOW NIL)
(MOVD? (QUOTE NILL)
       (QUOTE BLOCK))
(DECLARE: EVAL@COMPILE 

(PUTPROPS PEGN MACRO [(N)
		      (CAR (SELECTQ N
				    (1 PEGS)
				    (2 (CDR PEGS))
				    (CDDR PEGS])
)
(PUTPROPS HANOI COPYRIGHT ("Xerox Corporation" 1982 1983))
(DECLARE: DONTCOPY
  (FILEMAP (NIL (744 15112 (DISPLAYPEGSANDRINGS 754 . 1404) (DOHANOI 1406 . 1727) (FINDOTHER 1729 . 1914
) (HANOI 1916 . 2059) (HANOIDEMO 2061 . 2441) (MOVEDIS 2443 . 5849) (MOVERING 5851 . 6383) (RINGSHADE 
6385 . 6613) (SETUPRINGBITMAPS 6615 . 9694) (TRACK 9696 . 10068) (WHANOI 10070 . 14801) (XHANOI 14803
 . 15110)))))
STOP