Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

MASTERMIND SIMPLE ET PRATIQUE


Information sur la source

Description

- Mastermind le celebre petit jeu souvent utilisé comme exercice pédagogique au TPs de développement et une occasion de pratiquer les techniques de generation de combinaisons aléatoires (random) et de manipuler plusieurs autres techniques d'algorithmique et de programmation.
- dans cette version j'ai essayé d'utiliser des principes et techniques très simples basées surtout sur le changement des icones des JLabels.
- J'ai exclus les techniques graphiques car elle sont souvent très compliquées et sont plutot du Math que de la programmation.
- le jeu offre 3 niveaux de difficulté: normale, expert et commando:
   . le mode normale donne des combinaisons de 4 pions de couleurs differentes.
   . le mode expert permet d'avoir des repititions de la même couleur dans une combinaison.
   . le mode commando permet en plus des répititions d'avoir des trous vides (sans pions) dans la combinaison.

 

Source

  • import java.io.*;
  • import java.awt.*;
  • import javax.swing.*;
  • import java.awt.event.*;
  • import java.net.URL ;
  • import java.util.Vector;
  • import java.util.Random;
  • import java.applet.Applet ;
  • import java.applet.AudioClip ;
  • import java.net.MalformedURLException ;
  • public class MonMastermind extends JFrame implements MouseListener, ActionListener{
  • final int NORMALE=0;
  • final int EXPERT=1;
  • final int COMMANDO=2;
  • Container c;
  • JButton bOK,bCancel;
  • JPanel pd,pg,pn,ps,p;
  • Jeu pc;
  • JMenuBar mb1;
  • JMenu mf,mo,ma;
  • JMenuItem parti,quiter,affiche,mnormal,mexpert,mcomando,aide,appo,color;
  • JLabel selected,ldif;
  • JLabel res[]=new JLabel[48];
  • JLabel [] flc=new JLabel[12];
  • JLabel [] pions=new JLabel[7];
  • JLabel [] combin=new JLabel[4];
  • int line,difficult;
  • boolean fin;
  • Sound snd;
  • MonMastermind(){
  • super("Mon Mastermind");
  • setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  • Toolkit tk=Toolkit.getDefaultToolkit();
  • try{UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");}
  • catch(ClassNotFoundException e){}
  • catch(InstantiationException e){}
  • catch(IllegalAccessException e){}
  • catch(UnsupportedLookAndFeelException e){}
  • SwingUtilities.updateComponentTreeUI(this);
  • setSize(250,465);
  • setResizable(false);
  • c=this.getContentPane();
  • c.setLayout(new BorderLayout());
  • snd=new Sound("");
  • mb1=new JMenuBar();
  • mf=new JMenu("Fichier");
  • mo=new JMenu("Modes");
  • ma=new JMenu("Aide");
  • mf.add(parti=new JMenuItem("Nouvelle partie"));
  • mf.add(affiche=new JMenuItem("Afficher la combinaison"));
  • mf.add(color=new JMenuItem("Couleur de la table"));
  • mf.add(quiter=new JMenuItem("Quitter"));
  • mo.add(mnormal=new JMenuItem("Normal"));
  • mo.add(mexpert=new JMenuItem("Expert"));
  • mo.add(mcomando=new JMenuItem("Commando"));
  • ma.add(aide=new JMenuItem("Aide"));
  • ma.add(appo=new JMenuItem("A propos"));
  • mb1.add(mf);
  • mb1.add(mo);
  • mb1.add(ma);
  • parti.addActionListener(this);
  • affiche.addActionListener(this);
  • quiter.addActionListener(this);
  • mnormal.addActionListener(this);
  • mexpert.addActionListener(this);
  • mcomando.addActionListener(this);
  • aide.addActionListener(this);
  • appo.addActionListener(this);
  • color.addActionListener(this);
  • setJMenuBar(mb1);
  • c.add("North",pn=new JPanel(new GridLayout(1,7,0,0)));
  • c.add("West",pg=new JPanel(new GridLayout(24,2,0,0)));
  • c.add("East",pd=new JPanel(new FlowLayout(FlowLayout.CENTER,5,5)));
  • c.add("Center",p=new JPanel(new BorderLayout()));
  • pd.setPreferredSize(new Dimension(60,500));
  • pg.setPreferredSize(new Dimension(30,500));
  • pd.add(pions[0]=new JLabel(new ImageIcon("./m_rouge.gif")));
  • pd.add(pions[1]=new JLabel(new ImageIcon("./m_jaune.gif")));
  • pd.add(pions[2]=new JLabel(new ImageIcon("./m_bleu.gif")));
  • pd.add(pions[3]=new JLabel(new ImageIcon("./m_marron.gif")));
  • pd.add(pions[4]=new JLabel(new ImageIcon("./m_vert.gif")));
  • pd.add(pions[5]=new JLabel(new ImageIcon("./m_orange.gif")));
  • pd.add(pions[6]=new JLabel(new ImageIcon("./m_trou.gif")));
  • p.add("Center",pc=new Jeu(selected=new JLabel(),pions[6].getIcon(),snd));
  • p.add("West",ps=new JPanel(new GridLayout(12,1,0,0)));
  • pions[6].setVisible(false);
  • pd.add(new JLabel(" "));
  • pd.add(new JLabel("Difficulté"));
  • pd.add(ldif=new JLabel("Normale"));
  • ldif.setForeground(Color.blue);
  • pd.add(new JLabel(" "));
  • pd.add(bOK=new JButton("OK"));
  • pd.add(bCancel=new JButton("Annuler"));
  • pd.add(new JLabel(" "));
  • pd.add(new JLabel("Selection"));
  • pd.add(selected);
  • pn.add(new JLabel("Res"));
  • for(int i=0;i<4;i++){
  • pn.add(combin[i]=new JLabel());
  • }
  • Icon ic=new ImageIcon("./m_flech.gif");
  • for(int i=11;i>=0;i--){
  • ps.add(flc[i]=new JLabel(ic));
  • flc[i].setEnabled(false);
  • }
  • pn.add(new JLabel("Pions"));
  • bOK.addActionListener(this);
  • bCancel.addActionListener(this);
  • for(int i=0;i<6;i++){
  • pions[i].addMouseListener(this);
  • }
  • difficult=NORMALE;
  • for(int i=47;i>=0;i--){
  • pg.add(res[i]=new JLabel());
  • }
  • initialiser();
  • show();
  • }
  • public void initialiser(){
  • line=0;
  • fin=false;
  • select(0);
  • Icon ic=new ImageIcon("./m_ptrou.gif");
  • for(int i=47;i>=0;i--){
  • res[i].setIcon(ic);
  • }
  • nouvellCombine();
  • }
  • public void select(int l){
  • pc.selectLine(l);
  • for(int i=0;i<12;i++){
  • flc[i].setEnabled(false);
  • }
  • flc[l].setEnabled(true);
  • }
  • public void nouvellCombine(){
  • Vector v=new Vector();
  • Random rnd=new Random();
  • int n;
  • if(difficult==NORMALE){
  • while(v.size()<4){
  • n=Math.abs(rnd.nextInt()%6);
  • if(!v.contains(new Integer(n)))v.add(new Integer(n));
  • }
  • }else
  • if(difficult==EXPERT){
  • while(v.size()<4){
  • n=Math.abs(rnd.nextInt()%6);
  • v.add(new Integer(n));
  • }
  • }else
  • if(difficult==COMMANDO){
  • while(v.size()<4){
  • n=Math.abs(rnd.nextInt()%7);
  • v.add(new Integer(n));
  • }
  • }
  • for(int i=0;i<4;i++){
  • combin[i].setIcon(pions[((Integer)v.get(i)).intValue()].getIcon());
  • combin[i].setVisible(false); //pour cacher la combinaison
  • }
  • }
  • public void nouvelPartie(){
  • initialiser();
  • pc.initialiser();
  • }
  • public Dimension evaluerCombine(){
  • int bex=0,bpl=0,j=0,p;
  • p=line*4;
  • for(int i=p;i<p+4;i++,j++){
  • if (pc.lab[i].getIcon() == combin[3-j].getIcon()) bpl++;
  • for(int k=p;k<p+4;k++){
  • if (pc.lab[k].getIcon()== combin[3-j].getIcon()){
  • bex++;
  • k=p+4;
  • }
  • }
  • }
  • bex = bex - bpl;
  • return (new Dimension(bex,bpl));
  • }
  • public void showResults(Dimension dim){
  • int j=0,ex=dim.width,pl=dim.height;
  • if(pl==4) fin=true;
  • while(ex>0){
  • res[line*4+j].setIcon(new ImageIcon("./m_bblanc.gif"));
  • ex--;
  • j++;
  • snd.play("tounc.wav");
  • }
  • while(pl>0){
  • res[line*4+j].setIcon(new ImageIcon("./m_bnoir.gif"));
  • pl--;
  • j++;
  • snd.play("tounc.wav");
  • }
  • if(fin){
  • snd.play("bravo.wav");
  • new Alerte(this,"Partie terminée, vous avez gagné!!");
  • for(int i=0;i<4;i++){
  • combin[i].setVisible(true);
  • }
  • }
  • }
  • public void mousePressed(MouseEvent e){}
  • public void mouseReleased(MouseEvent e){}
  • public void mouseExited(MouseEvent e){}
  • public void mouseEntered(MouseEvent e){}
  • public void mouseClicked(MouseEvent e){
  • selected.setIcon(((JLabel)e.getSource()).getIcon());
  • }
  • public void actionPerformed(ActionEvent e){
  • if(e.getSource()==bOK){
  • if(!fin){
  • snd.play("Cloche.wav");
  • showResults(evaluerCombine());
  • if(line<11) select(++line);
  • else{
  • for(int i=0;i<4;i++){
  • combin[i].setVisible(true);
  • }
  • snd.play("Erreur.wav");
  • new Alerte(this,"Partie terminée! Vous avez échoué!");
  • fin=true;
  • }
  • }else{
  • snd.play("Erreur.wav");
  • new Alerte(this,"Veuillez commancer une nouvelle partie!");
  • }
  • }else
  • if(e.getSource()==bCancel){
  • for(int i=line*4;i<(line*4+4);i++){
  • pc.lab[i].setIcon(pions[6].getIcon());
  • }
  • snd.play("Zoom.wav");
  • }else
  • if(e.getSource()==parti){
  • nouvelPartie();
  • }else
  • if(e.getSource()==mnormal){
  • difficult=NORMALE;
  • ldif.setText("Normale");
  • nouvelPartie();
  • }else
  • if(e.getSource()==mexpert){
  • difficult=EXPERT;
  • ldif.setText("Expert");
  • nouvelPartie();
  • }else
  • if(e.getSource()==mcomando){
  • difficult=COMMANDO;
  • ldif.setText("Commando");
  • nouvelPartie();
  • }else
  • if(e.getSource()==affiche){
  • fin=true;
  • for(int i=0;i<4;i++){
  • combin[i].setVisible(true);
  • }
  • }else
  • if(e.getSource()==quiter){
  • System.exit(0);
  • }else
  • if(e.getSource()==aide){
  • new Aide(this);
  • }else
  • if(e.getSource()==appo){
  • new APropos(this);
  • }else
  • if(e.getSource()==color){
  • new Couleur(this,pc);
  • }
  • }
  • public static void main(String []args){
  • new MonMastermind();
  • }
  • }
  • class Jeu extends JPanel implements MouseListener{
  • JLabel la;
  • JLabel [] lab=new JLabel[48];
  • Icon ico;
  • Sound snd;
  • Jeu(JLabel l,Icon ic, Sound s){
  • super(new GridLayout(12,4,0,0));
  • la=l;
  • ico=ic;
  • snd=s;
  • setBackground(new Color(20,200,255));
  • for(int i=47;i>=0;i--){
  • add(lab[i]=new JLabel(ico));
  • }
  • }
  • public void initialiser(){
  • for(int i=47;i>=0;i--){
  • lab[i].setIcon(ico);
  • }
  • snd.play("Zoom.wav");
  • }
  • public void selectLine(int l){
  • int i;
  • for(i=0;i<48;i++){
  • lab[i].removeMouseListener(this);
  • }
  • i=l*4;
  • for(int j=i;j<(i+4);j++){
  • lab[j].addMouseListener(this);
  • }
  • }
  • public void mousePressed(MouseEvent e){}
  • public void mouseReleased(MouseEvent e){}
  • public void mouseExited(MouseEvent e){}
  • public void mouseEntered(MouseEvent e){}
  • public void mouseClicked(MouseEvent e){
  • if(la.getIcon()!=null){
  • ((JLabel)e.getSource()).setIcon(la.getIcon());
  • la.setIcon(null);
  • snd.play("Pong.wav");
  • }
  • }
  • }
  • class Alerte extends Dialog{
  • Button boutonOK=new Button("OK");
  • JPanel p;
  • Alerte(JFrame f,String alerteTexte){
  • super(f,"alert",true);
  • add("Center",new Label(alerteTexte));
  • add("South",p=new JPanel());
  • p.add(boutonOK);
  • pack();
  • show();
  • }
  • public boolean action(Event e, Object arg){
  • if(e.target==boutonOK){
  • hide();
  • dispose();
  • return true;
  • }else return false;
  • }
  • }
  • class Aide extends Dialog{
  • Button boutonOK=new Button("OK");
  • JTextArea t=new JTextArea(20,50);
  • Aide(Frame f){
  • super(f,"Mon Mastermind",true);
  • t.setFont(new Font("Courier",Font.CENTER_BASELINE,12));
  • t.setEditable(false);
  • t.setVisible(true);
  • t.setBackground(Color.white);
  • try{BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream(new File("./help.blt"))));
  • String lu=new String("");
  • try{
  • while(!(lu=in.readLine()).equals("fin")){
  • t.append(lu+"\n");
  • }
  • }catch(IOException e){}
  • }catch(FileNotFoundException e){}
  • add("Center",t);
  • JPanel pp=new JPanel();
  • pp.add(boutonOK);
  • add("South",pp);
  • pack();
  • show();
  • }
  • public boolean action(Event e, Object arg){
  • if (e.target==boutonOK) {
  • hide();
  • dispose();
  • return true;
  • }
  • return false;
  • }
  • }
  • class Couleur extends Dialog implements javax.swing.event.ChangeListener{
  • Button boutonOK=new Button("OK");
  • Jeu je;
  • JLabel lr,lv,lb;
  • JSlider sr,sv,sb;
  • Couleur(Frame f, Jeu j){
  • super(f,"Mon Mastermind",true);
  • je=j;
  • JPanel p=new JPanel(new GridLayout(3,2,5,5));
  • JPanel pp=new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
  • pp.add(boutonOK);
  • p.add(lr=new JLabel("Rouge"));
  • p.add(sr=new JSlider(0,255,0));
  • p.add(lv=new JLabel("Vert"));
  • p.add(sv=new JSlider(0,255,100));
  • p.add(lb=new JLabel("Bleu"));
  • p.add(sb=new JSlider(0,255,255));
  • sr.addChangeListener(this);
  • sv.addChangeListener(this);
  • sb.addChangeListener(this);
  • add("Center",p);
  • add("South",pp);
  • pack();
  • show();
  • }
  • public void stateChanged(javax.swing.event.ChangeEvent ce){
  • boutonOK.setBackground(new Color(sr.getValue(),sv.getValue(),sb.getValue()));
  • }
  • public boolean action(Event e, Object arg){
  • if (e.target==boutonOK) {
  • je.setBackground(boutonOK.getBackground());
  • hide();
  • dispose();
  • return true;
  • }
  • return false;
  • }
  • }
  • class APropos extends Dialog{
  • Button boutonOK=new Button("OK");
  • APropos(Frame f){
  • super(f,"Mon Mastermind",true);
  • PanelAP p=new PanelAP();
  • p.setLayout(new GridLayout(11,1,10,10));
  • JPanel pp=new JPanel();
  • pp.add(boutonOK);
  • setSize(100,200);
  • add("North",new Label("Mini-projet Mastermind"));
  • add("Center",p);
  • add("South",pp);
  • pack();
  • show();
  • resize(160,400);
  • }
  • public boolean action(Event e, Object arg){
  • if (e.target==boutonOK) {
  • hide();
  • dispose();
  • return true;
  • }
  • return false;
  • }
  • }
  • class PanelAP extends java.applet.Applet{
  • Image m;
  • PanelAP(){
  • super();
  • Toolkit tk=Toolkit.getDefaultToolkit();
  • m=tk.getImage("./arrp.gif");
  • repaint();
  • resize(100,200);
  • }
  • public void paint(Graphics g){
  • g.drawImage(m,0,0,this);
  • g.setColor(Color.blue);
  • g.drawString("Développé par:",0,30);
  • g.drawString(" BENBOUMEHDI Imane",0,45);
  • g.drawString(" BELATAR",0,55);
  • g.drawString(" Y",0,65);
  • }
  • }
  • class Sound{
  • private URL file ;
  • private AudioClip sound ;
  • public Sound(String name){
  • try{
  • file = new URL("file://"+name) ;
  • sound = Applet.newAudioClip(file) ;
  • }
  • catch (MalformedURLException e){}
  • }
  • public void play(String name)
  • {
  • try
  • {
  • File f=new File(name);
  • file = new URL("file://"+f.getAbsolutePath()) ;
  • sound = Applet.newAudioClip(file) ;
  • sound.play();
  • }
  • catch (MalformedURLException e){}
  • }
  • public void stop(){
  • sound.stop();
  • }
  • }
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.URL ;
import java.util.Vector;
import java.util.Random;
import java.applet.Applet ;
import java.applet.AudioClip ;
import java.net.MalformedURLException ;


public class MonMastermind extends JFrame implements MouseListener, ActionListener{
	final int NORMALE=0;
	final int EXPERT=1;
	final int COMMANDO=2;
	Container c;
	JButton bOK,bCancel;
	JPanel pd,pg,pn,ps,p;
	Jeu pc;
	JMenuBar mb1;
	JMenu mf,mo,ma;
	JMenuItem parti,quiter,affiche,mnormal,mexpert,mcomando,aide,appo,color;
	JLabel selected,ldif;
	JLabel res[]=new JLabel[48];
	JLabel [] flc=new JLabel[12];
	JLabel [] pions=new JLabel[7];
	JLabel [] combin=new JLabel[4];
	int line,difficult;
	boolean fin;
	Sound snd;
	
	MonMastermind(){
		super("Mon Mastermind");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Toolkit tk=Toolkit.getDefaultToolkit();
		try{UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");}
		catch(ClassNotFoundException e){}
		catch(InstantiationException e){}
		catch(IllegalAccessException e){}
		catch(UnsupportedLookAndFeelException e){}
		SwingUtilities.updateComponentTreeUI(this);
		setSize(250,465);
		setResizable(false);
		c=this.getContentPane();
		c.setLayout(new BorderLayout());
		snd=new Sound("");
		mb1=new JMenuBar();
		mf=new JMenu("Fichier");
		mo=new JMenu("Modes");
		ma=new JMenu("Aide");
		mf.add(parti=new JMenuItem("Nouvelle partie"));
		mf.add(affiche=new JMenuItem("Afficher la combinaison"));
		mf.add(color=new JMenuItem("Couleur de la table"));
		mf.add(quiter=new JMenuItem("Quitter"));
		mo.add(mnormal=new JMenuItem("Normal"));
		mo.add(mexpert=new JMenuItem("Expert"));
		mo.add(mcomando=new JMenuItem("Commando"));
		ma.add(aide=new JMenuItem("Aide"));
		ma.add(appo=new JMenuItem("A propos"));
		mb1.add(mf);
		mb1.add(mo);
		mb1.add(ma);
		parti.addActionListener(this);
		affiche.addActionListener(this);
		quiter.addActionListener(this);
		mnormal.addActionListener(this);
		mexpert.addActionListener(this);
		mcomando.addActionListener(this);
		aide.addActionListener(this);
		appo.addActionListener(this);
		color.addActionListener(this);
		setJMenuBar(mb1);
		c.add("North",pn=new JPanel(new GridLayout(1,7,0,0)));
		c.add("West",pg=new JPanel(new GridLayout(24,2,0,0)));
		c.add("East",pd=new JPanel(new FlowLayout(FlowLayout.CENTER,5,5)));
		c.add("Center",p=new JPanel(new BorderLayout()));
		pd.setPreferredSize(new Dimension(60,500));
		pg.setPreferredSize(new Dimension(30,500));
		pd.add(pions[0]=new JLabel(new ImageIcon("./m_rouge.gif")));
		pd.add(pions[1]=new JLabel(new ImageIcon("./m_jaune.gif")));
		pd.add(pions[2]=new JLabel(new ImageIcon("./m_bleu.gif")));
		pd.add(pions[3]=new JLabel(new ImageIcon("./m_marron.gif")));
		pd.add(pions[4]=new JLabel(new ImageIcon("./m_vert.gif")));
		pd.add(pions[5]=new JLabel(new ImageIcon("./m_orange.gif")));
		pd.add(pions[6]=new JLabel(new ImageIcon("./m_trou.gif")));
		p.add("Center",pc=new Jeu(selected=new JLabel(),pions[6].getIcon(),snd));
		p.add("West",ps=new JPanel(new GridLayout(12,1,0,0)));
		pions[6].setVisible(false);
		pd.add(new JLabel("        "));
		pd.add(new JLabel("Difficulté"));
		pd.add(ldif=new JLabel("Normale"));
		ldif.setForeground(Color.blue);
		pd.add(new JLabel("       "));
		pd.add(bOK=new JButton("OK"));
		pd.add(bCancel=new JButton("Annuler"));
		pd.add(new JLabel("       "));
		pd.add(new JLabel("Selection"));
		pd.add(selected);
		pn.add(new JLabel("Res"));
		for(int i=0;i<4;i++){
			pn.add(combin[i]=new JLabel());
		}
		Icon ic=new ImageIcon("./m_flech.gif");
		for(int i=11;i>=0;i--){
			ps.add(flc[i]=new JLabel(ic));
			flc[i].setEnabled(false);
		}
		pn.add(new JLabel("Pions"));
		bOK.addActionListener(this);
		bCancel.addActionListener(this);
		for(int i=0;i<6;i++){
			pions[i].addMouseListener(this);
		}
		difficult=NORMALE;
		for(int i=47;i>=0;i--){
			pg.add(res[i]=new JLabel());
		}
		initialiser();
		show();
	}
	
	public void initialiser(){
		line=0;
		fin=false;
		select(0);
		Icon ic=new ImageIcon("./m_ptrou.gif");
		for(int i=47;i>=0;i--){
			res[i].setIcon(ic);
		}
		nouvellCombine();
	}
	
	public void select(int l){
		pc.selectLine(l);
		for(int i=0;i<12;i++){
			flc[i].setEnabled(false);
		}
			flc[l].setEnabled(true);
	}
	
	public void nouvellCombine(){
		Vector v=new Vector();
		Random rnd=new Random();
		int n;
		if(difficult==NORMALE){
			while(v.size()<4){
				n=Math.abs(rnd.nextInt()%6);
				if(!v.contains(new Integer(n)))v.add(new Integer(n));
			}
		}else
		if(difficult==EXPERT){
			while(v.size()<4){
				n=Math.abs(rnd.nextInt()%6);
				v.add(new Integer(n));
			}
		}else
		if(difficult==COMMANDO){
			while(v.size()<4){
				n=Math.abs(rnd.nextInt()%7);
				v.add(new Integer(n));
			}
		}
		for(int i=0;i<4;i++){
			combin[i].setIcon(pions[((Integer)v.get(i)).intValue()].getIcon());
			combin[i].setVisible(false);             //pour cacher la combinaison
		}
	}
	
	public void nouvelPartie(){
		initialiser();
		pc.initialiser();
	}
	
	public Dimension evaluerCombine(){
		int bex=0,bpl=0,j=0,p;
		p=line*4;
		for(int i=p;i<p+4;i++,j++){
  			if (pc.lab[i].getIcon() == combin[3-j].getIcon()) bpl++;
  			for(int k=p;k<p+4;k++){
      			if (pc.lab[k].getIcon()== combin[3-j].getIcon()){
      				bex++;
      				k=p+4;
      			}
   			}	
		}
		bex = bex - bpl;
		return (new Dimension(bex,bpl));
	}
	
	public void showResults(Dimension dim){
		int j=0,ex=dim.width,pl=dim.height;
		if(pl==4) fin=true;
		while(ex>0){
			res[line*4+j].setIcon(new ImageIcon("./m_bblanc.gif"));
			ex--;
			j++;
			snd.play("tounc.wav");
		}
		while(pl>0){
			res[line*4+j].setIcon(new ImageIcon("./m_bnoir.gif"));
			pl--;
			j++;
			snd.play("tounc.wav");
		}
		if(fin){
			snd.play("bravo.wav");
			new Alerte(this,"Partie terminée, vous avez gagné!!");
			for(int i=0;i<4;i++){
				combin[i].setVisible(true);
			}
		}
	}
	
	public void mousePressed(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseClicked(MouseEvent e){
		selected.setIcon(((JLabel)e.getSource()).getIcon());
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==bOK){
			if(!fin){
				snd.play("Cloche.wav");
				showResults(evaluerCombine());
				if(line<11) select(++line);
				else{
					for(int i=0;i<4;i++){
						combin[i].setVisible(true);
					}
					snd.play("Erreur.wav");
					new Alerte(this,"Partie terminée! Vous avez échoué!");
					fin=true;
				}
			}else{
				snd.play("Erreur.wav");
				new Alerte(this,"Veuillez commancer une nouvelle partie!");
			}
		}else
		if(e.getSource()==bCancel){
			for(int i=line*4;i<(line*4+4);i++){
				pc.lab[i].setIcon(pions[6].getIcon());
			}
			snd.play("Zoom.wav");
		}else
		if(e.getSource()==parti){
			nouvelPartie();
		}else
		if(e.getSource()==mnormal){
			difficult=NORMALE;
			ldif.setText("Normale");
			nouvelPartie();
		}else
		if(e.getSource()==mexpert){
			difficult=EXPERT;
			ldif.setText("Expert");
			nouvelPartie();		
		}else
		if(e.getSource()==mcomando){
			difficult=COMMANDO;
			ldif.setText("Commando");
			nouvelPartie();			
		}else
		if(e.getSource()==affiche){
			fin=true;
			for(int i=0;i<4;i++){
				combin[i].setVisible(true);
			}
		}else
		if(e.getSource()==quiter){
			System.exit(0);
		}else
		if(e.getSource()==aide){
			new Aide(this);
		}else
		if(e.getSource()==appo){
			new APropos(this);
		}else
		if(e.getSource()==color){
			new Couleur(this,pc);
		}
	}
	
	public static void main(String []args){
		new MonMastermind();
	}
}

class Jeu extends JPanel implements MouseListener{
	JLabel la;
	JLabel [] lab=new JLabel[48];
	Icon ico;
	Sound snd;
	
	Jeu(JLabel l,Icon ic, Sound s){
		super(new GridLayout(12,4,0,0));
		la=l;
		ico=ic;
		snd=s;
		setBackground(new Color(20,200,255));
		for(int i=47;i>=0;i--){
			add(lab[i]=new JLabel(ico));
		}
	}
	
	public void initialiser(){
		for(int i=47;i>=0;i--){
			lab[i].setIcon(ico);
		}
		snd.play("Zoom.wav");
	}
	
	public void selectLine(int l){
		int i;
		for(i=0;i<48;i++){
			lab[i].removeMouseListener(this);
		}
		i=l*4;
		for(int j=i;j<(i+4);j++){
			lab[j].addMouseListener(this);
		}
	}
	
	public void mousePressed(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseClicked(MouseEvent e){
		if(la.getIcon()!=null){
			((JLabel)e.getSource()).setIcon(la.getIcon());
			la.setIcon(null);
			snd.play("Pong.wav");
		}
	}
}

class Alerte extends Dialog{
    Button boutonOK=new Button("OK");
	JPanel p;
	
   Alerte(JFrame f,String alerteTexte){
         super(f,"alert",true);
         add("Center",new Label(alerteTexte));
         add("South",p=new JPanel());
         p.add(boutonOK);
         pack();
         show();
    }
    
public boolean action(Event e, Object arg){
     if(e.target==boutonOK){ 
         hide();
         dispose();
         return true;
      }else return false;
   }
}

class Aide extends Dialog{
    Button boutonOK=new Button("OK");
	JTextArea t=new JTextArea(20,50);
	Aide(Frame f){
         super(f,"Mon Mastermind",true);
         t.setFont(new Font("Courier",Font.CENTER_BASELINE,12));
         t.setEditable(false);
         t.setVisible(true);
         t.setBackground(Color.white);
         try{BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream(new File("./help.blt"))));
         String lu=new String("");
        try{
        	while(!(lu=in.readLine()).equals("fin")){
       		  t.append(lu+"\n");
      		}
      	}catch(IOException e){}
         }catch(FileNotFoundException e){}
         add("Center",t);
         JPanel pp=new JPanel();
         pp.add(boutonOK);
         add("South",pp);
         pack();
         show();
    }

