IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Problème avec ReplayListener


Sujet :

Android

  1. #1
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2016
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Janvier 2016
    Messages : 10
    Points : 11
    Points
    11
    Par défaut Problème avec ReplayListener
    Bonjour,

    Je suis vraiment embarrassé par mon code depuis 2 jours. En effet, je décide de développer un petit jeu appelé JEU DU MORPION mais j'ai un problème avec replayListener. Pour mieux comprendre, je vous laisse mon code source afin d'avoir des aides a ce sujet. Merci par avance de votre aide.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    package human.jeux.ci.morpion;
     
    import android.graphics.Color;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
     
     
    import com.google.android.gms.appindexing.Action;
    import com.google.android.gms.appindexing.AppIndex;
    import com.google.android.gms.common.api.GoogleApiClient;
     
     
    public class MainActivity extends AppCompatActivity {
     
        Button pion0, pion1, pion2, pion3, pion4, pion5, pion6, pion7, pion8;
        Button[] pions = new Button[9];
        TextView gameStatus;
        Button replayStatus;
        ImageView morpionImageView;
        boolean gameIsOver = false;
        boolean isValid;
        int currenTurn = 0;
        String[] joueurs = {"X", "O"};
     
     
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
     
            initialize();
            bindEventListenerOnButtons();
     
     
     
        private void initialize() {
                pion0 = (Button) findViewById(R.id.pion0Bouton);
                pion1 = (Button) findViewById(R.id.pion1Bouton);
                pion2 = (Button) findViewById(R.id.pion2Bouton);
                pion3 = (Button) findViewById(R.id.pion3Bouton);
                pion4 = (Button) findViewById(R.id.pion4Bouton);
                pion5 = (Button) findViewById(R.id.pion5Bouton);
                pion6 = (Button) findViewById(R.id.pion6Bouton);
                pion7 = (Button) findViewById(R.id.pion7Bouton);
                pion8 = (Button) findViewById(R.id.pion8Bouton);
     
                gameStatus = (TextView) findViewById(R.id.gameStatusTextView);
                replayStatus = (Button) findViewById(R.id.gameReplayButton);
                morpionImageView = (ImageView) findViewById(R.id.imageView);
            }
     
     
     
        private void bindEventListenerOnButtons() {
            for (Button pion : pions) {
                pion.setOnClickListener(pionListener);
            }
     
            replayStatus.setOnClickListener(replayListener);
        }
     
        private View.OnClickListener pionListener = new View.OnClickListener() {
     
            public void onClick(View v) {
                //trouver le bouton sur lequel on a cliqué
                Button button = (Button) findViewById(v.getId());
                //est ce que le jeu est terminé
                if (gameIsOver) {
                    //alors on ne fait rien
                    return;
                }
     
                //verifier si l'emplacement est valide
                if (!isValid(button)) {
                    //SHAKE animation
                    gameStatus.setText("Deplacement Invalide");
                }
                //afficher emplacement invalide
                else {
                    setSymbol(button, joueurs[currenTurn]);
                    gameIsOver = winnerExists();
                    if (gameIsOver) {
                        gameStatus.setText("le joueur" + joueurs[currenTurn] + "à gagné");
                    }
                    if (boardIsFull()) {
                        gameStatus.setText("MATCH NUL");
                        gameIsOver = true;
                        return;
                    }
                    currenTurn = currenTurn ^ 1;
                    gameStatus.setText("joueur" + joueurs[currenTurn] + "c'est à votre Tour");
     
                }
            }
     
     
            protected boolean isValid(Button button) {
                return button.getText().toString().length() == 0;
            }
     
            protected boolean boardIsFull() {
                for (Button pion : pions) {
                    if (isValid(pion)) {
                        return false;
                    }
                }
                return true;
            }
     
            protected void setSymbol(Button button, String symbol) {
                button.setText(symbol);
            }
     
            protected boolean winnerExists() {
                if (pions[0].getText().toString() == joueurs[currenTurn]
                        && pions[1].getText().toString() == joueurs[currenTurn]
                        && pions[2].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(0, 1, 2);
                    return true;
                }
     
                if (pions[3].getText().toString() == joueurs[currenTurn]
                        && pions[4].getText().toString() == joueurs[currenTurn]
                        && pions[5].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(3, 4, 5);
                    return true;
     
                }
                if (pions[6].getText().toString() == joueurs[currenTurn]
                        && pions[7].getText().toString() == joueurs[currenTurn]
                        && pions[8].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(6, 7, 8);
                    return true;
     
                }
                if (pions[0].getText().toString() == joueurs[currenTurn]
                        && pions[3].getText().toString() == joueurs[currenTurn]
                        && pions[6].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(0, 3, 6);
                    return true;
                }
                if (pions[1].getText().toString() == joueurs[currenTurn]
                        && pions[4].getText().toString() == joueurs[currenTurn]
                        && pions[7].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(1, 4, 7);
                    return true;
                }
                if (pions[2].getText().toString() == joueurs[currenTurn]
                        && pions[5].getText().toString() == joueurs[currenTurn]
                        && pions[8].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(2, 5, 8);
                    return true;
                }
                if (pions[0].getText().toString() == joueurs[currenTurn]
                        && pions[4].getText().toString() == joueurs[currenTurn]
                        && pions[8].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(0, 4, 8);
                    return true;
                }
                if (pions[2].getText().toString() == joueurs[currenTurn]
                        && pions[4].getText().toString() == joueurs[currenTurn]
                        && pions[6].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(2, 4, 6);
                    return true;
                }
                if (pions[0].getText().toString() == joueurs[currenTurn]
                        && pions[4].getText().toString() == joueurs[currenTurn]
                        && pions[8].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(0, 4, 8);
                    return true;
                }
                return false;
            }
     
            private void changeColorOfButtons(int i, int i1, int i2) {
                pions[i].setTextColor(Color.argb(255, 8, 13, 246));
                pions[i1].setTextColor(Color.argb(255, 42, 188, 18));
                pions[i2].setTextColor(Color.argb(255, 230, 79, 97));
            }
     
            private View.OnClickListener replayListener = new View.OnClickListener() {
     
                public void onClick(View v) {
                    for (Button pion : pions) {
                        pion.setText("");
                        pion.setTextColor(Color.BLACK);
                    }
     
                    gameStatus.setText(R.string.game_status);
                }
            }

  2. #2
    Membre confirmé
    Homme Profil pro
    Développeur Java
    Inscrit en
    Février 2013
    Messages
    191
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 29
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Février 2013
    Messages : 191
    Points : 472
    Points
    472
    Par défaut
    Il manque des '}'

  3. #3
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2016
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Janvier 2016
    Messages : 10
    Points : 11
    Points
    11
    Par défaut Problème avec un listener
    Bonjour cher tous,
    je veux que vous m'aidez à trouvez le probleme dans mon code source. J'ai un un probleme avec replayListener.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    package human.jeux.ci.morpion;
     
    import android.graphics.Color;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
     
     
     
    import com.google.android.gms.appindexing.Action;
    import com.google.android.gms.appindexing.AppIndex;
    import com.google.android.gms.common.api.GoogleApiClient;
     
     
    public class MainActivity extends AppCompatActivity {
     
        Button pion0, pion1, pion2, pion3, pion4, pion5, pion6, pion7, pion8;
        Button[] pions = new Button[9];
        TextView gameStatus;
        Button replayStatus;
        ImageView morpionImageView;
        boolean gameIsOver = false;
        boolean isValid;
        int currenTurn = 0;
        String[] joueurs = {"X", "O"};
     
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
     
            initialize();
            bindEventListenerOnButtons();
     
        }
     
        private void initialize() {
            pion0 = (Button) findViewById(R.id.pion0Bouton);
            pion1 = (Button) findViewById(R.id.pion1Bouton);
            pion2 = (Button) findViewById(R.id.pion2Bouton);
            pion3 = (Button) findViewById(R.id.pion3Bouton);
            pion4 = (Button) findViewById(R.id.pion4Bouton);
            pion5 = (Button) findViewById(R.id.pion5Bouton);
            pion6 = (Button) findViewById(R.id.pion6Bouton);
            pion7 = (Button) findViewById(R.id.pion7Bouton);
            pion8 = (Button) findViewById(R.id.pion8Bouton);
     
            gameStatus = (TextView) findViewById(R.id.gameStatusTextView);
            replayStatus = (Button) findViewById(R.id.gameReplayButton);
            morpionImageView = (ImageView) findViewById(R.id.imageView);
        }
     
     
     
        private void bindEventListenerOnButtons() {
            for (Button pion : pions) {
                pion.setOnClickListener(pionListener);
            }
     
            replayStatus.setOnClickListener(replayListener);
     
        }
     
     
     
        private View.OnClickListener pionListener = new View.OnClickListener() {
     
            public void onClick(View v) {
                //trouver le bouton sur lequel on a cliqué
                Button button = (Button) findViewById(v.getId());
                //est ce que le jeu est terminé
                if (gameIsOver) {
                    //alors on ne fait rien
                    return;
                }
     
                //verifier si l'emplacement est valide
                if (!isValid(button)) {
                    //SHAKE animation
                    gameStatus.setText("Deplacement Invalide");
                }
                //afficher emplacement invalide
                else {
                    setSymbol(button, joueurs[currenTurn]);
                    gameIsOver = winnerExists();
                    if (gameIsOver) {
                        gameStatus.setText("le joueur" + joueurs[currenTurn] + "à gagné");
                    }
                    if (boardIsFull()) {
                        gameStatus.setText("MATCH NUL");
                        gameIsOver = true;
                        return;
                    }
                    currenTurn = currenTurn ^ 1;
                    gameStatus.setText("joueur" + joueurs[currenTurn] + "c'est à votre Tour");
     
                }
            }
     
     
            protected boolean isValid(Button button) {
                return button.getText().toString().length() == 0;
            }
     
            protected boolean boardIsFull() {
                for (Button pion : pions) {
                    if (isValid(pion)) {
                        return false;
                    }
                }
                return true;
            }
     
            protected void setSymbol(Button button, String symbol) {
                button.setText(symbol);
            }
     
            protected boolean winnerExists() {
                if (pions[0].getText().toString() == joueurs[currenTurn]
                        && pions[1].getText().toString() == joueurs[currenTurn]
                        && pions[2].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(0, 1, 2);
                    return true;
                }
     
                if (pions[3].getText().toString() == joueurs[currenTurn]
                        && pions[4].getText().toString() == joueurs[currenTurn]
                        && pions[5].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(3, 4, 5);
                    return true;
     
                }
                if (pions[6].getText().toString() == joueurs[currenTurn]
                        && pions[7].getText().toString() == joueurs[currenTurn]
                        && pions[8].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(6, 7, 8);
                    return true;
     
                }
                if (pions[0].getText().toString() == joueurs[currenTurn]
                        && pions[3].getText().toString() == joueurs[currenTurn]
                        && pions[6].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(0, 3, 6);
                    return true;
                }
                if (pions[1].getText().toString() == joueurs[currenTurn]
                        && pions[4].getText().toString() == joueurs[currenTurn]
                        && pions[7].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(1, 4, 7);
                    return true;
                }
                if (pions[2].getText().toString() == joueurs[currenTurn]
                        && pions[5].getText().toString() == joueurs[currenTurn]
                        && pions[8].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(2, 5, 8);
                    return true;
                }
                if (pions[0].getText().toString() == joueurs[currenTurn]
                        && pions[4].getText().toString() == joueurs[currenTurn]
                        && pions[8].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(0, 4, 8);
                    return true;
                }
                if (pions[2].getText().toString() == joueurs[currenTurn]
                        && pions[4].getText().toString() == joueurs[currenTurn]
                        && pions[6].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(2, 4, 6);
                    return true;
                }
                if (pions[0].getText().toString() == joueurs[currenTurn]
                        && pions[4].getText().toString() == joueurs[currenTurn]
                        && pions[8].getText().toString() == joueurs[currenTurn]) {
                    changeColorOfButtons(0, 4, 8);
                    return true;
                }
                return false;
            }
     
            private void changeColorOfButtons(int i, int i1, int i2) {
                pions[i].setTextColor(Color.argb(255, 8, 13, 246));
                pions[i1].setTextColor(Color.argb(255, 42, 188, 18));
                pions[i2].setTextColor(Color.argb(255, 230, 79, 97));
            }
     
            private View.OnClickListener replayListener = new View.OnClickListener() {
     
                public void onClick(View v) {
                    for (Button pion : pions) {
                        pion.setText("");
                        pion.setTextColor(Color.BLACK);
                    }
                    gameStatus.setText(R.string.game_status);
     
                }
            };
     
            };
    }

  4. #4
    Membre confirmé
    Avatar de Jacques Beauregard
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mai 2015
    Messages
    231
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2015
    Messages : 231
    Points : 595
    Points
    595
    Par défaut
    Hello,
    Veuillez corriger mon code SVP. MERCI.
    Ce genre de phrase n'est pas compatible avec ce forum.
    Merci de développer ( developpez.net) les erreurs que tu rencontres afin que l'on puisse t'aider.
    Cordialement.
    Il ne faut jamais prendre les gens pour des cons, mais il ne faut pas oublier qu'ils le sont...

    Le guide du débutant pour apprendre à programmer en Java - N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  5. #5
    Membre confirmé

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2014
    Messages
    262
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : Burkina Faso

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Finance

    Informations forums :
    Inscription : Août 2014
    Messages : 262
    Points : 634
    Points
    634
    Par défaut
    Le soucis dans ton code est que ton objet replayListener a été déclaré comme étant un attribut de l'objet pionListener. C'est çà que tu as fais :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    private View.OnClickListener pionListener = new View.OnClickListener() {
    //...
    //...
    private View.OnClickListener replayListener = new View.OnClickListener() {
    };
    //...
    };
    Du coup il n'est pas visible par la classe principale. Regardes bien tes accolades fermantes et place l'objet dans la classe principale.

    __salut !
    Aujourd'hui apprenant, demain appreneur.
    N'accuse pas le puits d'être trop profond,
    c'est peut-être ta corde qui est trop courte

  6. #6
    Membre confirmé
    Homme Profil pro
    Développeur Java
    Inscrit en
    Février 2013
    Messages
    191
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 29
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Février 2013
    Messages : 191
    Points : 472
    Points
    472
    Par défaut
    Y a-t'il besoin de créer 2 topics ?
    http://www.developpez.net/forums/d15...eplaylistener/

Discussions similaires

  1. [API][Cup] problem niveau compilation
    Par KoLziG dans le forum Général Java
    Réponses: 1
    Dernier message: 02/01/2005, 19h30
  2. [JSP & TAGLIB] Probleme de compilation
    Par GesMo dans le forum Servlets/JSP
    Réponses: 7
    Dernier message: 17/05/2004, 15h03
  3. Probleme de Compilation de la STL
    Par stoluup dans le forum MFC
    Réponses: 3
    Dernier message: 05/05/2004, 17h25
  4. [TP]Probleme de compilation sous TP7
    Par yffick dans le forum Turbo Pascal
    Réponses: 7
    Dernier message: 18/12/2003, 20h32
  5. Problemes de compilation avec g++ sous linux
    Par Selimmel dans le forum Autres éditeurs
    Réponses: 3
    Dernier message: 19/09/2003, 13h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo