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
TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Arnault Nouvel et Antoine Dongois Le processus à prendre : Apprendre (découvrir la plateforme) Préparer (documenter l'historique et choisir la méthode de MAJ) Test (Test de MAJ) Implémenter (Effectuer la MAJ) Valid...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : LA PLEINIèRE DU SECOND JOURTECHDAYS PARIS 2010 : LA PLEINIèRE DU SECOND JOUR par ROMELARD Fabrice
Après un retour sur l'histoire des TechDays de Paris et le fait que ce soit le plus gros event MS au monde (du fait de sa gratuité), le président de MS France (Eric Boustoullier) a fait une présentation de la vision Microsoft pour les années à venir...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|