Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

JEU DU CISEAUX


Information sur la source

Catégorie :Jeux Source .NET ( DotNet ) Classé sous : randomize, picturebox, vba excel, timer, combobox Niveau : Débutant Date de création : 07/08/2008 Date de mise à jour : 16/08/2008 16:30:38 Vu / téléchargé: 3 241 / 198

Note :
Aucune note

Commentaire sur cette source (13)
Ajouter un commentaire et/ou une note

Description

Cliquez pour voir la capture en taille normale
Pour l'été le petit jeu du ciseaux, de la feuille, de la pierre et du puits. Vous connaissez? Vous pouvez chronométrer vos parties.
Le code est en VB2005 et en VBA Excel
 

Source

  • Imports System.IO
  • Public Class Form1
  • ' ouvrir les fichiers dans leur programme
  • 'C'est nécessaire pour ouvrir n'importe quel dossier, il déclare ce qui va être exécuté plus tard
  • Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Integer, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Integer) As Integer
  • Private m_ControlCount As Int16 = 0 'image dans bouton
  • Dim ChoixJoueur As Long ' Jeu
  • 'Label défilant
  • Dim Address As String
  • Dim response As DialogResult
  • Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  • Randomize()
  • btnciseaux.Enabled = False 'boutons inactifs à l'ouverture
  • btnfeuille.Enabled = False
  • btnpierre.Enabled = False
  • btnpuits.Enabled = False
  • btnstart.Enabled = False
  • End Sub
  • #Region "Jeu"
  • Sub Choix(ByVal Valeur As Long)
  • ChoixJoueur = Valeur
  • PictureBox1.Image = Nothing
  • End Sub
  • Private Sub btnciseaux_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnciseaux.Click
  • Call Choix(1)
  • Startjeu()
  • PictureBox2.Image = My.Resources.ciseaux
  • End Sub
  • Private Sub btnfeuille_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfeuille.Click
  • Call Choix(2)
  • Startjeu()
  • PictureBox2.Image = My.Resources.feuille
  • End Sub
  • Private Sub btnpierre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpierre.Click
  • Call Choix(3)
  • Startjeu()
  • PictureBox2.Image = My.Resources.pierre
  • End Sub
  • Private Sub btnpuits_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpuits.Click
  • Call Choix(4)
  • Startjeu()
  • PictureBox2.Image = My.Resources.puits
  • End Sub
  • Sub Startjeu()
  • 'Ordinateur
  • Dim ChoixOrdi As Long
  • ChoixOrdi = Int(4 * Rnd() + 1)
  • Select Case ChoixOrdi
  • Case 1
  • PictureBox1.Image = My.Resources.ciseaux
  • Case 2
  • PictureBox1.Image = My.Resources.feuille
  • Case 3
  • PictureBox1.Image = My.Resources.pierre
  • Case 4
  • PictureBox1.Image = My.Resources.puits
  • End Select
  • 'Résultat
  • If ChoixJoueur = 1 And ChoixOrdi = 2 Then
  • Label2.Text = "Gagné, les ciseaux coupent la feuille"
  • Label1.Text = CStr(CDbl(Label1.Text) + 1)
  • ElseIf ChoixJoueur = 2 And ChoixOrdi = 4 Then
  • Label2.Text = "Gagné, la feuille bouche le puits"
  • Label1.Text = CStr(CDbl(Label1.Text) + 1)
  • ElseIf ChoixJoueur = 2 And ChoixOrdi = 3 Then
  • Label2.Text = "Gagné, la feuille enveloppe la pierre "
  • Label1.Text = CStr(CDbl(Label1.Text) + 1)
  • ElseIf ChoixJoueur = 3 And ChoixOrdi = 1 Then
  • Label2.Text = "Gagné, la pierre émousse les ciseaux"
  • Label1.Text = CStr(CDbl(Label1.Text) + 1)
  • ElseIf ChoixJoueur = 4 And ChoixOrdi = 3 Then
  • Label2.Text = "Gagné, la pierre tombe dans le puits"
  • Label1.Text = CStr(CDbl(Label1.Text) + 1)
  • ElseIf ChoixJoueur = 4 And ChoixOrdi = 1 Then
  • Label2.Text = "Gagné, les ciseaux tombent dans le puits"
  • Label1.Text = CStr(CDbl(Label1.Text) + 1)
  • ElseIf ChoixJoueur = ChoixOrdi Then
  • Label2.Text = "Egalité"
  • Else
  • Label2.Text = "Perdu"
  • Label3.Text = CStr(CDbl(Label3.Text) + 1)
  • End If
  • End Sub
  • #End Region
  • #Region "Chronométrage"
  • 'Chrono
  • Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  • Label7.Text = CStr(CDbl(Label7.Text) + 1)
  • End Sub
  • ' On arrête le chrono
  • Private Sub Label7_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label7.TextChanged
  • If Label7.Text = ComboBox1.Text Then
  • Timer1.Enabled = False
  • tmrImage.Stop()
  • btnciseaux.Enabled = False 'boutons inactifs
  • btnfeuille.Enabled = False
  • btnpierre.Enabled = False
  • btnpuits.Enabled = False
  • timdef.Enabled = False
  • Label9.Text = ("Terminé,cliquez sur Start pour rejouer")
  • My.Computer.Audio.Play(Application.StartupPath & "\ringin.wav", AudioPlayMode.Background) 'Mettre le fichier wav dans le dossier 'Debug'
  • Label7.Text = "0"
  • End If
  • End Sub
  • ' On fait démarrer le chrono
  • Private Sub btnstart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstart.Click
  • Timer1.Enabled = True
  • btnciseaux.Enabled = True 'boutons actifs
  • btnfeuille.Enabled = True
  • btnpierre.Enabled = True
  • btnpuits.Enabled = True
  • tmrImage.Start()
  • Label1.Text = "0"
  • Label2.Text = ""
  • Label3.Text = "0"
  • Label9.Text = ""
  • 'On démarre le label défilant
  • txtStart.Text = StrConv(txtStart.Text, VbStrConv.ProperCase)
  • If Trim(txtStart.Text) = "" Then
  • Address = "et Bonjour"
  • Else
  • Address = txtStart.Text
  • End If
  • lblName.Text = Address
  • lblMessage.Text = " Bienvenue " + Address & " et bon jeu. "
  • timdef.Enabled = True
  • End Sub
  • Private Sub tmrImage_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrImage.Tick
  • ' Mettre l'image dans le bouton
  • btnstart.Image = imgList.Images(m_ControlCount)
  • 'On fait défiler les images
  • m_ControlCount = (m_ControlCount + 1) Mod imgList.Images.Count
  • End Sub
  • #End Region
  • #Region "Résultats"
  • Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
  • Dim time As String 'date et heure
  • SaveFileDialog1.InitialDirectory = Application.StartupPath & "\Scores\"
  • SaveFileDialog1.Filter = "Fichier TEXTE(*.txt)|*.txt"
  • SaveFileDialog1.FilterIndex = 0
  • If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
  • If File.Exists(SaveFileDialog1.FileName) Then File.Delete(SaveFileDialog1.FileName)
  • 'Si le fichier existe deja il est supprimé.
  • Dim Fichier As IO.StreamWriter = File.AppendText(SaveFileDialog1.FileName)
  • Dim Chaine As String = ""
  • time = CStr(Now) 'on inscrit la date et l'heure
  • Chaine = time & vbNewLine & "Résultats en " & ComboBox1.Text & " secondes" & vbNewLine & Label5.Text & ": " & Label1.Text & vbNewLine & "Ordinateur: " & Label3.Text
  • 'La chaine est ecrite dans le fichier.
  • Fichier.Write(Chaine)
  • Fichier.Close()
  • End If
  • End Sub
  • ' Ouvrir programme par défaut
  • Public Function OpenFile(ByRef File As String, Optional ByRef Parametres As String = "") As Object
  • ShellExecute(Handle.ToInt32, "Open", File, Parametres, My.Application.Info.DirectoryPath, 1)
  • OpenFile = 1
  • End Function
  • Dim fichier As String
  • Private Sub btnresult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnresult.Click
  • 'on cherche le chemin
  • Dim dlg As New OpenFileDialog
  • If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
  • fichier = dlg.FileName
  • End If
  • Try
  • 'Ouvre le fichier dans son programme par défaut
  • OpenFile(fichier)
  • Catch ex As Exception
  • End Try
  • End Sub
  • #End Region
  • #Region "Label défilant"
  • Private Sub timdef_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timdef.Tick
  • 'créez un texte défilant dans la boîte de label
  • lblMessage.Text = Mid(lblMessage.Text, 2, lblMessage.Text.Length - 1) & Mid(lblMessage.Text, 1, 1)
  • End Sub
  • Private Sub txtStart_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtStart.TextChanged
  • btnstart.Enabled = True
  • Label5.Text = txtStart.Text
  • If txtStart.Text = "" Then
  • txtStart.Text = "Joueur"
  • End If
  • If txtStart.TextLength > 9 Then
  • response = MessageBox.Show("Votre nom est trop long, faites-le moins de 9 lettres. S'il vous plaît!", "Nom top long", _
  • MessageBoxButtons.OK)
  • If response = Windows.Forms.DialogResult.OK Then
  • txtStart.Focus()
  • txtStart.Text = ""
  • End If
  • Exit Sub
  • End If
  • End Sub
  • #End Region
  • End Class
Imports System.IO
Public Class Form1
    ' ouvrir les fichiers dans leur programme 
    'C'est nécessaire pour ouvrir n'importe quel dossier, il déclare ce qui va être exécuté plus tard 
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Integer, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Integer) As Integer
    Private m_ControlCount As Int16 = 0 'image dans bouton 
    Dim ChoixJoueur As Long ' Jeu
    'Label défilant
    Dim Address As String
    Dim response As DialogResult
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Randomize()
        btnciseaux.Enabled = False 'boutons inactifs à l'ouverture
        btnfeuille.Enabled = False
        btnpierre.Enabled = False
        btnpuits.Enabled = False
        btnstart.Enabled = False
    End Sub
#Region "Jeu"
    Sub Choix(ByVal Valeur As Long)
        ChoixJoueur = Valeur
        PictureBox1.Image = Nothing
    End Sub
    Private Sub btnciseaux_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnciseaux.Click
        Call Choix(1)
        Startjeu()
        PictureBox2.Image = My.Resources.ciseaux
    End Sub

    Private Sub btnfeuille_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfeuille.Click
        Call Choix(2)
        Startjeu()
        PictureBox2.Image = My.Resources.feuille
    End Sub

    Private Sub btnpierre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpierre.Click
        Call Choix(3)
        Startjeu()
        PictureBox2.Image = My.Resources.pierre
    End Sub

    Private Sub btnpuits_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpuits.Click
        Call Choix(4)
        Startjeu()
        PictureBox2.Image = My.Resources.puits
    End Sub

    Sub Startjeu()

        'Ordinateur
        Dim ChoixOrdi As Long
        ChoixOrdi = Int(4 * Rnd() + 1)

        Select Case ChoixOrdi
            Case 1
                PictureBox1.Image = My.Resources.ciseaux
            Case 2
                PictureBox1.Image = My.Resources.feuille
            Case 3
                PictureBox1.Image = My.Resources.pierre
            Case 4
                PictureBox1.Image = My.Resources.puits
        End Select

        'Résultat
        If ChoixJoueur = 1 And ChoixOrdi = 2 Then
            Label2.Text = "Gagné, les ciseaux coupent la feuille"
            Label1.Text = CStr(CDbl(Label1.Text) + 1)
        ElseIf ChoixJoueur = 2 And ChoixOrdi = 4 Then
            Label2.Text = "Gagné, la feuille bouche le puits"
            Label1.Text = CStr(CDbl(Label1.Text) + 1)
        ElseIf ChoixJoueur = 2 And ChoixOrdi = 3 Then
            Label2.Text = "Gagné, la feuille enveloppe la pierre "
            Label1.Text = CStr(CDbl(Label1.Text) + 1)
        ElseIf ChoixJoueur = 3 And ChoixOrdi = 1 Then
            Label2.Text = "Gagné, la pierre émousse les ciseaux"
            Label1.Text = CStr(CDbl(Label1.Text) + 1)
        ElseIf ChoixJoueur = 4 And ChoixOrdi = 3 Then
            Label2.Text = "Gagné, la pierre tombe dans le puits"
            Label1.Text = CStr(CDbl(Label1.Text) + 1)
        ElseIf ChoixJoueur = 4 And ChoixOrdi = 1 Then
            Label2.Text = "Gagné, les ciseaux tombent dans le puits"
            Label1.Text = CStr(CDbl(Label1.Text) + 1)
        ElseIf ChoixJoueur = ChoixOrdi Then
            Label2.Text = "Egalité"
        Else
            Label2.Text = "Perdu"
            Label3.Text = CStr(CDbl(Label3.Text) + 1)
        End If

    End Sub
#End Region
#Region "Chronométrage"
    'Chrono
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label7.Text = CStr(CDbl(Label7.Text) + 1)
    End Sub
    ' On arrête le chrono
    Private Sub Label7_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label7.TextChanged
        If Label7.Text = ComboBox1.Text Then
            Timer1.Enabled = False
            tmrImage.Stop()
            btnciseaux.Enabled = False 'boutons inactifs
            btnfeuille.Enabled = False
            btnpierre.Enabled = False
            btnpuits.Enabled = False
            timdef.Enabled = False
            Label9.Text = ("Terminé,cliquez sur Start pour rejouer")
            My.Computer.Audio.Play(Application.StartupPath & "\ringin.wav", AudioPlayMode.Background) 'Mettre le fichier wav dans le dossier 'Debug'
            Label7.Text = "0"
        End If

    End Sub

    ' On fait démarrer le chrono
    Private Sub btnstart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstart.Click
        Timer1.Enabled = True
        btnciseaux.Enabled = True 'boutons actifs
        btnfeuille.Enabled = True
        btnpierre.Enabled = True
        btnpuits.Enabled = True
        tmrImage.Start()
        Label1.Text = "0"
        Label2.Text = ""
        Label3.Text = "0"
        Label9.Text = ""
        'On démarre le label défilant
        txtStart.Text = StrConv(txtStart.Text, VbStrConv.ProperCase)
        If Trim(txtStart.Text) = "" Then
            Address = "et Bonjour"
        Else
            Address = txtStart.Text
        End If
        lblName.Text = Address
        lblMessage.Text = "  Bienvenue  " + Address & "  et bon jeu.  "
        timdef.Enabled = True
    End Sub

    Private Sub tmrImage_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrImage.Tick
        ' Mettre l'image dans le bouton
        btnstart.Image = imgList.Images(m_ControlCount)
        'On fait défiler les images 
        m_ControlCount = (m_ControlCount + 1) Mod imgList.Images.Count
    End Sub
