Newer
Older
import puissance4.model.Grille;
import puissance4.model.Joueur;
public Puissance4Controleur() {
this.grilleCourant = new Grille();
}
public Grille getGrilleCourant() {
return this.grilleCourant;
}
private void setGrilleCourant(Grille grilleGame) {
this.grilleCourant = grilleGame;
}
public Joueur getJoueurCourant() {
}
private void setJoueurCourant(Joueur JoueurCourant) {
}
private ArrayList<Joueur> getListeJoueur() {
public void joueurSuivant() {
if(this.joueurCourant==this.listeJoueur.get(0)){
this.setJoueurCourant(this.listeJoueur.get(1));
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())
{
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
210
211
212
213
214
215
}
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;