#!/usr/local/bin/kermit +

; h a n o i  --  Towers Of Hanoi
;
; Author:  Dat Thuc Nguyen, 21 Sep 2001

; \%1 Number_of_Discs
; \%2 Start_Peg
; \%3 Goal_Peg

define Tower_Of_Hanoi {
    if < \v(argc) 4 exit 1 Usage: hanoi ndisks startpeg goalpeg
    if not numeric \%1 exit 1 \%1: not a number
    if not numeric \%2 exit 1 \%2: not a number
    if not numeric \%3 exit 1 \%3: not a number
    if ( < \%1 1 || < \%2 1 || < \%3 1 ) exit 1 Numbers must be positive
    if ( > \%2 3 || > \%3 3 ) exit 1 Peg must be 1 or 2 or 3
    if ( = \%2 \%3 ) exit 1 Peg numbers must be distinct
    if = 1 \%1 {
        echo Move Top Disc from peg \%2 to peg \%3.
    } else {
        (Tower_Of_Hanoi (- \%1 1) \%2 (- 6 (+ \%2 \%3)))
        Tower_Of_Hanoi 1 \%2 \%3
        (Tower_Of_Hanoi (- \%1 1) (- 6 (+ \%2 \%3)) \%3)
    }
}
Tower_Of_Hanoi \%1 \%2 \%3
exit 0
