# Towrs of Hanoi

Hi my name is Tony and I have been working on a problem in Visual Basic class for 2 weeks now. It is the Towers of Hanoi Program. I just can't figure it out. The problem is asking that the user be able to put in the number of disks and use recursion. If you think you can help or need more information PLEASE email me or respond back. Thank you so much.

• VERSION 5.00
Begin VB.Form frmTowersOfHanoi
AutoRedraw = -1 'True
Caption = "Towers of Hanoi"
ClientHeight = 6165
ClientLeft = 60
ClientTop = 345
ClientWidth = 2685
ScaleHeight = 6165
ScaleWidth = 2685
StartUpPosition = 2 'CenterScreen
End
Attribute VB_Name = "frmTowersOfHanoi"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Option Explicit

'-------------------------------------------------------------
' The Towers of Hanoi problem is an old Asian puzzle. The
' puzzle is how to move a specified number of disks from one
' tower to another without putting a disk of larger size upon
' another. The disks start out on the first tower arranged in
' order of increasing size.
'
' Based on example by Michael Feldman, The George Washington
' University, and Elliot Koffman, Temple University.
' ISBN 0-201-50006-X
' Author: Jerry Yoakum, Southwest Missouri State University
'-------------------------------------------------------------

Dim FromTower As String * 1
Dim ToTower As String * 1
Dim AuxTower As String * 1

Private Sub MoveDisk (FromTower As String, ToTower As String, _
WhichDisk As Integer)

Print "Move disk " & WhichDisk & " from tower " _
& FromTower & " to tower " & ToTower

End Sub

Private Sub Tower (FromTower As String, ToTower As String, _
AuxTower As String, WhichDisk As Integer)

If WhichDisk = 1 Then
' stopping case
Call MoveDisk(FromTower, ToTower, 1)
Else
' recursive step
Call Tower(FromTower, AuxTower, ToTower, WhichDisk - 1)
Call MoveDisk(FromTower, ToTower, WhichDisk)
Call Tower(AuxTower, ToTower, FromTower, WhichDisk - 1)
End If

End Sub

Call Tower("A", "C", "B", 5)

End Sub

• uhm, is this a complete, code for tower of hanoi... coz i really need a code for my project, damn... my proffesors givin meee a hard time...

• : Hi my name is Tony and I have been working on a problem in Visual Basic class for 2 weeks now. It is the Towers of Hanoi Program. I just can't figure it out. The problem is asking that the user be able to put in the number of disks and use recursion. If you think you can help or need more information PLEASE email me or respond back. Thank you so much.
:
Hi, If you need code of all project, I have one. But I didn't use recursion (I used math! A pretty piece of math)[code] For Pozice = 1 To 2 ^ PocetKotoucu - 1
For CisloKotouce = PocetKotoucu To 1 Step -1
Overeni = (Pozice - 2 ^ CisloKotouce / 2) / 2 ^ CisloKotouce
If Overeni >= 0 And (Overeni - Overeni 1) = 0 Then 'Jestli je to kladne a cele
PohniKotoucem CisloKotouce
If tmrKrok.Enabled = True Then Cekej
Exit For
End If
Next CisloKotouce
Next Pozice[/code]But you should have see theBut I have it all in Czech language (variables and comments ;-)...
[gray]Lesson of Czech language:
Pozice = Position
PocetKotoucu = CountOfDiscs
CisloKotouce = NumberOfDisc
Overeni = Verification
PohniKotoucem = MoveDisc
Cekej = Wait[/gray]
This piece gives you number of disc that should be moved to show solution of Towers of Hanoi... To get place where to move isn't problem... But there's no recursion... Will it the rest of project helps?
PavlinII