Hanoi graphique avec choix de l intervalle - 1 a 10 anneaux

Soyez le premier à donner votre avis sur cette source.

Vue 2 461 fois - Téléchargée 231 fois

Description

Les tours de hanoi, avec 1 a 10 anneaux, avec choix de l'intervalle de temps entre chaque mouvement!
Recursivité aussi ...
++

Source / Exemple :

******************************************
********** DANS UNE FORM (FORM1) **********
******************************************

Dim a(100) As Integer
Dim b(100) As Integer
Dim c(100) As Integer
Dim n As Integer
Dim aH As Integer
Dim bH As Integer
Dim cH As Integer
Dim Retenu As Integer
Dim cpt As Integer
Dim test As Boolean
Dim test2 As Boolean



Private Sub Command1_Click()
    Dim j As Integer
    
    n = Val(List1.Text)
    aH = n
    bH = 0
    cH = 0
    

    
    Dim depart As Integer
    depart = 1
    Dim tempo As Integer
    tempo = 2
    Dim arrivee As Integer
    arrivee = 3
        
    For j = 0 To 100
        a(j) = 0
        b(j) = 0
        c(j) = 0
    Next j
    
        
        For j = 0 To n - 1
            a(n - j - 1) = n - j
        Next j
                
        hanoi n, 1, 3, 2
                     
             
        For j = 0 To 100
            a(j) = 0
            b(j) = 0
            c(j) = 0
        Next j
        
        MsgBox ("Fini!")
        
        cpt = 0
        Text1.Text = 0
        
        test = False
        test2 = False
        Command1.Visible = False
        
        
        
        

        
        
End Sub



