An error occurred while loading the file. Please try again.
-
AMOCA OKKES authored65f1e78b
Puissance4Controleur.java 6.43 KiB
package puissance4.controleur;
import puissance4.model.Grille;
import puissance4.model.Joueur;
import java.awt.*;
import java.util.ArrayList;
public class Puissance4Controleur
{
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));
}
}
public Joueur getWinner() {
return this.victoire;
}
public void setWinner(Joueur joueurVictoire) {
this.victoire = joueurVictoire;
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())
{
setWinner(getJoueurCourant());
}
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;
}
}