#End Region
#Region "Résultats"
    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        Dim time As String 'date et heure
        SaveFileDialog1.InitialDirectory = Application.StartupPath & "\Scores\"
        SaveFileDialog1.Filter = "Fichier TEXTE(*.txt)|*.txt"
        SaveFileDialog1.FilterIndex = 0

        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
          
            If File.Exists(SaveFileDialog1.FileName) Then File.Delete(SaveFileDialog1.FileName)
            'Si le fichier existe deja il est supprimé.
            Dim Fichier As IO.StreamWriter = File.AppendText(SaveFileDialog1.FileName)
            Dim Chaine As String = ""
            time = CStr(Now) 'on inscrit la date et l'heure
            Chaine = time & vbNewLine & "Résultats en " & ComboBox1.Text & " secondes" & vbNewLine & Label5.Text & ": " & Label1.Text & vbNewLine & "Ordinateur: " & Label3.Text
            'La chaine est ecrite dans le fichier.
            Fichier.Write(Chaine)
            Fichier.Close()
        End If
    End Sub

    ' Ouvrir programme par défaut 
    Public Function OpenFile(ByRef File As String, Optional ByRef Parametres As String = "") As Object
        ShellExecute(Handle.ToInt32, "Open", File, Parametres, My.Application.Info.DirectoryPath, 1)
        OpenFile = 1
    End Function
    Dim fichier As String
    Private Sub btnresult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnresult.Click
        'on cherche le chemin
        Dim dlg As New OpenFileDialog
        If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            fichier = dlg.FileName
        End If
        Try
            'Ouvre le fichier dans son programme par défaut
            OpenFile(fichier)
        Catch ex As Exception
        End Try
    End Sub
