Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package p32_tp4_mastermind;
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- public class ControleurMastermind implements ActionListener
- {
- private enum Etat
- {
- DEBUT_COMBINAISON,
- CHOIX_COULEUR,
- CHOIX_POSITION
- }
- private Etat ControllerState;
- private VueMastermind vue;
- private ModeleMastermind modele;
- private Color currentColor;
- private int ligneCourante;
- public ControleurMastermind(VueMastermind vue)
- {
- this.vue = vue;
- this.modele = new ModeleMastermind(vue.getTaille(), vue.getNbCouleurs());
- this.modele.genererCombinaison();
- this.ControllerState = Etat.DEBUT_COMBINAISON;
- }
- public void init()
- {
- this.ligneCourante = 0;
- vue.activerCombinaison(ligneCourante);
- }
- @Override
- public void actionPerformed(ActionEvent evt) {
- switch(this.ControllerState)
- {
- case DEBUT_COMBINAISON:
- {
- if(vue.appartientPalette((JButton)evt.getSource()))
- {
- this.currentColor = ((JButton)evt.getSource()).getBackground();
- this.ControllerState = Etat.CHOIX_COULEUR;
- }
- }
- break;
- case CHOIX_COULEUR:
- {
- if(vue.appartientCombinaison((JButton)evt.getSource(), ligneCourante))
- {
- ((JButton)evt.getSource()).setBackground(this.currentColor);
- this.ControllerState = Etat.CHOIX_POSITION;
- }
- }
- break;
- case CHOIX_POSITION:
- {
- if(vue.appartientPalette((JButton)evt.getSource()))
- {
- this.currentColor = ((JButton)evt.getSource()).getBackground();
- this.ControllerState = Etat.CHOIX_COULEUR;
- }
- else if(((JButton)evt.getSource()).getText() == "Valider")
- {
- this.vue.afficherBP(ligneCourante, this.modele.nbChiffresBienPlaces(this.vue.combinaisonEnEntiers(ligneCourante)));
- this.vue.afficherMP(ligneCourante, this.modele.nbChiffresMalPlaces(this.vue.combinaisonEnEntiers(ligneCourante)));
- this.vue.desactiverCombinaison(ligneCourante);
- if(this.ligneCourante != vue.NBMAX_COMBINAISONS - 1)
- {
- //On est pas arrivé au nombre max d'essais
- if(isSame(this.vue.combinaisonEnEntiers(ligneCourante), this.modele.getCombinaison()))
- this.vue.afficherCombinaisonOrdinateur(this.modele.getCombinaison());
- else
- {
- ligneCourante++;
- this.vue.activerCombinaison(ligneCourante);
- }
- }
- else
- {
- //On est au nombre max d'essais :'(
- this.vue.afficherCombinaisonOrdinateur(this.modele.getCombinaison());
- }
- this.ControllerState = Etat.DEBUT_COMBINAISON;
- }
- }
- break;
- default:
- break;
- }
- }
- public boolean isSame(int[] par1, int[] par2)
- {
- if(par1.length != par2.length)
- return false;
- for(int i=0; i<par1.length; i++)
- {
- if(par1[i] != par2[i])
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement