Newer
Older
import puissance4.model.Grille;
import puissance4.model.Joueur;
import puissance4.model.Pion;
import puissance4.vue.VueJoueur;
import javax.swing.*;
import java.awt.*;
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
private Grille grilleCourant;
private Joueur JoueurCourant;
private Joueur victoire;
private ArrayList<Joueur> ListeJoueur;
public Puissance4Controleur() {
this.grilleCourant = new Grille();
this.JoueurCourant = null;
this.victoire = null;
this.ListeJoueur = new ArrayList<Joueur>();
}
public void startGame() {
this.setJoueurCourant(this.ListeJoueur.get(0));
}
public Grille getGrilleCourant() {
return this.grilleCourant;
}
private void setGrilleCourant(Grille grilleGame) {
this.grilleCourant = grilleGame;
}
public Joueur getJoueurCourant() {
return this.JoueurCourant;
}
private void setJoueurCourant(Joueur JoueurCourant) {
this.JoueurCourant = JoueurCourant;
}
private ArrayList<Joueur> getListeJoueur() {
return ListeJoueur;
}
public void addJoueur(Joueur joueur) {
this.ListeJoueur.add(joueur);
}
public void JoueurSuivant() {
if(this.JoueurCourant==this.ListeJoueur.get(0)){
this.setJoueurCourant(this.ListeJoueur.get(1));
}
else{
this.setJoueurCourant(this.ListeJoueur.get(0));
}
}
if(joueurVictoire!=null){
System.out.println(joueurVictoire.getNom());
}
public boolean peutJouer(int colonne)
boolean res=true;
//Test si y'a de la place dans la colonne
if(this.getGrilleCourant().getCases()[colonne][this.getGrilleCourant().getNblignes() - 1].estVide()){
for(int i = 0; i < this.getGrilleCourant().getCases()[colonne].length; i++)
{
if(this.getGrilleCourant().getCases()[colonne][i].estVide())
{
this.getGrilleCourant().getCases()[colonne][i].remplir(this.getJoueurCourant().getColor());
break;
}
}
}
else{ //Colonne rempli
res=false;
}
//Si joueur gagne on réinitialise la grille
if(testWinner())
{
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
210
211
212
213
214
215
216
217
218
219
220
221
this.getGrilleCourant().viderGrille();
}
JoueurSuivant();
return res;
}
public boolean testWinner(){
boolean r = true; // red
boolean y = false; // yellow
boolean res=false;
Boolean[][] tab = new Boolean[this.getGrilleCourant().getNbColonnes()][this.getGrilleCourant().getNblignes()];
for(int i = 0; i < this.getGrilleCourant().getNbColonnes(); i++)
{
for(int j = 0; j < this.getGrilleCourant().getNblignes(); j++)
{
if(!this.getGrilleCourant().getCases()[i][j].estVide())
{
if (this.getGrilleCourant().getCases()[i][j].getPion().getCouleur()==Color.RED){
tab[i][j] = r;
}
if (this.getGrilleCourant().getCases()[i][j].getPion().getCouleur()==Color.YELLOW){
tab[i][j] = y;
}
}
else{
tab[i][j] = null;
}
//System.out.print(tab[i][j]+" ");
}
//System.out.println();
}
res= detect(tab);
//System.out.println(res);
return res;
}
/**
* Détecte 4 'true' ou 4 'false' alignés dans une des 4 directions |, _, /, \.
*
* Si un élément a la valeur null, tout ce qui est au-dessus est sensé être null.
* @param board : un tableau de valeurs de type Boolean ou null
* @return les coordonnées des deux extrémités du premier alignement trouvé.
*/
public boolean detect( Boolean[][] board ) {
int l = 4;
int ll = l - 1;
int width = board.length;
int height = board[0].length;
// South - North
int h = height - ll;
for ( int col = 0; col < width; col++ )
for ( int row = 0; row < l; row++ ) {
Boolean b = board[col][row];
if ( b == null)
break;
boolean success = true;
for ( int i = 1; i < l && success; i++ )
success &= ( board[col][row + i] == b );
if ( success )
return true;
//return new int[] { col, row, col, row + ll };
}
// West - East
int w = width - ll;
for ( int col = 0; col < w; col++ )
for ( int row = 0; row < height; row++ ) {
Boolean b = board[col][row];
if ( b != null ) {
boolean success = true;
for ( int i = 1; i < l && success; i++ )
success &= ( board[col + i][row] == b );
if ( success )
return true;
//return new int[] { col, row, col + ll, row };
}
}
// SouthWest - NorthEast
h = height - ll;
w = width - ll;
for ( int col = 0; col < w; col++ )
for ( int row = 0; row < h; row++ ) {
Boolean b = board[col][row];
if ( b != null ) {
boolean success = true;
for ( int i = 1; i < l && success; i++ )
success &= ( board[col + i][row + i] == b );
if ( success )
return true;
//return new int[] { col, row, col + ll, row + ll };
}
}
// NorthWest - SouthEast
w = width - ll;
for ( int col = 0; col < w; col++ )
for ( int row = ll; row < height; row++ ) {
Boolean b = board[col][row];
if ( b != null ) {
boolean success = true;
for ( int i = 1; i < l && success; i++ )
success &= (board[col + i][row - i ] == b);
if ( success )
return true;
//return new int[] { col, row, col + ll, row - ll };
}
}
// No connection detected
return false;