Code archives/Miscellaneous/Tower of Hanoi

This code has been declared by its author to be Public Domain code.

Download source code

Tower of Hanoi by Neuro2002
This code was converted from my C++ recursive assignment. It solves the puzzle of the Tower of Hanoi. I wouldn't recommend trying any more than 20 discs..or you'll be waiting a while!
; Tower of Hanoi
; by Neuro 11-20-02
; -----------------
; Legend has it that in a temple in India, there stands 3 crystal pegs. 
; On the first peg, there are 64 gold discs, each biggest at the bottom and smallest
; at the top. The monks in the temple must move all the discs from the first peg to
; the third. But since each discs are so fragile, that only one can be moved at a time
; and a larger disc can not be placed on top of a smaller disc.
; It is said, that when monks completely move each disc to the third pegs, the temple 
; will collapse and thus be the end of the world.

; This was originally a C++ recursive assignment givin to me by my professor. 
; After messing with this for so long, I finnally got it working. So then I decided to
; try it out on Blitz. And it worked! 

; BTW, this is my first Blitz program.


Global counter=0

Function domove(n, a, c, b)   ;# of disc, first peg, last peg, middle peg
  If (n=1)
   Print a +"->"+c           ;Prints to screen after movement completed  
   counter=counter+1
  Else
   domove(n-1,a,b,c)         ;move n-1 from peg 1 -> peg 2
   domove(1,a,c,b)  		 ;move last disc(1) from peg 1 -> peg 3
   domove(n-1,b,c,a)         ;move n-1 disc from peg 2 -> peg 3
  EndIf
End Function


.Main
Print "Tower of Hanoi" : Print 
Print " 64 disc is not recommended!" : Print 
disc=Input(" Enter number of disc : ")
domove(disc,1,3,2)
Print "Total number of moves : "+ counter
WaitKey()

Comments

None.

Code Archives Forum