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);
            }
        }