Sub hanoi(nu As Integer, depart As Integer, arrivee As Integer, tempo As Integer)
    Dim i As Integer
    Dim j As Integer
    i = 0
    If (nu = 1) Then
        cpt = cpt + 1
        Text1.Text = cpt
        Select Case depart
        Case 1: While (a(i) = 0)
                i = i + 1
                Wend
                aH = aH - 1
              
                a(i) = 0
        Case 2: While (b(i) = 0)
                i = i + 1
                Wend
                bH = bH - 1
           
                b(i) = 0
        Case 3: While (c(i) = 0)
                i = i + 1
                Wend
                cH = cH - 1
             
                c(i) = 0
        End Select
        
        i = 0
        
        
        Select Case arrivee
        Case 1: While (a(n - 1 - i) <> 0 And i + 1 <> n)
                i = i + 1
                Wend
                a(n - 1 - i) = nu
                aH = aH + 1
                
                
         Case 2: While (b(n - 1 - i) <> 0 And i + 1 <> n)
                i = i + 1
                Wend
                b(n - 1 - i) = nu
                bH = bH + 1
                
                
         Case 3: While (c(n - 1 - i) <> 0 And i + 1 <> n)
                i = i + 1
                Wend
                c(n - 1 - i) = nu
                cH = cH + 1
                
        End Select
        
        
        For j = 0 To n - 1
            If a(j) <> 0 Then
                Select Case a(j)
                Case 1:
                    Label1.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label1.Left = 1320
                Case 2:
                    Label2.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label2.Left = 1200
                Case 3:
                    Label3.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label3.Left = 1080
                Case 4:
                    Label4.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label4.Left = 960
                Case 5:
                    Label5.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label5.Left = 840
                Case 6:
                    Label6.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label6.Left = 720
                Case 7:
                    Label7.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label7.Left = 600
                Case 8:
                    Label8.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label8.Left = 480
                Case 9:
                    Label9.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label9.Left = 360
                Case 10:
                    Label10.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label10.Left = 240
                End Select
            End If
            If b(j) <> 0 Then
                Select Case b(j)
                Case 1:
                    Label1.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label1.Left = 4800
                Case 2:
                    Label2.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label2.Left = 4680
                Case 3:
                    Label3.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label3.Left = 4540
                Case 4:
                    Label4.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label4.Left = 4420
                Case 5:
                    Label5.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label5.Left = 4300
                Case 6:
                    Label6.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label6.Left = 4180
                Case 7:
                    Label7.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label7.Left = 4060
                Case 8:
                    Label8.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label8.Left = 3940
                Case 9:
                    Label9.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label9.Left = 3820
                Case 10:
                    Label10.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label10.Left = 3700
                End Select
            End If
            
            If c(j) <> 0 Then
            Select Case c(j)
                Case 1:
                    Label1.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label1.Left = 8280
                Case 2:
                    Label2.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label2.Left = 8160
                Case 3:
                    Label3.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label3.Left = 8040
                Case 4:
                    Label4.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label4.Left = 7920
                Case 5:
                    Label5.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label5.Left = 7800
                Case 6:
                    Label6.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label6.Left = 7680
                Case 7:
                    Label7.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label7.Left = 7560
                Case 8:
                    Label8.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label8.Left = 7440
                Case 9:
                    Label9.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label9.Left = 7320
                Case 10:
                    Label10.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label10.Left = 7200
                End Select
            End If
        Next j
        
        Sleep Val(List2.Text)
        
    Else
        
        hanoi nu - 1, depart, tempo, arrivee
        cpt = cpt + 1
        Text1.Text = cpt
        Select Case depart
        Case 1: While (a(i) = 0)
                i = i + 1
                Wend
                aH = aH - 1
                a(i) = 0
                
        Case 2: While (b(i) = 0)
                i = i + 1
                Wend
                bH = bH - 1
                b(i) = 0
                
        Case 3: While (c(i) = 0)
                i = i + 1
                Wend
                cH = cH - 1
                c(i) = 0
        End Select
        

        i = 0
        
        Select Case arrivee
        Case 1: While (a(n - 1 - i) <> 0 And i + 1 <> n)
                i = i + 1
                Wend
                a(n - 1 - i) = nu
                aH = aH + 1
                
                
         Case 2: While (b(n - 1 - i) <> 0 And i + 1 <> n)
                i = i + 1
                Wend
                b(n - 1 - i) = nu
                bH = bH + 1
               
                
         Case 3: While (c(n - 1 - i) <> 0 And i + 1 <> n)
                i = i + 1
                Wend
                c(n - 1 - i) = nu
                cH = cH + 1
                
                
        End Select
        
        
        For j = 0 To n - 1
            If a(j) <> 0 Then
                Select Case a(j)
                Case 1:
                    Label1.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label1.Left = 1320
                Case 2:
                    Label2.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label2.Left = 1200
                Case 3:
                    Label3.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label3.Left = 1080
                Case 4:
                    Label4.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label4.Left = 960
                Case 5:
                    Label5.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label5.Left = 840
                Case 6:
                    Label6.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label6.Left = 720
                Case 7:
                    Label7.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label7.Left = 600
                Case 8:
                    Label8.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label8.Left = 480
                Case 9:
                    Label9.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label9.Left = 360
                Case 10:
                    Label10.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label10.Left = 240
                End Select
            End If
            
            If b(j) <> 0 Then
                Select Case b(j)
                Case 1:
                    Label1.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label1.Left = 4800
                Case 2:
                    Label2.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label2.Left = 4680
                Case 3:
                    Label3.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label3.Left = 4540
                Case 4:
                    Label4.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label4.Left = 4420
                Case 5:
                    Label5.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label5.Left = 4300
                Case 6:
                    Label6.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label6.Left = 4180
                Case 7:
                    Label7.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label7.Left = 4060
                Case 8:
                    Label8.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label8.Left = 3940
                Case 9:
                    Label9.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label9.Left = 3820
                Case 10:
                    Label10.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label10.Left = 3700
                End Select
            End If
            
            If c(j) <> 0 Then
            Select Case c(j)
                Case 1:
                    Label1.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label1.Left = 8280
                Case 2:
                    Label2.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label2.Left = 8160
                Case 3:
                    Label3.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label3.Left = 8040
                Case 4:
                    Label4.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label4.Left = 7920
                Case 5:
                    Label5.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label5.Left = 7800
                Case 6:
                    Label6.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label6.Left = 7680
                Case 7:
                    Label7.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label7.Left = 7560
                Case 8:
                    Label8.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label8.Left = 7440
                Case 9:
                    Label9.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label9.Left = 7320
                Case 10:
                    Label10.Top = 2500 + (350 * j) + (10 - CInt(List1.Text)) * 350
                    Label10.Left = 7200
                End Select
            End If
        Next j
        
        
        Sleep Val(List2.Text)
       
        hanoi nu - 1, tempo, arrivee, depart

End If
End Sub


Public Sub pause(durée As Long)

    Dim actu As Long
    actu = Timer
    Do Until Timer - actu >= durée
    DoEvents
    Loop
    
End Sub



Private Sub Form_Load()

test = False
test2 = False


Command1.Visible = False

Text1.Text = cpt

List1.AddItem "1"
List1.AddItem "2"
List1.AddItem "3"
List1.AddItem "4"
List1.AddItem "5"
List1.AddItem "6"
List1.AddItem "7"
List1.AddItem "8"
List1.AddItem "9"
List1.AddItem "10"

List2.AddItem "0"
List2.AddItem "10"
List2.AddItem "100"
List2.AddItem "500"
List2.AddItem "1000"


End Sub


Private Sub List1_Click()

test = True

Label1.Top = 2500 + (10 - CInt(List1.Text)) * 350
Label1.Left = 1320
Label2.Top = 2500 + 350 + (10 - CInt(List1.Text)) * 350
Label2.Left = 1200
Label3.Top = 2500 + 700 + (10 - CInt(List1.Text)) * 350
Label3.Left = 1080
Label4.Top = 2500 + 1050 + (10 - CInt(List1.Text)) * 350
Label4.Left = 960
Label5.Top = 2500 + 1400 + (10 - CInt(List1.Text)) * 350
Label5.Left = 840
Label6.Top = 2500 + 1750 + (10 - CInt(List1.Text)) * 350
Label6.Left = 720
Label7.Top = 2500 + 2100 + (10 - CInt(List1.Text)) * 350
Label7.Left = 600
Label8.Top = 2500 + 2450 + (10 - CInt(List1.Text)) * 350
Label8.Left = 480
Label9.Top = 2500 + 2800 + (10 - CInt(List1.Text)) * 350
Label9.Left = 360
Label10.Top = 2500 + 3150 + (10 - CInt(List1.Text)) * 350
Label10.Left = 240


Select Case CInt(List1.Text)
    Case 1:
    Label1.Visible = True
    Label2.Visible = False
    Label3.Visible = False
    Label4.Visible = False
    Label5.Visible = False
    Label6.Visible = False
    Label7.Visible = False
    Label8.Visible = False
    Label9.Visible = False
    Label10.Visible = False
    
   
    Case 2:
        Label1.Visible = True
Label2.Visible = True
Label3.Visible = False
    Label4.Visible = False
    Label5.Visible = False
    Label6.Visible = False
    Label7.Visible = False
    Label8.Visible = False
    Label9.Visible = False
    Label10.Visible = False

    Case 3:
        Label1.Visible = True
Label2.Visible = True
Label3.Visible = True
   Label4.Visible = False
    Label5.Visible = False
    Label6.Visible = False
    Label7.Visible = False
    Label8.Visible = False
    Label9.Visible = False
    Label10.Visible = False

    Case 4:
        Label1.Visible = True
Label2.Visible = True
Label3.Visible = True
Label4.Visible = True
    Label5.Visible = False
    Label6.Visible = False
    Label7.Visible = False
    Label8.Visible = False
    Label9.Visible = False
    Label10.Visible = False

    Case 5:
        Label1.Visible = True
Label2.Visible = True
Label3.Visible = True
Label4.Visible = True
Label5.Visible = True
    Label6.Visible = False
    Label7.Visible = False
    Label8.Visible = False
    Label9.Visible = False
    Label10.Visible = False

    Case 6:
        Label1.Visible = True
Label2.Visible = True
Label3.Visible = True
Label4.Visible = True
Label5.Visible = True
Label6.Visible = True
Label7.Visible = False
    Label8.Visible = False
    Label9.Visible = False
    Label10.Visible = False

    Case 7:
        Label1.Visible = True
Label2.Visible = True
Label3.Visible = True
Label4.Visible = True
Label5.Visible = True
Label6.Visible = True
Label7.Visible = True
    Label8.Visible = False
    Label9.Visible = False
    Label10.Visible = False

    Case 8:
        Label1.Visible = True
Label2.Visible = True
Label3.Visible = True
Label4.Visible = True
Label5.Visible = True
Label6.Visible = True
Label7.Visible = True
Label8.Visible = True
    Label9.Visible = False
    Label10.Visible = False

    Case 9:
        Label1.Visible = True
Label2.Visible = True
Label3.Visible = True
Label4.Visible = True
Label5.Visible = True
Label6.Visible = True
Label7.Visible = True
Label8.Visible = True
Label9.Visible = True
    Label10.Visible = False

    Case 10:
        Label1.Visible = True
Label2.Visible = True
Label3.Visible = True
Label4.Visible = True
Label5.Visible = True
Label6.Visible = True
Label7.Visible = True
Label8.Visible = True
Label9.Visible = True
Label10.Visible = True
    
    End Select
    
    If test = True And test2 = True Then
        Command1.Visible = True
    End If



End Sub

Private Sub List2_Click()
test2 = True

If test = True And test2 = True Then
        Command1.Visible = True
End If
End Sub









*****************************************
***************** DANS UN MODULE *********
*****************************************

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Conclusion :

La fonction sleep permet de gerer l'intervalle

Codes Sources

A voir également

Ajouter un commentaire Commentaires
Messages postés
122
Date d'inscription
mercredi 25 février 2004
Statut
Membre
Dernière intervention
2 juillet 2014

Un truc pour gagner une bonne centaine de lignes :
Remplace ton immense select case de la fin par :

Label1.Visible = False
Label2.Visible = False
Label3.Visible = False
Label4.Visible = False
Label5.Visible = False
Label6.Visible = False
Label7.Visible = False
Label8.Visible = False
Label9.Visible = False
Label10.Visible = False

Select Case CInt(List1.Text)
Case 1: Label1.Visible = True
Case 2: Label2.Visible = True
Case 3: Label3.Visible = True
Case 4: Label4.Visible = True
Case 5: Label5.Visible = True
Case 6: Label6.Visible = True
Case 7: Label7.Visible = True
Case 8: Label8.Visible = True
Case 9: Label9.Visible = True
Case 10: Label10.Visible = True
End Select

Et tu pourrais aussi utiliser une collection.

Voila =)
Messages postés
319
Date d'inscription
vendredi 21 février 2003
Statut
Membre
Dernière intervention
16 février 2009

Salut !

Une dernière choise que j'ai oubliée : déclare ta fonction sleep en private et met la dans ta feuille au lieu de prendre un module.
C'est tout !

@+ Neria
Messages postés
319
Date d'inscription
vendredi 21 février 2003
Statut
Membre
Dernière intervention
16 février 2009

Salut !

La tour de Hanoi, en voila un bon jeux de reflexion !!! Vieux Lion en a poster un pas mal du tout. Bref Voila quelques reproches :
Le jeux plante quel que soit l'interval au bout de quelques coups
Il faudrait enlever le caption des labels dans la source
Ca manque un peu de commentaires tout ca ( surtout pour un algo de maths !!! )
Pour les tableaux, fait les dynamiques et utilise un redim ( sans preserve ) pour les vider, c'est plus rapide !
Tu pourrais utiliser des index pour les labels ainsi qu'une boucle dans Form_Load pour les afficher.
Il faudrait utiliser un algo pour positionner les étages et pas y aller à la bourrin
Ce n'était pas la peine de mettre un code aussi long dans la case de texte d'en haut !
Sinon l'idée de départ est bonne. Au fait tu as pioché l'algo dans un bouquin ou tu as essayé d'en créer un toi même ?

@+ Neria

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.