home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / hanoi2 < prev    next >
Text File  |  2020-01-01  |  1KB  |  45 lines

  1. # !/usr/local/bin/kermit +
  2.  
  3. ; From: "Nguyen, Dat" <dnguyen@siemens-emis.com>
  4. ; To: "'Frank da Cruz'" <fdc@columbia.edu>
  5. ; Subject: A Faster Towers of Hanoi Script
  6. ; Date: Tue, 15 Jan 2002 10:00:48 -0600
  7. ;
  8. ; You are very thorough in verifying input parameters, sometimes at the
  9. ; expense of the execution of recursive macro.  I reworked the Tower_Of_Hanoi
  10. ; script to separate the verification of the inputs and the execution of the
  11. ; recursive computation.  I think this pattern is applicable to similar
  12. ; recursive macros.
  13. ;
  14. ; h a n o i  --  Towers Of Hanoi
  15. ;
  16. ; Author:  Dat Thuc Nguyen, 21 Sep 2001
  17. ;  Revised 15 Jan 2002
  18. ;
  19. ; \%1 Number_of_Discs
  20. ; \%2 Start_Peg
  21. ; \%3 Goal_Peg
  22.  
  23. define Tower_Of_Hanoi {
  24.     if < \v(argc) 4 exit 1 Usage: hanoi ndisks startpeg goalpeg
  25.     if not numeric \%1 exit 1 \%1: not a number
  26.     if not numeric \%2 exit 1 \%2: not a number
  27.     if not numeric \%3 exit 1 \%3: not a number
  28.     if ( < \%1 1 || < \%2 1 || < \%3 1 ) exit 1 Numbers must be positive
  29.     if ( > \%2 3 || > \%3 3 ) exit 1 Peg must be 1 or 2 or 3
  30.     if ( = \%2 \%3 ) exit 1 Peg numbers must be distinct
  31.  
  32.     local recursive
  33.     define recursive {
  34.         if = 1 \%1 {
  35.             echo Move Top Disc from peg \%2 to peg \%3.
  36.         } else {
  37.             (recursive (- \%1 1) \%2 (- 6 (+ \%2 \%3)))
  38.             recursive 1 \%2 \%3
  39.             (recursive (- \%1 1) (- 6 (+ \%2 \%3)) \%3)
  40.         }
  41.     }
  42.     recursive \%1 \%2 \%3
  43.     exit 0
  44. }
  45.