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
[RIA SERVICES] INCLUDE ET DOMAINDATASOURCE[RIA SERVICES] INCLUDE ET DOMAINDATASOURCE par Audrey
Dans un de mes articles précédents , j'avais parlé des DomainDataSource avec RIA Services dans le cas d'une interface Maître - Détail. Dans le même principe, je vais parler d'une autre manière de mettre en forme ce cas d'interface avec RIA Services. Et po...
Cliquez pour lire la suite de l'article par Audrey ZUNE : VERSION ZUNE SOFTWARE V 4.2 ET LA SOCIALISATIONZUNE : VERSION ZUNE SOFTWARE V 4.2 ET LA SOCIALISATION par ROMELARD Fabrice
Une des nouveautés de la version V 3.0 était l'apparition de l'onglet Social qui ne fonctionnait que si le MarketPlace était activé sur son poste. Cela limitait donc son intérêt, car hors du cadre commercial USA-CANADA, peu de monde trouva...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice PRATIQUE DE SILVERLIGHT PAR ERIC AMBROSIPRATIQUE DE SILVERLIGHT PAR ERIC AMBROSI par MPOWARE
Je viens de finir la lecture du dernier livre d'
Eric Ambrosi
éditions PEARSON
Son livre donne une approche pratique de Silverlight qui sera aussi bien comprise par le développeur que par le designeur.
Tous les aspects du développement RIA sont abor...
Cliquez pour lire la suite de l'article par MPOWARE APPRENDRE à DéVELOPPER POUR LES MOBILES AVEC LA NOUVELLE GéNéRATION .NETAPPRENDRE à DéVELOPPER POUR LES MOBILES AVEC LA NOUVELLE GéNéRATION .NET par odewit
2 déclinaisons de Silverlight et 2 déclinaisons de Mono permettent dorénavant (ou permettront prochainement) de développer des applications .NET mobiles pour les principales plates-formes du marché :
Silverlight pour Symbian, basé sur Silverlight 2...
Cliquez pour lire la suite de l'article par odewit ZUNE : NOUVELLE VERSION DU ZUNE SOFTWARE - V 4.2ZUNE : NOUVELLE VERSION DU ZUNE SOFTWARE - V 4.2 par ROMELARD Fabrice
Avec la dernière génération du lecteur MP3 de Microsoft, le ZUNE HD, Microsoft a publié une nouvelle version du logiciel pour PC. Ainsi, je me suis décidé à installer celle-ci sur mon Tablet PC ACER, comme toujours le logiciel est donc tél...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
RE : ANDROIDRE : ANDROID par angie23
Cliquez pour lire la suite par angie23 EXERCICE EN JAVAEXERCICE EN JAVA par nouna1551
Cliquez pour lire la suite par nouna1551 PARALLELISATIONPARALLELISATION par infogoss
Cliquez pour lire la suite par infogoss
Logiciels
Academy System (10.9.4.0)ACADEMY SYSTEM (10.9.4.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Xilisoft Convertisseur Vidéo Ultimate (5.1.39.0305)XILISOFT CONVERTISSEUR VIDéO ULTIMATE (5.1.39.0305)Xilisoft Convertisseur Vidéo Ultimate est un outil puissant de conversion vidéo, facile à utilise... Cliquez pour télécharger Xilisoft Convertisseur Vidéo Ultimate Xilisoft DVD Ripper Ultimate (5.0.64.0304)XILISOFT DVD RIPPER ULTIMATE (5.0.64.0304)Xilisoft DVD Ripper Ultimate est un logiciel excellent pour copier et convertir DVD vers presque ... Cliquez pour télécharger Xilisoft DVD Ripper Ultimate Rigs of Rods (63.3)RIGS OF RODS (63.3)c'est un jeu de multi-simulation camions,autobus voitures, avions, bateaux, hélicoptère avec défo... Cliquez pour télécharger Rigs of Rods
|