Plan 9 from Bell Labs’s /usr/web/sources/contrib/fgb/root/sys/src/cmd/4th/examples/mxhanoi.4th

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


\ Towers of Hanoi, Raul Deluth Miller's version
\ Provided by Marcel Hendrix

: MOVERING ( depth dir -- depth dir ) 
                 ." Moving disk '" DUP 0 .R ." '" cr ;

create TO! ( direction -- otherdirection ) 
                 0 , 2 , 1 , 
                 5 , 0 , 3 , 
                 7 , 6 ,     \ use alternate dest 

create FRO! ( direction -- otherdirection ) 
                 0 , 7 , 5 , 
                 6 , 0 , 2 , 
                 3 , 1 ,     \ use alternate source 
 
:this to! does> + @c ;
:this fro! does> + @c ;

: SMALLER ( depth direction -- depth-1 direction ) 
                 SWAP 1- SWAP ; 

: BIGGER ( depth direction -- depth+1 direction ) 
                 SWAP 1+ SWAP ; 

: HANOI ( depth direction -- depth direction ) 
   smaller 
        OVER IF  to!  RECURSE  to! 
                      movering 
                 fro! RECURSE  fro! 
           ELSE  movering 
           THEN 
   bigger ; 

.( Using a table: ) cr 10 1 hanoi 2DROP

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].