Recherche

Jeux Vache_Toreau Sur Excel via VBA



1. Génération d’un nombre aléatoire de 4 chiffre (Bouton Générer)

Private Sub generer_Click()
Dim nbr As Integer
nbr = Int(10 * Rnd) & Int(10 * Rnd) & Int(10 * Rnd) & Int(10 * Rnd)
Nbr_Ord.Caption = nbr
Nbr_Ord.Visible = False
generer.Enabled = False
End Sub

2. La Vérification de la saisie chiffre par chiffre (fonction Test)

Function pos_just(ByVal position_chiffre As Integer) As Integer
pos_just = Mid(Nbr_Ord.Caption, position_chiffre, 1)
End Function

Sub colorer(ByVal num_essai As Integer, ByVal position_chiffre As Integer, ByVal Type_Reponse As String)
Select Case Type_Reponse

Case "T" 'Toreau
Me.Controls("cmd" & num_essai & position_chiffre).BackColor = &HFF00&

Case "V" 'vache
Me.Controls("cmd" & num_essai & position_chiffre).BackColor = &HC0FFC0
Case "N" 'Inexistante
Me.Controls("cmd" & num_essai & position_chiffre).BackColor = &HFF&
End Select

End Sub

Ceci passe par l’identification du numéro correct de chaque chiffre à travers la fonction Pos_Juste
Function pos_just(ByVal position_chiffre As Integer) As Integer
pos_just = Mid(Nbr_Ord.Caption, position_chiffre, 1)
End Function

3. Colorer les Boutons par rapport au résultat de la fonction Test

Function pos_just(ByVal position_chiffre As Integer) As Integer
pos_just = Mid(Nbr_Ord.Caption, position_chiffre, 1)
End Function

Sub colorer(ByVal num_essai As Integer, ByVal position_chiffre As Integer, ByVal Type_Reponse As String)
Select Case Type_Reponse

Case "T" 'Toreau
Me.Controls("cmd" & num_essai & position_chiffre).BackColor = &HFF00&

Case "V" 'vache
Me.Controls("cmd" & num_essai & position_chiffre).BackColor = &HC0FFC0
Case "N" 'Inexistante
Me.Controls("cmd" & num_essai & position_chiffre).BackColor = &HFF&
End Select

End Sub

4. Vérification de la saisie globale de l’utilisateur (Bouton Vérifier)

Private Sub Verifier_Click()
Dim pos1, pos2, pos3, pos4, Nbr_Essai As Integer
Nbr_Essai = ESSAI.Caption + 1
If Nbr_Essai > 12 Then
MsgBox ("Vous avez perdu!!!!!!! Vous avez le nombre maximum d'essai")
Else
If Len(SAISIE) <> 4 Then
MsgBox ("Sasir un Numéro de 4 chiffre ")
Else

ESSAI.Caption = Nbr_Essai

pos1 = Mid(SAISIE, 1, 1)
pos4 = Mid(SAISIE, 4, 1)
pos2 = Mid(SAISIE, 2, 1)
pos3 = Mid(SAISIE, 3, 1)
For j = 1 To 4
Me.Controls("CMD" & Nbr_Essai & j).Caption = Mid(SAISIE, j, 1)
Next

Call colorer(Nbr_Essai, 1, test(pos1, 1))
Call colorer(Nbr_Essai, 2, test(pos2, 2))
Call colorer(Nbr_Essai, 3, test(pos3, 3))
Call colorer(Nbr_Essai, 4, test(pos4, 4))
End If
If test(pos1, 1) & test(pos2, 2) & test(pos3, 3) & test(pos4, 4) = "TTTT" Then
MsgBox ("Bravooooooooooooooo , Vous avez gangnez le nombre est " & Nbr_Ord.Caption)
Nbr_Ord.Visible = True
Verifier.Enabled = False
sms.Visible = True
sms.Caption = "Bravooooooooooooooo , Vous avez gangnez le nombre est " & Nbr_Ord.Caption
End If
End If
End Sub

5. Recommencer un autre Jeux (Bouton Recommencer)

Private Sub Recommencer_Click()
For i = 1 To 4
For j = 1 To 12
Me.Controls("CMD" & j & i).Caption = " "
Me.Controls("CMD" & j & i).BackColor = &H8000000F
Next
Next
Verifier.Enabled = True
generer.Enabled = True
SAISIE.Value = ""
sms.Visible = False
ESSAI.Caption = 0
Nbr_Ord.Visible = False
End Sub

Auteur: N’hésitez pas à me signaler les erreurs qui peuvent glisser dans cet article afin de les corriger.

Aucun commentaire :

Enregistrer un commentaire