Newer
Older
package puissance4.vue;
import puissance4.controleur.Puissance4Controleur;
import javax.swing.*;
import java.awt.*;
public class VueJoueur extends JFrame {
private Puissance4Controleur game;
private JLabel jlJoueur;
private JLabel jlGagnant;
public VueJoueur(Puissance4Controleur game){
this.game=game;
//On créer la vue
this.setSize( 1000, 800);
JPanel vue= new JPanel(new BorderLayout());
//On prépare la partie droite de la fenêtre avec un bouton quitter + un texte annoncant une éventuelle victoire
JPanel info = new JPanel(new GridLayout(1,3));
jlGagnant = new JLabel("Aucun gagnant");
jlJoueur = new JLabel("Tour du joueur : "+this.game.getJoueurCourant().getNom());
//JLabel joueur = new JLabel("C'est au tour de " + this.game.getJoueurCourant().getNom() + " de jouer.");
JButton recommancer = new JButton("Recommancer");
recommancer.setCursor(Cursor.getPredefinedCursor(12));
//On rajoute les items au jpanel
info.add(jlGagnant);
info.add(jlJoueur);
info.add(recommancer);
info.setBackground(Color.BLACK);
jlGagnant.setForeground(Color.WHITE);
jlJoueur.setForeground(this.game.getJoueurCourant().getColor().getCouleur());
//Evenement du bouton recommancer
recommancer.addActionListener(e -> {
this.game.getGrilleCourant().viderGrille();
this.majGrille();
});
//Récupération ligne & colonne
int nbLigne = this.game.getGrilleCourant().getNblignes();
int nbColonne = this.game.getGrilleCourant().getNbColonnes();
//New grille
JPanel grille = new JPanel(new GridLayout(nbLigne+1, nbColonne));
//On créé chaque case
for(int colonne = 0; colonne < nbColonne; colonne++)
{
for(int ligne = 0; ligne < nbLigne; ligne++)
{
//Création ligne & colonne
JLabel cases = new JLabel();
cases.setBorder(BorderFactory.createLineBorder(Color.BLACK));
//Ajout de la case à la grille
grille.add(cases);
}
}
//Bouttons pour jouer
for(int i = 0; i < nbColonne; i++)
{
int col = i;
JButton buttonPLay = new JButton("Jouer colonne :"+(col+1));
buttonPLay.setCursor(Cursor.getPredefinedCursor(12));
}
//Ajout dans la grille
vue.add(grille, BorderLayout.CENTER);
vue.add(info, BorderLayout.NORTH);
//Affichage de la vue
this.setContentPane(vue);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
if(this.game.peutJouer(colonne)) {
if (this.game.getWinner()!=null){
this.majGrille();
String colorWinner="Jaune";
if (this.game.getWinner().getColor().getCouleur()==Color.RED){
colorWinner="Rouge";
}
this.jlGagnant.setText(this.game.getWinner().getNom()+" A GAGNÉ LA PARTIE !");
this.jlGagnant.setForeground(this.game.getJoueurCourant().getColor().getCouleur());
this.game.getWinner().getNom()+" ("+colorWinner+") A GAGNÉ LA PARTIE !");
this.game.getGrilleCourant().viderGrille();
this.game.joueurSuivant();
}
else {
System.out.println("Colonne rempli !");
}
//Test si la grille est pleine
if(!this.game.getGrilleCourant().getCases()[0][this.game.getGrilleCourant().getNblignes()-1].estVide()){
if(!this.game.getGrilleCourant().getCases()[1][this.game.getGrilleCourant().getNblignes()-1].estVide()){
if(!this.game.getGrilleCourant().getCases()[2][this.game.getGrilleCourant().getNblignes()-1].estVide()){
if(!this.game.getGrilleCourant().getCases()[3][this.game.getGrilleCourant().getNblignes()-1].estVide()){
if(!this.game.getGrilleCourant().getCases()[4][this.game.getGrilleCourant().getNblignes()-1].estVide()){
if(!this.game.getGrilleCourant().getCases()[5][this.game.getGrilleCourant().getNblignes()-1].estVide()){
if(!this.game.getGrilleCourant().getCases()[6][this.game.getGrilleCourant().getNblignes()-1].estVide()){
this.game.joueurSuivant();
this.game.getGrilleCourant().viderGrille();
JOptionPane.showMessageDialog(this, "EGALITÉ ! Nouvelle partie.");
this.majGrille();
}
}
}
}
}
}
}
this.jlJoueur.setText("Tour du joueur : "+this.game.getJoueurCourant().getNom());
this.jlJoueur.setForeground(this.game.getJoueurCourant().getColor().getCouleur());
{
int nbLigne = this.game.getGrilleCourant().getNblignes();
int nbColonne = this.game.getGrilleCourant().getNbColonnes();
JPanel grille = (JPanel)((BorderLayout)this.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER);
int but = 0;
for (int i = nbLigne-1; i >= 0; i--)
{
for(int j = 0; j < nbColonne; j++)
{
if(this.game.getGrilleCourant().getCases()[j][i].estVide()) //Si la case est vide
{
grille.getComponent(but).setBackground(Color.WHITE);
((JLabel) grille.getComponent(but)).setOpaque(true);
}
else{
grille.getComponent(but).setBackground(this.game.getGrilleCourant().getCases()[j][i].getPion().getCouleur());
((JLabel) grille.getComponent(but)).setOpaque(true);
}
but++;
}
}
}
}