#End Region
#Region "Label défilant"
    Private Sub timdef_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timdef.Tick
        'créez un texte défilant dans la boîte de label
        lblMessage.Text = Mid(lblMessage.Text, 2, lblMessage.Text.Length - 1) & Mid(lblMessage.Text, 1, 1)

    End Sub
    Private Sub txtStart_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtStart.TextChanged
        btnstart.Enabled = True
        Label5.Text = txtStart.Text
        If txtStart.Text = "" Then
            txtStart.Text = "Joueur"
       End If
        If txtStart.TextLength > 9 Then
            response = MessageBox.Show("Votre nom est trop long, faites-le moins de 9 lettres. S'il vous plaît!", "Nom top long", _
            MessageBoxButtons.OK)
            If response = Windows.Forms.DialogResult.OK Then
                txtStart.Focus()
                txtStart.Text = ""
            End If
            Exit Sub
        End If
    End Sub
#End Region
End Class

Conclusion

un code trés simple avec Randomize
 

Fichier Zip

Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip

Historique

07 août 2008 18:50:17 :
J'ai simplifié la source avec des boutons qui agissent directement.
08 août 2008 11:13:55 :
Suppression de la répétition "égalité" en VB2005.Restructuration du code en VBA Excel grâce à l'aide précieuse de US_30 que je remercie, j'ai seulement changé les boutonsoptions par des boutons comme dans le VB2005.
08 août 2008 15:27:18 :
Rectifications dans les résultats
11 août 2008 16:16:32 :
Chronométrages des parties avec choix dans un comboBox. Animation du bouton "Start" pendant le cours du jeu.
12 août 2008 14:23:24 :
Ajout du chronométrage en VBA. La difficulté se trouve dans le fait qu'il n'y a pas de Timer en VBA, donc il faut utiliser un module.Le choix est fait comme en VB2005 avec un comboBox.
13 août 2008 08:19:14 :
En VBA Excel: Affichage toutes les 5 secondes de la durée du jeu.
13 août 2008 11:16:53 :
Problème résolue pour les erreurs de saisies en VBA: MsgBox "Erreur de saisie" Application.OnTime Now + TimeValue("00:00:01"), "TimerOff" on régle sur 1 seconde, sinon sur 0 seconde cela crée un bug.
16 août 2008 16:30:39 :
On peut enregistrer les résultats des parties dans un fichier txt, avec les temps ainsi que la date et aussi un label défilant avec votre nom pendant la durée de la partie.

Commentaires et avis

signaler à un administrateur
Commentaire de pape0 le 07/08/2008 13:52:40

dans l'histoire le puit nexiste pas!

signaler à un administrateur
Commentaire de Le Pivert le 07/08/2008 14:59:01

Tout à fait exact, j'ai la confirmation sur le Net.J'y ai joué il y a si longtemps! (J'aurais dû me renseigner avant).Merci et voici la confirmation.
http://fr.wikipedia.org/wiki/Pierre-feuille-ciseaux.
@+ PaPeo

signaler à un administrateur
Commentaire de Le Pivert le 07/08/2008 16:01:47

En lisant en détail le sujet sur le site nommé plus haut, il existe une variante avec le puits, donc je laisse la source en l'état.Ceci n'a pas trop d'importance vu qu'il s'agit de code ici-même.Mais la remarque était judicieuse.

signaler à un administrateur
Commentaire de us_30 le 07/08/2008 23:11:58

Bonsoir,

J'ai regardé ton code en VBA, il y a encore pas mal d'améliorations possibles...

Par exemple, le simple fait de choisir des "OptionButton" au lieu de "CheckBox", permet un code allégé, car la gestion du choix d'une seule possibilité est automatique. Ensuite le clique sur ces "optionbutton" peut facilement se simplifier avec une SUB commune. Pour retenir le choix, il suffit de récupérer l'optionbutton sélectionné pour le joueur, et de mettre le choix de l'ordi dans une variable; au lieu de passer par des objets "Textbox" pour retenir ces nombres...
Les lignes :
If TextBox2.Text = TextBox1.Text Then Label2.Text = "Egalité"
présentent de nombreuses fois, ne sont pas utiles... Juste un fois, suffit.
Le titre de l'userform1 (caption) peut se définir directement dans les propriétés sans passer par le codage (plus simple, donc.)

Donc, en remplaçant les CheckBox par des OptionButtons, en retirant les Textbox, et en suivant mes remarques, j'ai obtenu le code simplifié suivant :

=

Option Explicit

Sub Choix(Fichier As String)
    Me.Image1.Picture = LoadPicture(ActiveWorkbook.Path & "\" & Fichier & ".jpg")
    Image2.Picture = Nothing
    Label1.Caption = vbNullString
End Sub

Private Sub OptionButton1_Click()
Call Choix("ciseaux")
End Sub

Private Sub OptionButton2_Click()
Call Choix("feuille")
End Sub

Private Sub OptionButton3_Click()
Call Choix("pierre")
End Sub

Private Sub OptionButton4_Click()
Call Choix("puits")
End Sub

Private Sub UserForm_Activate()
Randomize Timer
OptionButton1_Click
End Sub

Private Sub CommandButton1_Click()
    
    'Ordinateur
    Dim Img As String, ChoixOrdi As Long
    
    ChoixOrdi = Int(4 * Rnd() + 1)
    
    Select Case ChoixOrdi
        Case 1
        Img = "ciseaux"
        Case 2
        Img = "feuille"
        Case 3
        Img = "pierre"
        Case 4
        Img = "puits"
    End Select
    
    Me.Image2.Picture = LoadPicture(ActiveWorkbook.Path & "\" & Img & ".jpg")
  
   'Joueur
   Dim ChoixJoueur As Long
  
   If OptionButton1 = True Then ChoixJoueur = 1
   If OptionButton2 = True Then ChoixJoueur = 2
   If OptionButton3 = True Then ChoixJoueur = 3
   If OptionButton4 = True Then ChoixJoueur = 4
  
   'Résultat
    If ChoixJoueur = 1 And ChoixOrdi = 2 Then
        Label1 = "Gagné, le ciseaux coupe la feuille"
        Label2 = CStr(CDbl(Label2) + 1)
    ElseIf ChoixJoueur = 2 And ChoixOrdi = 4 Then
        Label1 = "Gagné, la feuille bouche le puit"
        Label2 = CStr(CDbl(Label2) + 1)
    ElseIf ChoixJoueur = 3 And ChoixOrdi = 1 Then
        Label1 = "Gagné, la pierre casse le ciseau"
        Label2 = CStr(CDbl(Label2) + 1)
    ElseIf ChoixJoueur = 4 And ChoixOrdi = 3 Then
        Label1 = "Gagné, la pierre tombe dans le puit"
        Label2 = CStr(CDbl(Label2) + 1)
    ElseIf ChoixJoueur = ChoixOrdi Then
        Label1 = "Egalité"
    Else
        Label1 = "Perdu"
        Label3 = CStr(CDbl(Label3) + 1)
    End If

End Sub

=

Je suis resté avec un code de base, mais il y a surement matière à encore quelques simplifications...

Amicalement,
Us.

signaler à un administrateur
Commentaire de us_30 le 07/08/2008 23:27:26

Re,

On penser aussi à renommer les fichiers images par leurs nombres correspondant, ainsi le SELECT CASE devient aussi inutile, et le reste du code se simplifie d'avantage.

Amicalement,
Us.

signaler à un administrateur
Commentaire de us_30 le 08/08/2008 00:06:19

Re, re...

De plus en mettant ChoixJoueur en variable global on réduit encore le codage.
Pour peu, qu'on simplifie les messages, on obtient le code court suivant :

=
Option Explicit

Dim ChoixJoueur As Long

Sub Choix(Valeur As Long)
    ChoixJoueur = Valeur
    Me.Image1.Picture = LoadPicture(ActiveWorkbook.Path & "\" & CStr(Valeur) & ".jpg")
    Image2.Picture = Nothing
    Label1.Caption = vbNullString
End Sub

Private Sub OptionButton1_Click()
Call Choix(1)
End Sub

Private Sub OptionButton2_Click()
Call Choix(2)
End Sub

Private Sub OptionButton3_Click()
Call Choix(3)
End Sub

Private Sub OptionButton4_Click()
Call Choix(4)
End Sub

Private Sub UserForm_Activate()
Randomize Timer
OptionButton1_Click
End Sub

Private Sub CommandButton1_Click()
    
    'Ordinateur
    Dim ChoixOrdi As Long
    ChoixOrdi = Int(4 * Rnd() + 1)
    Me.Image2.Picture = LoadPicture(ActiveWorkbook.Path & "\" & CStr(ChoixOrdi) & ".jpg")
    
    'Résultat
    Debug.Print ChoixJoueur & "  " & ChoixOrdi
    If ChoixJoueur = ChoixOrdi Then
        Label1 = "Egalité"
    ElseIf ((ChoixJoueur = 1) And (ChoixOrdi = 2)) Or ((ChoixJoueur = 2) And (ChoixOrdi = 4)) _
        Or ((ChoixJoueur = 3) And (ChoixOrdi = 1)) Or ((ChoixJoueur = 4) And (ChoixOrdi = 3)) Then
        Label1 = "Gagné"
        Label2 = CStr(CDbl(Label2) + 1)
    Else
        Label1 = "Perdu"
        Label3 = CStr(CDbl(Label3) + 1)
    End If

End Sub

=

Voilà, pour une première optimisation.

Amicalement,
Us.

signaler à un administrateur
Commentaire de Le Pivert le 08/08/2008 08:04:57

Je te remercie de tes conseils, j'avais déjà remplacé en VB les checkBox par des boutons. Pour le reste j'étudie cela en détail et mettrais une mise à jour.Encore merci pour ces commentaires constructifs qui vont me faire avancer d'un pas.
@+ US_30

signaler à un administrateur
Commentaire de Le Pivert le 13/08/2008 09:12:25

En VBA Excel: il va sans dire que vous pouvez faire défiler toutes les secondes en changeant  TimerOn 5000 en 1000 et les point 5 par 1, dans le module Timer.
Par contre j'ai un petit problème en cas d'erreur de saisie: impossible d'arrêter le Timer à part la méthode radicale de la fermeture du programme. Si quelqu"un à une idée elle serait la bienvenue, merci.

signaler à un administrateur
Commentaire de us_30 le 13/08/2008 15:53:24

Bonjour,

J'ai manqué un épisode ?...

Est-ce que vous parlez du Timer de Randomize ?... Si oui, alors le Timer ne peut pas s'arrêter pour la simple raison qu'il renvoit uniquement une valeur représentant le nombre de secondes écoulées depuis minuit... Et "randomize Timer", donc permet d'initialiser le générateur de nombre aléatoire sur une valeur quelconque, enfin plutôt peu évidente à deviner... sans cela, on risque de toujours obtenir la même suite de nb aléatoire...

Amicalement,
Us.

signaler à un administrateur
Commentaire de Le Pivert le 13/08/2008 16:09:13

Non c'est un Timer qui détermine un temps donné pour le jeu.C'est un module qui remplace le Timer de VB car il n'y en a pas en VBA.
Je tiens à faire une rectif sur la dernière mise à jour:
MsgBox "Erreur de saisie"
TimerOff
sufit pour arrête le Timer
Merci US_30
@+

signaler à un administrateur
Commentaire de us_30 le 13/08/2008 16:27:02

Désolé, je n'y étais pas. Je vois que tu as repris le jeu avec un chrono...
Petite remarque, pour Excel, il n'est pas obligatoire de mettre un objet "son" sur la feuille, on peut l'intégrer aussi au code VBA... c'est plus discret... voir à ce sujet : http://www.codyx.org/snippet_jouer-arreter-son-wav_42.aspx#127

Amicalement,
Us.

signaler à un administrateur