	public boolean action(Event e, Object arg){
   	  if (e.target==boutonOK)  { 
  	       hide();
  	       dispose();
  	       return true;
  	    }
  	    return false;
  	 }
}

class Couleur extends Dialog implements javax.swing.event.ChangeListener{
    Button boutonOK=new Button("OK");
	Jeu je;
	JLabel lr,lv,lb;
	JSlider sr,sv,sb;
	Couleur(Frame f, Jeu j){
		super(f,"Mon Mastermind",true);
		je=j;
		JPanel p=new JPanel(new GridLayout(3,2,5,5));
		JPanel pp=new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
		pp.add(boutonOK);
		p.add(lr=new JLabel("Rouge"));
		p.add(sr=new JSlider(0,255,0));
		p.add(lv=new JLabel("Vert"));
		p.add(sv=new JSlider(0,255,100));
		p.add(lb=new JLabel("Bleu"));
		p.add(sb=new JSlider(0,255,255));
		sr.addChangeListener(this);
		sv.addChangeListener(this);
		sb.addChangeListener(this);
		add("Center",p);
		add("South",pp);
		pack();
        show();
	}
	
	public void stateChanged(javax.swing.event.ChangeEvent ce){
		boutonOK.setBackground(new Color(sr.getValue(),sv.getValue(),sb.getValue()));
	}
	
	public boolean action(Event e, Object arg){
   	  if (e.target==boutonOK)  { 
   	  je.setBackground(boutonOK.getBackground());
  	       hide();
  	       dispose();
  	       return true;
  	    }
  	    return false;
  	 }
}

class APropos extends Dialog{
    Button boutonOK=new Button("OK");
	APropos(Frame f){
         super(f,"Mon Mastermind",true);
         PanelAP p=new PanelAP();
         p.setLayout(new GridLayout(11,1,10,10));
         JPanel pp=new JPanel();
         pp.add(boutonOK);
         setSize(100,200);
		 add("North",new Label("Mini-projet Mastermind"));
         add("Center",p);
         add("South",pp);
         pack();
         show();
         resize(160,400);
    }

	public boolean action(Event e, Object arg){
   	  if (e.target==boutonOK)  { 
  	       hide();
  	       dispose();
  	       return true;
  	    }
  	    return false;
  	 }
}

class PanelAP extends java.applet.Applet{
	Image m;
	PanelAP(){
		super();
		Toolkit tk=Toolkit.getDefaultToolkit();
		m=tk.getImage("./arrp.gif");
		repaint();
		resize(100,200);
	}
	
	public void paint(Graphics g){
		g.drawImage(m,0,0,this);
		g.setColor(Color.blue);
		g.drawString("Développé par:",0,30);
		g.drawString("  BENBOUMEHDI Imane",0,45);
		g.drawString("  BELATAR",0,55);
		g.drawString("  Y",0,65);
	}
}

class Sound{
    private URL file ;
    private AudioClip sound ;

    public Sound(String name){
	    try{
	    	file = new URL("file://"+name) ;
	    	sound = Applet.newAudioClip(file) ;
	    }
	    catch (MalformedURLException e){}
	}

    public void play(String name)
        {
        try
          {
          	File f=new File(name);
            file = new URL("file://"+f.getAbsolutePath()) ;
            sound = Applet.newAudioClip(file) ;
            sound.play();
          }
          catch (MalformedURLException e){}
        }
    
    public void stop(){
    	sound.stop();
    }
}

Fichier Zip

Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip

Commentaires et avis

signaler à un administrateur
Commentaire de gmi19oj19 le 27/09/2004 15:16:42

2 (petits) reproches :

- ne mets pas tout le code comme ça, c'est pénible pour les 56k (je me répète par rapport à d'autres sources, je sais)
- il eut été préférable de séparer partie interface et partie traitement, java s'y prétends bien.

signaler à un administrateur
Commentaire de AbriBus le 23/10/2004 06:09:00

simple et rapide, heh ? ;-)

Ajouter un commentaire



Nos sponsors

Sondage...

CalendriCode

Septembre 2008
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
2930     

Consulter la suite du CalendriCode



Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel BAÏSE, Merci à Vincent pour ses précieux conseils
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés
Temps d'éxécution de la page : 0,47 sec

Google Coop CodeS-SourceS Google Coop CodeS-SourceS