Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package p32_tp4_mastermind;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.FlowLayout;
- import java.awt.GridLayout;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.SwingConstants;
- public class VueMastermind extends JPanel {
- private javax.swing.JTextField[] bPIHM; //Tableau des champs textes de couleurs bien placées
- private javax.swing.JTextField[] combinaisonOrdiIHM; //Tableaux des champs contenant les couleurs à trouver
- private javax.swing.JButton[][] combinaisonsJoueurIHM; //Tableaux des boutons utilisables par l'utilisateur
- private javax.swing.JTextField[] mPIHM; //Tableau des champs textes de couleurs mal placées
- private final int nbCouleurs = 6; //Nombre de couleurs différentes
- static int NBMAX_COMBINAISONS = 10; //Nombre maximum d'essais
- private javax.swing.JButton[] paletteIHM; //Palette de couleurs
- private static long serialVersionUID;
- private int taille = 4; //Nombre de couleurs à trouver
- private static Color[] couleurs = {Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW, Color.CYAN, Color.MAGENTA};
- public VueMastermind()
- {
- //Instanciations
- this.bPIHM = new JTextField[NBMAX_COMBINAISONS];
- this.mPIHM = new JTextField[NBMAX_COMBINAISONS];
- this.combinaisonOrdiIHM = new JTextField[taille];
- this.combinaisonsJoueurIHM = new JButton[NBMAX_COMBINAISONS][taille];
- this.paletteIHM = new JButton[nbCouleurs];
- this.setLayout(new BorderLayout());
- //Partie haute de l'interface
- {
- JPanel haut = new JPanel();
- haut.setLayout(new FlowLayout());
- JLabel label_couleurs = new JLabel("Couleurs: ");
- haut.add(label_couleurs);
- //Boutons de la partie haute de l'interface
- {
- JPanel hautBoutons = new JPanel();
- hautBoutons.setLayout(new GridLayout(1, this.nbCouleurs));
- for(int i=0; i<this.nbCouleurs; i++)
- {
- JButton tmp = new JButton();
- tmp.setBackground(this.couleurs[i]);
- this.paletteIHM[i] = tmp;
- hautBoutons.add(tmp);
- }
- haut.add(hautBoutons);
- }
- this.add(haut, BorderLayout.NORTH);
- }
- //Milieu de l'interface
- {
- JPanel milieu = new JPanel();
- milieu.setLayout(new GridLayout(NBMAX_COMBINAISONS, 2));
- //Boucle de lignes
- for(int i = 0; i<NBMAX_COMBINAISONS; i++)
- {
- //Boutons
- {
- JPanel btns = new JPanel();
- btns.setLayout(new GridLayout(1, this.taille));
- for(int j=0; j<this.taille; j++)
- {
- JButton tmp = new JButton();
- if(i != 0)
- tmp.setEnabled(false);
- this.combinaisonsJoueurIHM[i][j] = tmp;
- btns.add(tmp);
- }
- milieu.add(btns);
- }
- //Compteurs
- {
- JPanel cmpts = new JPanel();
- cmpts.setLayout(new GridLayout(2, 2));
- //Labels
- {
- JLabel tmp;
- tmp = new JLabel("BP");
- tmp.setHorizontalAlignment(SwingConstants.CENTER);
- cmpts.add(tmp);
- tmp = new JLabel("MP");
- tmp.setHorizontalAlignment(SwingConstants.CENTER);
- cmpts.add(tmp);
- }
- //Champs de texte
- {
- for(int j=0; j<2; j++)
- {
- JTextField tmp = new JTextField();
- tmp.setEditable(false);
- if(i==0)
- this.bPIHM[i] = tmp;
- else
- this.mPIHM[i] = tmp;
- cmpts.add(tmp);
- }
- }
- milieu.add(cmpts);
- }
- }
- this.add(milieu, BorderLayout.CENTER);
- }
- //Partie basse de l'interface
- {
- JPanel bas = new JPanel();
- bas.setLayout(new GridLayout(1, 2));
- //Partie boutons
- {
- JPanel lbls = new JPanel();
- lbls.setLayout(new GridLayout(1, this.taille));
- for(int i=0; i<this.taille; i++)
- {
- JTextField tmp = new JTextField();
- tmp.setEditable(false);
- this.combinaisonOrdiIHM[i] = tmp;
- lbls.add(tmp);
- }
- bas.add(lbls);
- }
- //Partie Valider
- {
- bas.add(new JButton("Valider"));
- }
- this.add(bas, BorderLayout.SOUTH);
- }
- }
- void activerCombinaison(int ligne)
- {
- for(int i=0; i<this.taille; i++)
- {
- this.combinaisonsJoueurIHM[ligne][i].setEnabled(true);
- }
- }
- void desactiverCombinaison(int ligne)
- {
- for(int i=0; i<this.taille; i++)
- {
- this.combinaisonsJoueurIHM[ligne][i].setEnabled(false);
- }
- }
- void afficherBP(int ligne, int nbBP)
- {
- this.bPIHM[ligne].setText("" + nbBP);
- }
- void afficherMP(int ligne, int nbMP)
- {
- this.mPIHM[ligne].setText("" + nbMP);
- }
- void afficherCombinaisonOrdinateur(int[] tableauCouleurs)
- {
- for(int i = 0; i<this.taille; i++)
- {
- this.combinaisonOrdiIHM[i].setBackground(chiffreEnCouleur(tableauCouleurs[i]));
- }
- }
- Color chiffreEnCouleur(int chiffre)
- {
- return this.couleurs[chiffre];
- }
- boolean appartientCombinaison(JButton b, int ligne)
- {
- for(int i=0; i<this.taille; i++)
- {
- if(this.combinaisonsJoueurIHM[ligne][i]==b) //Comparaison de références
- return true;
- }
- return false;
- }
- boolean appartientPalette(JButton b)
- {
- for(int i=0; i<this.nbCouleurs; i++)
- {
- if(this.paletteIHM[i]==b) //Comparaison de références
- return true;
- }
- return false;
- }
- boolean combinaisonComplete(int ligne)
- {
- for(int i=0; i<this.taille; i++)
- {
- if(this.combinaisonsJoueurIHM[ligne][i].getBackground() == null)
- return false;
- }
- return true;
- }
- static int couleurEnChiffre(java.awt.Color c)
- {
- for(int i=0; i< couleurs.length ; i++)
- {
- if(couleurs[i] == c)
- return i;
- }
- return -1;
- }
- int[] combinaisonEnEntiers(int ligne)
- {
- int[] tmp = new int[this.taille];
- for(int i=0; i<this.taille; i++)
- {
- tmp[i] = couleurEnChiffre(this.combinaisonsJoueurIHM[ligne][i].getBackground());
- }
- return tmp;
- }
- int getNbCouleurs()
- {
- return couleurs.length;
- }
- int getTaille()
- {
- return this.taille;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement