Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.89 KB | None | 0 0
  1. package p32_tp4_mastermind;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.FlowLayout;
  6. import java.awt.GridLayout;
  7.  
  8. import javax.swing.JButton;
  9. import javax.swing.JLabel;
  10. import javax.swing.JPanel;
  11. import javax.swing.JTextField;
  12. import javax.swing.SwingConstants;
  13.  
  14. public class VueMastermind extends JPanel {
  15.  
  16.     private javax.swing.JTextField[] bPIHM; //Tableau des champs textes de couleurs bien placées
  17.     private javax.swing.JTextField[] combinaisonOrdiIHM; //Tableaux des champs contenant les couleurs à trouver
  18.     private javax.swing.JButton[][] combinaisonsJoueurIHM; //Tableaux des boutons utilisables par l'utilisateur
  19.     private javax.swing.JTextField[] mPIHM; //Tableau des champs textes de couleurs mal placées
  20.     private final int nbCouleurs = 6; //Nombre de couleurs différentes
  21.     static int NBMAX_COMBINAISONS = 10; //Nombre maximum d'essais
  22.     private javax.swing.JButton[] paletteIHM; //Palette de couleurs
  23.     private static long serialVersionUID;
  24.     private int taille = 4; //Nombre de couleurs à trouver
  25.     private static Color[] couleurs = {Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW, Color.CYAN, Color.MAGENTA};
  26.    
  27.     public VueMastermind()
  28.     {
  29.         //Instanciations
  30.         this.bPIHM = new JTextField[NBMAX_COMBINAISONS];
  31.         this.mPIHM = new JTextField[NBMAX_COMBINAISONS];
  32.         this.combinaisonOrdiIHM = new JTextField[taille];
  33.         this.combinaisonsJoueurIHM = new JButton[NBMAX_COMBINAISONS][taille];
  34.         this.paletteIHM = new JButton[nbCouleurs];
  35.        
  36.        
  37.         this.setLayout(new BorderLayout());
  38.        
  39.         //Partie haute de l'interface
  40.         {
  41.             JPanel haut = new JPanel();
  42.             haut.setLayout(new FlowLayout());
  43.             JLabel label_couleurs = new JLabel("Couleurs: ");
  44.             haut.add(label_couleurs);
  45.             //Boutons de la partie haute de l'interface
  46.             {
  47.                 JPanel hautBoutons = new JPanel();
  48.                 hautBoutons.setLayout(new GridLayout(1, this.nbCouleurs));
  49.                 for(int i=0; i<this.nbCouleurs; i++)
  50.                 {
  51.                     JButton tmp = new JButton();
  52.                     tmp.setBackground(this.couleurs[i]);
  53.                     this.paletteIHM[i] = tmp;
  54.                     hautBoutons.add(tmp);
  55.                 }
  56.                 haut.add(hautBoutons);
  57.             }
  58.            
  59.             this.add(haut, BorderLayout.NORTH);
  60.         }
  61.        
  62.         //Milieu de l'interface
  63.         {
  64.             JPanel milieu = new JPanel();
  65.             milieu.setLayout(new GridLayout(NBMAX_COMBINAISONS, 2));
  66.            
  67.             //Boucle de lignes
  68.             for(int i = 0; i<NBMAX_COMBINAISONS; i++)
  69.             {
  70.                 //Boutons
  71.                 {
  72.                     JPanel btns = new JPanel();
  73.                     btns.setLayout(new GridLayout(1, this.taille));
  74.                    
  75.                     for(int j=0; j<this.taille; j++)
  76.                     {
  77.                         JButton tmp = new JButton();
  78.                         if(i != 0)
  79.                             tmp.setEnabled(false);
  80.                         this.combinaisonsJoueurIHM[i][j] = tmp;
  81.                         btns.add(tmp);
  82.                     }
  83.                    
  84.                     milieu.add(btns);
  85.                 }
  86.                 //Compteurs
  87.                 {
  88.                     JPanel cmpts = new JPanel();
  89.                     cmpts.setLayout(new GridLayout(2, 2));
  90.                    
  91.                     //Labels
  92.                     {
  93.                         JLabel tmp;
  94.                         tmp = new JLabel("BP");
  95.                         tmp.setHorizontalAlignment(SwingConstants.CENTER);
  96.                         cmpts.add(tmp);
  97.                         tmp = new JLabel("MP");
  98.                         tmp.setHorizontalAlignment(SwingConstants.CENTER);
  99.                         cmpts.add(tmp);
  100.                     }
  101.                     //Champs de texte
  102.                     {
  103.                         for(int j=0; j<2; j++)
  104.                         {
  105.                             JTextField tmp = new JTextField();
  106.                             tmp.setEditable(false);
  107.                             if(i==0)
  108.                                 this.bPIHM[i] = tmp;
  109.                             else
  110.                                 this.mPIHM[i] = tmp;
  111.                             cmpts.add(tmp);
  112.                         }
  113.                     }
  114.                     milieu.add(cmpts);
  115.                 }
  116.             }
  117.             this.add(milieu, BorderLayout.CENTER);
  118.         }
  119.        
  120.         //Partie basse de l'interface
  121.         {
  122.             JPanel bas = new JPanel();
  123.             bas.setLayout(new GridLayout(1, 2));
  124.             //Partie boutons
  125.             {
  126.                 JPanel lbls = new JPanel();
  127.                 lbls.setLayout(new GridLayout(1, this.taille));
  128.                
  129.                 for(int i=0; i<this.taille; i++)
  130.                 {
  131.                     JTextField tmp = new JTextField();
  132.                     tmp.setEditable(false);
  133.                     this.combinaisonOrdiIHM[i] = tmp;
  134.                     lbls.add(tmp);
  135.                 }
  136.                
  137.                 bas.add(lbls);
  138.             }
  139.             //Partie Valider
  140.             {
  141.                 bas.add(new JButton("Valider"));
  142.             }
  143.             this.add(bas, BorderLayout.SOUTH);
  144.         }
  145.     }
  146.    
  147.     void activerCombinaison(int ligne)
  148.     {
  149.         for(int i=0; i<this.taille; i++)
  150.         {
  151.             this.combinaisonsJoueurIHM[ligne][i].setEnabled(true);
  152.         }
  153.     }
  154.    
  155.     void desactiverCombinaison(int ligne)
  156.     {
  157.         for(int i=0; i<this.taille; i++)
  158.         {
  159.             this.combinaisonsJoueurIHM[ligne][i].setEnabled(false);
  160.         }
  161.     }
  162.    
  163.     void afficherBP(int ligne, int nbBP)
  164.     {
  165.         this.bPIHM[ligne].setText("" + nbBP);
  166.     }
  167.    
  168.     void afficherMP(int ligne, int nbMP)
  169.     {
  170.         this.mPIHM[ligne].setText("" + nbMP);
  171.     }
  172.    
  173.     void afficherCombinaisonOrdinateur(int[] tableauCouleurs)
  174.     {
  175.         for(int i = 0; i<this.taille; i++)
  176.         {
  177.             this.combinaisonOrdiIHM[i].setBackground(chiffreEnCouleur(tableauCouleurs[i]));
  178.         }
  179.     }
  180.    
  181.     Color chiffreEnCouleur(int chiffre)
  182.     {
  183.         return this.couleurs[chiffre];
  184.     }
  185.    
  186.     boolean appartientCombinaison(JButton b, int ligne)
  187.     {
  188.         for(int i=0; i<this.taille; i++)
  189.         {
  190.             if(this.combinaisonsJoueurIHM[ligne][i]==b) //Comparaison de références
  191.                 return true;
  192.         }
  193.         return false;
  194.     }
  195.    
  196.      boolean appartientPalette(JButton b)
  197.      {
  198.          for(int i=0; i<this.nbCouleurs; i++)
  199.          {
  200.              if(this.paletteIHM[i]==b) //Comparaison de références
  201.                  return true;
  202.          }
  203.          return false;
  204.      }
  205.      
  206.      boolean combinaisonComplete(int ligne)
  207.      {
  208.          for(int i=0; i<this.taille; i++)
  209.          {
  210.              if(this.combinaisonsJoueurIHM[ligne][i].getBackground() == null)
  211.                  return false;
  212.          }
  213.          return true;
  214.      }
  215.      
  216.      static int couleurEnChiffre(java.awt.Color c)
  217.      {
  218.          for(int i=0; i< couleurs.length ; i++)
  219.          {
  220.              if(couleurs[i] == c)
  221.                  return i;
  222.          }
  223.          return -1;
  224.      }
  225.      
  226.      int[] combinaisonEnEntiers(int ligne)
  227.      {
  228.          int[] tmp = new int[this.taille];
  229.          for(int i=0; i<this.taille; i++)
  230.          {
  231.              tmp[i] = couleurEnChiffre(this.combinaisonsJoueurIHM[ligne][i].getBackground());
  232.          }
  233.          return tmp;
  234.      }
  235.      
  236.      int getNbCouleurs()
  237.      {
  238.          return couleurs.length;
  239.      }
  240.      
  241.      int getTaille()
  242.      {
  243.          return this.taille;
  244.      }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement