Liste
Enoncé

Visual Basic - Solution 5 ( La taupe )

 

Interface graphique :

Il y a très peu à dire au sujet de l'interface, puisqu'il s'agit de dessiner directement sur le fond de la feuille (Form1).

Code de la feuille Form1 :

Dim t(1 To 10, 1 To 10) As Long
Dim p(1 To 100) As XY 'Parcours
Dim c As Integer 'déplacement Courant (n ième Coup)

Private Sub Form_Activate( )
     Dim i As Integer
     Form1.Scale (-0.01, -0.01) - (10.01, 10.01)
     For i = 0 To 10
          Form1.Line (i, 0) - (i, 10)
          Form1.Line (0, i) - (10, i)
     Next i
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     Dim r As Double
     Dim Xnew As Integer, Ynew As Integer
     Randomize
     c = 1
     p(c).X = Int(X) + 1
     p(c).Y = Int(Y) + 1
     marquer vbGreen
     Do
          If impasse Then
               marquer vbRed
               If c = 1 Then
                    MsgBox "Aucune solution !!!"
                    Exit Do
               Else
                    'reculer d'un pas
                    c = c - 1
               End If
          Else ' Il existe donc au moins une solution
               r = Int(Rnd * 4) * 1.57 'Choix aléatoire de la direction en radians
               Xnew = p(c).X + CInt(Cos(r))
               Ynew = p(c).Y + CInt(Sin(r))
               If Xnew > 0 And Xnew <= 10 And Ynew > 0 And Ynew <= 10 Then 'pas hors-cadre
                    If t(Xnew, Ynew) = 0 Then 'case vide
                         c = c + 1
                         p(c).X = Xnew
                         p(c).Y = Ynew
                         marquer vbBlue
                    End If
               End If
          End If
     Loop Until p(c).X = 10 And p(c).Y = 10
End Sub

Sub marquer(couleur As Long)
     t(p(c).X, p(c).Y) = couleur
     Form1.Line (p(c).X - 1, p(c).Y - 1)-(p(c).X, p(c).Y), couleur, BF
     Form1.CurrentX = p(c).X - 1
     Form1.CurrentY = p(c).Y - 1
     Form1.Print c
End Sub

Function impasse() As Boolean
     impasse = True
     If p(c).X + 1 <= 10 Then If t(p(c).X + 1, p(c).Y) = 0 Then impasse = False
     If p(c).X - 1 > 0 Then If t(p(c).X - 1, p(c).Y) = 0 Then impasse = False
     If p(c).Y + 1 <= 10 Then If t(p(c).X, p(c).Y + 1) = 0 Then impasse = False
     If p(c).Y - 1 > 0 Then If t(p(c).X, p(c).Y - 1) = 0 Then impasse = False
End Function

Code à placer dans un module :

Public Type XY
     X As Integer
     Y As Integer
End Type

    ( retour à l'énoncé )