Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.95 KB | None | 0 0
  1.  
  2. package V2;
  3.  
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. public class PlateauDeJeu extends JFrame implements ActionListener{
  8.     private int TAILLEFRAME = 1000;
  9.    
  10.     private ImageIcon dosCarte = new ImageIcon("logo.jpg");
  11.    
  12.     private int[][] tab;
  13.     private JButton[][] jbCartes;
  14.     private JLabel compteur;
  15.     private int cpt;
  16.     private int cpt2 =3;
  17.     private int l1=-1,c1=-1,l2=-1,c2=-1;//coordonnées des 2 éventuelles cartes cliquées
  18.     private boolean carte1Retournee = false;
  19.     private boolean cartesRetournees = false;
  20.    
  21.     public static int aleaEntreBornes(int a, int b){
  22.         return (int)(a+(b-a)*Math.random());
  23.     }
  24.     public PlateauDeJeu(int n){
  25.         //Gestion JFrame
  26.         super("Memory");
  27.         this.setBounds(0,0,TAILLEFRAME,TAILLEFRAME);
  28.         //**********************************************************************
  29.         //Gestion jPanel servant de fond
  30.         JPanel fond = new JPanel();
  31.         fond.setLayout(null);
  32.         this.setContentPane(fond);
  33.         //**********************************************************************
  34.         //Gestion du choix du niveau
  35.         int dim = 4+n*2; // 4 (niveau le plus bas) + numéro du niveau (récupéré en paramètre)
  36.         tab = new int[dim][dim];
  37.         int[] tabAlea = new int[dim*dim];
  38.         //**********************************************************************
  39.         for(int j = 0 ; j < 2 ; j++){
  40.             for(int i = 0;i<tabAlea.length/2;i++){
  41.                 tabAlea[j*tabAlea.length/2+i] = i;
  42.             }
  43.         }
  44.         //MELANGER tabAlea
  45.         for(int i = 0 ; i < tabAlea.length ; i++){
  46.             int r = aleaEntreBornes(i,tab.length);
  47.             int t = tabAlea[i];
  48.             tabAlea[i] = tabAlea[r];
  49.             tabAlea[r] = t;
  50.         }
  51.        
  52.         for(int i = 0;i<dim;i++){
  53.             for(int j = 0;j<dim;j++){
  54.                 tab[i][j] = tabAlea[i*dim+j];
  55.             }
  56.         }
  57.         //**********************************************************************
  58.         //JPanel contenant les cartes
  59.         //**********************************************************************
  60.         JPanel cartes = new JPanel();
  61.         GridLayout g1 = new GridLayout(dim,dim,10,10);
  62.         cartes.setLayout(g1);
  63.         int tailleCarte = 80*dim+10*(dim+2); // taille du jpanel contenant les cartes
  64.         int posCarte = (TAILLEFRAME-tailleCarte)/2;
  65.         cartes.setBounds(posCarte,posCarte,tailleCarte,tailleCarte);
  66.         //**********************************************************************
  67.  
  68.         jbCartes = new JButton[dim][dim];
  69.         for(int i = 0;i<dim;i++){
  70.             for(int j = 0;j<dim;j++){
  71.                 jbCartes[i][j] = new JButton(new ImageIcon("doscarte.png"));
  72.                 jbCartes[i][j].addActionListener(this);
  73.                 cartes.add(jbCartes[i][j]);  
  74.             }
  75.         }
  76.         //**********************************************************************
  77.         //JPanel HUD avec les infos à l'écran
  78.         cpt = dim*dim/2;
  79.         compteur = new JLabel("Paires restantes : "+cpt);
  80.         JLabel nomNiveau = new JLabel("NIVEAU "+(n+1)+" - ");
  81.        
  82.         JPanel hud = new JPanel();
  83.         GridBagLayout grid1 = new GridBagLayout();
  84.         hud.setLayout(grid1);
  85.         hud.add(nomNiveau);
  86.         hud.add(compteur);
  87.         hud.setBounds(250,0,500,100);
  88.        
  89.         fond.add(hud);
  90.         fond.add(cartes);
  91.        
  92.         Color couleurfond = new Color(184,220,229);
  93.         fond.setBackground(couleurfond);
  94.         cartes.setBackground(couleurfond);
  95.         hud.setBackground(couleurfond);
  96.         this.setLocationRelativeTo(null);
  97.         this.setResizable(false);
  98.         this.setVisible(true);
  99.     }
  100.    
  101.     public void actionPerformed(ActionEvent ae){        
  102.         JButton jb = (JButton)ae.getSource();
  103.         if(cartesRetournees){//On clique une troisième fois, pour vérifier et réinitialiser
  104.             if(tab[l1][c1] == tab[l2][c2]){
  105.                 jbCartes[l1][c1].setIcon(new ImageIcon("verif.png"));
  106.                 jbCartes[l2][c2].setIcon(new ImageIcon("verif.png"));
  107.                 jbCartes[l1][c1].setEnabled(false);
  108.                 jbCartes[l2][c2].setEnabled(false);
  109.                 jbCartes[l1][c1].setDisabledIcon(jbCartes[l1][c1].getIcon());
  110.                 jbCartes[l2][c2].setDisabledIcon(jbCartes[l2][c2].getIcon());
  111.                 cpt--;
  112.                 compteur.setText("Paires restantes : "+cpt);
  113.                 if(cpt==1){cpt2=0;}
  114.             }
  115.             else{
  116.                 jbCartes[l1][c1].setIcon(new ImageIcon("doscarte.png"));
  117.                 jbCartes[l2][c2].setIcon(new ImageIcon("doscarte.png"));
  118.                 jbCartes[l1][c1].setEnabled(true);
  119.                 jbCartes[l2][c2].setEnabled(true);
  120.             }
  121.             cartesRetournees = false;
  122.             carte1Retournee = false;
  123.             System.out.println(""+cpt2);
  124.         }
  125.        
  126.         if(! carte1Retournee){//si premiere carte pas retournée
  127.             carte1Retournee = true;//elle le devient
  128.             cartesRetournees = false;
  129.             for(int i = 0;i<jbCartes.length;i++){
  130.                 for(int j = 0;j<jbCartes[i].length;j++){            
  131.                     if(jb == jbCartes[i][j]){
  132.                         l1 = i;
  133.                         c1 = j;
  134.                         jbCartes[l1][c1].setIcon(new ImageIcon("img"+tab[l1][c1]+".png"));
  135.                         jbCartes[l1][c1].setEnabled(false);
  136.                         jbCartes[l1][c1].setDisabledIcon(jbCartes[l1][c1].getIcon());
  137.                         l2=-1;c2=-1;
  138.                         cpt2++;
  139.                         break;
  140.                     }
  141.                 }
  142.             }
  143.             System.out.println(""+cpt2);
  144.         }
  145.         else{
  146.             if(!cartesRetournees){//carte 1 retournée mais pas les 2 !          
  147.                 for(int i = 0;i<jbCartes.length;i++){
  148.                     for(int j = 0;j<jbCartes[i].length;j++){              
  149.                         if(jb == jbCartes[i][j]){
  150.                             l2 = i;
  151.                             c2 = j;
  152.                             jbCartes[l2][c2].setIcon(new ImageIcon("img"+tab[l2][c2]+".png"));
  153.                             jbCartes[l2][c2].setEnabled(false);
  154.                             jbCartes[l2][c2].setDisabledIcon(jbCartes[l2][c2].getIcon());
  155.                             cpt2++;
  156.                             break;
  157.                         }
  158.                     }
  159.                 }
  160.             }
  161.             carte1Retournee = false;
  162.             cartesRetournees = true;
  163.             System.out.println(""+cpt2);
  164.         }
  165.         if(cpt2==2){
  166.             compteur.setText("Paires restantes : "+0);
  167.             JOptionPane.showMessageDialog(null,"Bravo tu as réussi à trouver toutes les paires !");
  168.             MenuDuJeu verslemenu = new MenuDuJeu();
  169.             this.dispose();
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement