Jeu

Résolu
hichem94120 Messages postés 4 Date d'inscription lundi 3 novembre 2008 Statut Membre Dernière intervention 13 janvier 2009 - 11 janv. 2009 à 00:25
hichem94120 Messages postés 4 Date d'inscription lundi 3 novembre 2008 Statut Membre Dernière intervention 13 janvier 2009 - 13 janv. 2009 à 22:52
Bonjour à tous,
Voila je suis débutant et j'ai entrepris de réaliser un jeu de mémoire. Mon problème et d'écrire un algorithme qui contrôlera les cases des tableaux:
il faudrait que lorsque je clique une première fois, puis une seconde fois et que quand les deux cases soit de la même couleurs , ces deux cases restent activent et quand les couleurs ne sont pas pareils que les cases se désactives. Après plusieurs jours de recherchent je n'arrive pas à avancer.
Merci à vous.

Public Class Form1
    Dim tableau1(5, 5) As Button
    Dim tableau2(5, 5) As Button
    Dim i As Integer
    Dim j As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Size = New Size(700, 600)

        For i As Integer = 0 To 5
            For j As Integer = 0 To 5
                tableau1(i, j) = New Button
                tableau1(i, j).Visible = True
                Me.Controls.Add(tableau1(i, j))

                tableau1(i, j).Tag = i.ToString + ";" + j.ToString

                AddHandler tableau1(i, j).Click, AddressOf tableau_Click

                tableau1(i, j).Top = i * 100
                tableau1(i, j).Left = j * 100
                tableau1(i, j).Width = 90
                tableau1(i, j).Height = 90
                tableau1(i, j).BackColor = Color.Transparent
            Next
        Next

        For i As Integer = 0 To 5
            For j As Integer = 0 To 5

                tableau2(i, j) = New Button

                tableau2(i, j).Visible = True
                Me.Controls.Add(tableau2(i, j))

                tableau2(i, j).Top = i * 100
                tableau2(i, j).Left = j * 100
                tableau2(i, j).Width = 90
                tableau2(i, j).Height = 90

            Next
        Next
        tableau2(0, 0).BackColor = Color.Yellow
        tableau2(0, 1).BackColor = Color.Yellow
        tableau2(0, 2).BackColor = Color.DarkBlue
        tableau2(0, 3).BackColor = Color.DarkBlue
        tableau2(0, 4).BackColor = Color.AliceBlue
        tableau2(1, 0).BackColor = Color.DarkMagenta
        tableau2(1, 1).BackColor = Color.DarkMagenta
        tableau2(1, 2).BackColor = Color.Indigo
        tableau2(1, 3).BackColor = Color.Indigo
        tableau2(1, 4).BackColor = Color.AliceBlue
        tableau2(1, 5).BackColor = Color.DarkTurquoise
        tableau2(2, 0).BackColor = Color.Maroon
        tableau2(2, 1).BackColor = Color.Maroon
        tableau2(2, 2).BackColor = Color.Orange
        tableau2(2, 3).BackColor = Color.Orange
        tableau2(2, 4).BackColor = Color.Azure
        tableau2(2, 5).BackColor = Color.DarkSeaGreen
        tableau2(3, 0).BackColor = Color.Black
        tableau2(3, 1).BackColor = Color.Black
        tableau2(3, 2).BackColor = Color.Turquoise
        tableau2(3, 3).BackColor = Color.Turquoise
        tableau2(3, 4).BackColor = Color.Azure
        tableau2(3, 5).BackColor = Color.DarkTurquoise
        tableau2(4, 0).BackColor = Color.Beige
        tableau2(4, 1).BackColor = Color.Beige
        tableau2(4, 2).BackColor = Color.BlanchedAlmond
        tableau2(4, 3).BackColor = Color.BlanchedAlmond
        tableau2(4, 4).BackColor = Color.DarkTurquoise
        tableau2(4, 5).BackColor = Color.AliceBlue
        tableau2(5, 0).BackColor = Color.DarkSalmon
        tableau2(5, 1).BackColor = Color.DarkSalmon
        tableau2(5, 2).BackColor = Color.DeepSkyBlue
        tableau2(5, 3).BackColor = Color.DeepSkyBlue
        tableau2(5, 4).BackColor = Color.Goldenrod
        tableau2(5, 5).BackColor = Color.Goldenrod

        For i As Integer = 0 To 5
            For j As Integer = 0 To 5
                Randomize()
                Dim value1 As Integer = CInt(Int((6 * Rnd())))
                Dim value2 As Integer = CInt(Int((6 * Rnd())))

                Dim button1 As Color = tableau2(i, j).BackColor
                tableau2(i, j).BackColor = tableau2(value1, value2).BackColor
                tableau2(value1, value2).BackColor = button1

            Next
        Next
     
    End Sub
    Private Sub tableau_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim coord() As String

        coord = Split(sender.tag, ";")

        i = CType(coord(0), Integer)
        j = CType(coord(1), Integer)

        'Debug.WriteLine("Touche activée" )

        If tableau1(i, j).BackColor.Equals(Color.Transparent) Then

            tableau1(i, j).Visible = False
        End If

    End Sub

    Private Sub JOUER_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JOUER.Click
        For i As Integer = 0 To 5
            For j As Integer = 0 To 5
                Randomize()
                Dim value1 As Integer = CInt(Int((6 * Rnd())))
                Dim value2 As Integer = CInt(Int((6 * Rnd())))

                Dim button1 As Color = tableau2(i, j).BackColor
                tableau2(i, j).BackColor = tableau2(value1, value2).BackColor
                tableau2(value1, value2).BackColor = button1

            Next
        Next

    End Sub
End Class

5 réponses

hichem94120 Messages postés 4 Date d'inscription lundi 3 novembre 2008 Statut Membre Dernière intervention 13 janvier 2009
13 janv. 2009 à 22:52
En effet FAUVE le code est le même , c'est pour le même projet. Un gros merci en tous cas pour le code.
3
hichem94120 Messages postés 4 Date d'inscription lundi 3 novembre 2008 Statut Membre Dernière intervention 13 janvier 2009
11 janv. 2009 à 17:38
personne?????
0
Benythefrog Messages postés 39 Date d'inscription lundi 12 janvier 2009 Statut Membre Dernière intervention 12 février 2009
12 janv. 2009 à 15:13
as tu essayer avec le parametre de la couleur
il faut ke tu fasse un test sur la couleur de ta case

if  tableau2(3, 0).BackColor = tableau2(3, 1).BackColor then
      ...
apré je saispas comment il faut faire pour les laisser active dsl...
0
hichem94120 Messages postés 4 Date d'inscription lundi 3 novembre 2008 Statut Membre Dernière intervention 13 janvier 2009
12 janv. 2009 à 20:43
merci, je vais essayer de suite.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_fauve Messages postés 661 Date d'inscription vendredi 2 décembre 2005 Statut Membre Dernière intervention 23 mars 2011 8
13 janv. 2009 à 22:33
Salut,

Tiens, tiens, ce petit jeu me rappelle quelque chose, il y a un autre membre qui a exactement le même code que toi alors je vais faire simple : je te file la class Form1 en entier et tu la remplace la tienne par la mienne (j'ai bien commenté et ça fonctionne du tonnerre) :





Public Class
 Form1
    Inherits System.Windows.Forms.Form
    Dim tableau1(55As Button
    Dim tableau2(55As Button
    Dim i, j, k, l As Integer
    Dim ibouton1, jbouton1, ibouton2, jbouton2 As Integer
    Dim clicbouton2 As Boolean =   False

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Size  = New Size(700700)
        Me.StartPosition =  FormStartPosition.CenterScreen

         For  i As Integer  = 0 To 5
            For j As Integer =   0  To 5
                tableau1(i, j)  = New Button
                tableau1(i, j).Visible =   True
                Me.Controls.Add(tableau1(i, j))

                tableau1(i, j).Tag  = i.ToString +  ";"  + j.ToString

                AddHandler tableau1(i, j).Click, AddressOf tableau_Click

                tableau1(i, j).Top  =  i *  100
                tableau1(i, j).Left  =  j * 100
                tableau1(i, j).Width =   90
                tableau1(i, j).Height  = 90
                tableau1(i, j).BackColor =  Color.Gray
             Next
        Next

        For i As Integer  = 0 To 5
            For j As Integer =   0  To 5

                tableau2(i, j)  = New Button

                tableau2(i, j).Visible =   True
                Me.Controls.Add(tableau2(i, j))

                tableau2(i, j).Top  = i * 100
                tableau2(i, j).Left =  j *  100
                tableau2(i, j).Width  = 90
                tableau2(i, j).Height =   90

            Next
        Next
        tableau2(00).BackColor  = Color.Yellow
        tableau2(01).BackColor =  Color.Yellow
        tableau2( 0 , 2).BackColor  = Color.DarkBlue
        tableau2(03).BackColor =  Color.DarkBlue
        tableau2( 0 , 4).BackColor  = Color.AliceBlue
        tableau2(10).BackColor =  Color.DarkMagenta
        tableau2( 1 , 1).BackColor  = Color.DarkMagenta
        tableau2(12).BackColor =  Color.Indigo
        tableau2( 1 , 3).BackColor  = Color.Indigo
        tableau2(14).BackColor =  Color.AliceBlue
        tableau2( 1 , 5).BackColor  = Color.DarkTurquoise
        tableau2(20).BackColor =  Color.Maroon
        tableau2( 2 , 1).BackColor  = Color.Maroon
        tableau2(22).BackColor =  Color.Orange
        tableau2( 2 , 3).BackColor  = Color.Orange
        tableau2(24).BackColor =  Color.Azure
        tableau2( 2 , 5).BackColor  = Color.DarkSeaGreen
        tableau2(30).BackColor =  Color.Black
        tableau2( 3 , 1).BackColor  = Color.Black
        tableau2(32).BackColor =  Color.Turquoise
        tableau2( 3 , 3).BackColor  = Color.Turquoise
        tableau2(34).BackColor =  Color.Azure
        tableau2( 3 , 5).BackColor  = Color.DarkTurquoise
        tableau2(40).BackColor =  Color.Beige
        tableau2( 4 , 1).BackColor  = Color.Beige
        tableau2(42).BackColor =  Color.DarkOrange
        tableau2( 4 , 3).BackColor  = Color.DarkOrange
        tableau2(44).BackColor =  Color.DarkTurquoise
        tableau2( 4 , 5).BackColor  = Color.AliceBlue
        tableau2(50).BackColor =  Color.DarkSalmon
        tableau2( 5 , 1).BackColor  = Color.DarkSalmon
        tableau2(52).BackColor =  Color.DeepSkyBlue
        tableau2( 5 , 3).BackColor  = Color.DeepSkyBlue
        tableau2(54).BackColor =  Color.Goldenrod
        tableau2( 5 , 5).BackColor  = Color.Goldenrod

        For i As Integer =   0  To 5
            For j As Integer  = 0 To 5
                Randomize()
                Dim value1 As Integer =   CInt (Int((6 * Rnd())))
                Dim value2 As Integer  = CInt(Int((6 * Rnd())))

                Dim button1 As Color =  tableau2(i, j).BackColor
                tableau2(i, j).BackColor = tableau2(value1, value2).BackColor
                tableau2(value1, value2).BackColor = button1

             Next
        Next
    End Sub

    Private Sub tableau_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim coord() As String
        ' Variable que restera à True si toutes
        ' les couleurs ont été trouvées
        Dim colorsfound As Boolean  = True

        coord =  Split(sender.tag,  ";" )

        ' Si c'est le premier bouton qu'on clic
         If  clicbouton2  = False Then
            ' On récupère les coordonnées
            ibouton1 =   CInt (coord(0))
            jbouton1  = CInt(coord(1))

            ' On rend invisible le bouton gris
            tableau1(ibouton1, jbouton1).Visible =   False

            ' On met cette variable à True pour dire que le
            ' prochain clic sera celui du 2° bouton
            clicbouton2  = True

            ' C'est le clic sur le deuxième bouton
        ElseIf clicbouton2 =   True Then

            ' Avec les 2 boucles For, on désactive
            ' le clic sur un autre bouton jusqu'à ce
            ' que les boutons soit vérifiés et correct
            ' soit qu'ils sont pas correct et remis en
            ' gris
            For nb1 As Integer  = 0 To 5
                For nb2 As Integer =   0  To 5
                    tableau1(nb1, nb2).Enabled  = False
                Next
            Next
            ' On récupère les coordonnées
            ibouton2 =   CInt (coord(0))
            jbouton2  = CInt(coord(1))

            ' On rend invisible le bouton gris
            tableau1(ibouton2, jbouton2).Visible =   False

            ' Si les couleurs entre le 1° bouton cliqué et le 2° bouton sont identiques
            If tableau2(ibouton1, jbouton1).BackColor  = tableau2(ibouton2, jbouton2).BackColor Then
                ' On ne fait rien
                ' On remet juste clicbouton2 à False
                ' pour dire que le prochain clic sera de
                ' nouveau le 1° bouton
                clicbouton2 =   False
            Else
                ' Les couleurs ne sont pas identiques

                ' Création d'une attente de 2 secondes
                hbwait(2000)

                ' On affiche de nouveau les 2 boutons gris
                tableau1(ibouton1, jbouton1).Visible  = True
                tableau1(ibouton2, jbouton2).Visible =   True

                ' On remet juste clicbouton2 à False
                ' pour dire que le prochain clic sera de
                ' nouveau le 1° bouton
                clicbouton2  = False
            End If
        End If

        ' Avec les 2 boucles For, on réactive
        ' le clic sur un autre bouton
        For nb1 As Integer =   0  To 5
            For nb2 As Integer  = 0 To 5
                tableau1(nb1, nb2).Enabled =   True
            Next
        Next

        ' Avec ces 2 boucles, on teste si toutes les couleurs
        ' ont été trouvées si ce n'est pas le cas, la variable
        ' colorsfound passe à False
        For nb1 As Integer  = 0 To 3
            For nb2 As Integer =   0  To 3
                If tableau1(nb1, nb2).Visible  = True Then
                    colorsfound =   False
                End If
            Next
        Next

        ' Si colorsfound est à True, celà
        ' veut dire que plus aucun bouton gris
        ' n'est visible (on a trouvé toutes les
        ' couleurs)
        If colorsfound  = True Then
            MsgBox( "Félicitation, vous avez gagné !!!" )
        End If

    End Sub

    Friend Sub hbwait(ByVal ms_to_wait As Long)
        Dim endwait As Double
        endwait  =  Environment.TickCount + ms_to_wait
         While  Environment.TickCount < endwait
            System.Threading.Thread.Sleep(1)
            Application.DoEvents()
        End While
    End Sub

End Class



<hr size ="2" width="100%" />
++

Fauve

<hr size="2" width="100%" />
Pensez : Réponse Acceptée 
0
Rejoignez-nous