Advertisement
Guest User

Controleur

a guest
Feb 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. package p32_tp4_mastermind;
  2.  
  3. import java.awt.Color;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.JButton;
  8.  
  9. public class ControleurMastermind implements ActionListener
  10. {
  11.     private enum Etat
  12.     {
  13.         DEBUT_COMBINAISON,
  14.         CHOIX_COULEUR,
  15.         CHOIX_POSITION
  16.     }
  17.    
  18.     private Etat ControllerState;
  19.     private VueMastermind vue;
  20.     private ModeleMastermind modele;
  21.     private Color currentColor;
  22.     private int ligneCourante;
  23.  
  24.     public ControleurMastermind(VueMastermind vue)
  25.     {
  26.         this.vue = vue;
  27.         this.modele = new ModeleMastermind(vue.getTaille(), vue.getNbCouleurs());
  28.         this.modele.genererCombinaison();
  29.         this.ControllerState = Etat.DEBUT_COMBINAISON;
  30.     }
  31.  
  32.     public void init()
  33.     {
  34.         this.ligneCourante = 0;
  35.         vue.activerCombinaison(ligneCourante);
  36.     }
  37.    
  38.     @Override
  39.     public void actionPerformed(ActionEvent evt) {
  40.         switch(this.ControllerState)
  41.         {
  42.         case DEBUT_COMBINAISON:
  43.             {
  44.                 if(vue.appartientPalette((JButton)evt.getSource()))
  45.                 {
  46.                     this.currentColor = ((JButton)evt.getSource()).getBackground();
  47.                     this.ControllerState = Etat.CHOIX_COULEUR;
  48.                 }
  49.             }
  50.             break;
  51.         case CHOIX_COULEUR:
  52.             {
  53.                 if(vue.appartientCombinaison((JButton)evt.getSource(), ligneCourante))
  54.                 {
  55.                     ((JButton)evt.getSource()).setBackground(this.currentColor);
  56.                     this.ControllerState = Etat.CHOIX_POSITION;
  57.                 }
  58.             }
  59.             break;
  60.         case CHOIX_POSITION:
  61.             {
  62.                 if(vue.appartientPalette((JButton)evt.getSource()))
  63.                 {
  64.                     this.currentColor = ((JButton)evt.getSource()).getBackground();
  65.                     this.ControllerState = Etat.CHOIX_COULEUR;
  66.                 }
  67.                 else if(((JButton)evt.getSource()).getText() == "Valider")
  68.                 {
  69.                     this.vue.afficherBP(ligneCourante, this.modele.nbChiffresBienPlaces(this.vue.combinaisonEnEntiers(ligneCourante)));
  70.                     this.vue.afficherMP(ligneCourante, this.modele.nbChiffresMalPlaces(this.vue.combinaisonEnEntiers(ligneCourante)));
  71.                     this.vue.desactiverCombinaison(ligneCourante);
  72.                     if(this.ligneCourante != vue.NBMAX_COMBINAISONS - 1)
  73.                     {
  74.                         //On est pas arrivé au nombre max d'essais
  75.                        
  76.                         if(isSame(this.vue.combinaisonEnEntiers(ligneCourante), this.modele.getCombinaison()))
  77.                             this.vue.afficherCombinaisonOrdinateur(this.modele.getCombinaison());
  78.                         else
  79.                         {
  80.                             ligneCourante++;
  81.                             this.vue.activerCombinaison(ligneCourante);
  82.                         }
  83.                     }
  84.                     else
  85.                     {
  86.                         //On est au nombre max d'essais :'(
  87.                         this.vue.afficherCombinaisonOrdinateur(this.modele.getCombinaison());
  88.                     }
  89.                     this.ControllerState = Etat.DEBUT_COMBINAISON;
  90.                 }
  91.             }
  92.             break;
  93.         default:
  94.             break;
  95.         }
  96.        
  97.     }
  98.    
  99.     public boolean isSame(int[] par1, int[] par2)
  100.     {
  101.         if(par1.length != par2.length)
  102.             return false;
  103.         for(int i=0; i<par1.length; i++)
  104.         {
  105.             if(par1[i] != par2[i])
  106.                 return false;
  107.         }
  108.         return true;
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement