Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package V2;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class PlateauDeJeu extends JFrame implements ActionListener{
- private int TAILLEFRAME = 1000;
- private ImageIcon dosCarte = new ImageIcon("logo.jpg");
- private int[][] tab;
- private JButton[][] jbCartes;
- private JLabel compteur;
- private int cpt;
- private int cpt2 =3;
- private int l1=-1,c1=-1,l2=-1,c2=-1;//coordonnées des 2 éventuelles cartes cliquées
- private boolean carte1Retournee = false;
- private boolean cartesRetournees = false;
- public static int aleaEntreBornes(int a, int b){
- return (int)(a+(b-a)*Math.random());
- }
- public PlateauDeJeu(int n){
- //Gestion JFrame
- super("Memory");
- this.setBounds(0,0,TAILLEFRAME,TAILLEFRAME);
- //**********************************************************************
- //Gestion jPanel servant de fond
- JPanel fond = new JPanel();
- fond.setLayout(null);
- this.setContentPane(fond);
- //**********************************************************************
- //Gestion du choix du niveau
- int dim = 4+n*2; // 4 (niveau le plus bas) + numéro du niveau (récupéré en paramètre)
- tab = new int[dim][dim];
- int[] tabAlea = new int[dim*dim];
- //**********************************************************************
- for(int j = 0 ; j < 2 ; j++){
- for(int i = 0;i<tabAlea.length/2;i++){
- tabAlea[j*tabAlea.length/2+i] = i;
- }
- }
- //MELANGER tabAlea
- for(int i = 0 ; i < tabAlea.length ; i++){
- int r = aleaEntreBornes(i,tab.length);
- int t = tabAlea[i];
- tabAlea[i] = tabAlea[r];
- tabAlea[r] = t;
- }
- for(int i = 0;i<dim;i++){
- for(int j = 0;j<dim;j++){
- tab[i][j] = tabAlea[i*dim+j];
- }
- }
- //**********************************************************************
- //JPanel contenant les cartes
- //**********************************************************************
- JPanel cartes = new JPanel();
- GridLayout g1 = new GridLayout(dim,dim,10,10);
- cartes.setLayout(g1);
- int tailleCarte = 80*dim+10*(dim+2); // taille du jpanel contenant les cartes
- int posCarte = (TAILLEFRAME-tailleCarte)/2;
- cartes.setBounds(posCarte,posCarte,tailleCarte,tailleCarte);
- //**********************************************************************
- jbCartes = new JButton[dim][dim];
- for(int i = 0;i<dim;i++){
- for(int j = 0;j<dim;j++){
- jbCartes[i][j] = new JButton(new ImageIcon("doscarte.png"));
- jbCartes[i][j].addActionListener(this);
- cartes.add(jbCartes[i][j]);
- }
- }
- //**********************************************************************
- //JPanel HUD avec les infos à l'écran
- cpt = dim*dim/2;
- compteur = new JLabel("Paires restantes : "+cpt);
- JLabel nomNiveau = new JLabel("NIVEAU "+(n+1)+" - ");
- JPanel hud = new JPanel();
- GridBagLayout grid1 = new GridBagLayout();
- hud.setLayout(grid1);
- hud.add(nomNiveau);
- hud.add(compteur);
- hud.setBounds(250,0,500,100);
- fond.add(hud);
- fond.add(cartes);
- Color couleurfond = new Color(184,220,229);
- fond.setBackground(couleurfond);
- cartes.setBackground(couleurfond);
- hud.setBackground(couleurfond);
- this.setLocationRelativeTo(null);
- this.setResizable(false);
- this.setVisible(true);
- }
- public void actionPerformed(ActionEvent ae){
- JButton jb = (JButton)ae.getSource();
- if(cartesRetournees){//On clique une troisième fois, pour vérifier et réinitialiser
- if(tab[l1][c1] == tab[l2][c2]){
- jbCartes[l1][c1].setIcon(new ImageIcon("verif.png"));
- jbCartes[l2][c2].setIcon(new ImageIcon("verif.png"));
- jbCartes[l1][c1].setEnabled(false);
- jbCartes[l2][c2].setEnabled(false);
- jbCartes[l1][c1].setDisabledIcon(jbCartes[l1][c1].getIcon());
- jbCartes[l2][c2].setDisabledIcon(jbCartes[l2][c2].getIcon());
- cpt--;
- compteur.setText("Paires restantes : "+cpt);
- if(cpt==1){cpt2=0;}
- }
- else{
- jbCartes[l1][c1].setIcon(new ImageIcon("doscarte.png"));
- jbCartes[l2][c2].setIcon(new ImageIcon("doscarte.png"));
- jbCartes[l1][c1].setEnabled(true);
- jbCartes[l2][c2].setEnabled(true);
- }
- cartesRetournees = false;
- carte1Retournee = false;
- System.out.println(""+cpt2);
- }
- if(! carte1Retournee){//si premiere carte pas retournée
- carte1Retournee = true;//elle le devient
- cartesRetournees = false;
- for(int i = 0;i<jbCartes.length;i++){
- for(int j = 0;j<jbCartes[i].length;j++){
- if(jb == jbCartes[i][j]){
- l1 = i;
- c1 = j;
- jbCartes[l1][c1].setIcon(new ImageIcon("img"+tab[l1][c1]+".png"));
- jbCartes[l1][c1].setEnabled(false);
- jbCartes[l1][c1].setDisabledIcon(jbCartes[l1][c1].getIcon());
- l2=-1;c2=-1;
- cpt2++;
- break;
- }
- }
- }
- System.out.println(""+cpt2);
- }
- else{
- if(!cartesRetournees){//carte 1 retournée mais pas les 2 !
- for(int i = 0;i<jbCartes.length;i++){
- for(int j = 0;j<jbCartes[i].length;j++){
- if(jb == jbCartes[i][j]){
- l2 = i;
- c2 = j;
- jbCartes[l2][c2].setIcon(new ImageIcon("img"+tab[l2][c2]+".png"));
- jbCartes[l2][c2].setEnabled(false);
- jbCartes[l2][c2].setDisabledIcon(jbCartes[l2][c2].getIcon());
- cpt2++;
- break;
- }
- }
- }
- }
- carte1Retournee = false;
- cartesRetournees = true;
- System.out.println(""+cpt2);
- }
- if(cpt2==2){
- compteur.setText("Paires restantes : "+0);
- JOptionPane.showMessageDialog(null,"Bravo tu as réussi à trouver toutes les paires !");
- MenuDuJeu verslemenu = new MenuDuJeu();
- this.dispose();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement