Accueil > > > MASTERMIND SIMPLE ET PRATIQUE
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();
}
}
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
[FRAMEWORK 4] LES TASKS ET LE THREAD UI[FRAMEWORK 4] LES TASKS ET LE THREAD UI par fathi
Je viens de passer quelques temps au TechDay's et j'ai pu voir pas mal de session intéressante. Par contre une chose m'a un peu étonné lors de certaines de ces sessions qui abordaient les améliorations du framework .NET (donc le 4.5) : en gros, bea...
Cliquez pour lire la suite de l'article par fathi WORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBEWORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBE par JeremyJeanson
Depuis déjà un an, je conseille vivement les utilisateurs de Workflow Foundation 3 à migrer vers la version 4. L'information qui va suivre ne devrait donc pas trop prendre au dépourvu les personnes qui m'ont suivi. Je profite de ce poste, pour faire le re...
Cliquez pour lire la suite de l'article par JeremyJeanson TECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PCTECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PC par ROMELARD Fabrice
Speakers: Thierry Rapatout, Antoine Petit et Xavier Trebbia Cette session entre dans le cadre des RDV Décideurs des TechDays 2012, elle est liée à la consumérisation de l'IT et la mise en place du "DeskTop as a Service" dans de plus en ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLETECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLE par ROMELARD Fabrice
Speakers: Julien Marechal, Gautier Confiant, Sébastien MEYER La session débute par le positionnement de la solution System Center par rapport aux concepts d'organisation ITIL. Le portail du catalogue de se...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : PLEINIèRE SECOND JOURTECHDAYS PARIS 2012 : PLEINIèRE SECOND JOUR par ROMELARD Fabrice
Après une première journée dédiée aux développeurs, cette seconde journée est dédiée au monde des entreprises et de ses applications. Ainsi, cette pleinière est dédiée à faire un 360 de l'évolution des applications Business aux demandes ac...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
RE : SQLRE : SQL par Julien39
Cliquez pour lire la suite par Julien39
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|