proPares

Este código es para crear un juego de memoria, para formar parejas entre cartas, utilizamos matrices, imágenes, clase random, y el ciclo for para poder hacerla, a través del código aparecen comentarios sobre para que sirven las diferentes lineas.



public class JPares2 extends javax.swing.JFrame {

    Integer[][] matriz= new Integer[6][6];
     JLabel[][] botones=new JLabel[6][6];
    /**
     * Creates new form JPares2
     */
    public JPares2() {
        initComponents();
         this.setBounds(0,0, 600,620);
        
        for (int i=0; i<6; i++)
            for (int j=0; j<6; j++)
        {
            botones[i][j] = new JLabel();
        botones[i][j].setBounds(j*100,i*100, 80,80);
        this.add(botones[i][j]);
        botones[i][j].setVisible(true);
        botones[i][j].setText(i+1+"");
        botones[i][j].addMouseListener(list);
        ImageIcon icono=new ImageIcon(getClass().getResource("..\\paqImagenes\\revés.jpg"));
        Image imagen1=icono.getImage();
        ImageIcon icono_escalado=new ImageIcon(imagen1.getScaledInstance(80, 80, Image.SCALE_SMOOTH));
        botones[i][j].setIcon(icono_escalado);
        
        }
        
        for (int i=0; i<6; i++)
            for (int j=0; j<6; j++)
            {
              matriz[i][j]=0;
       }
        
        Random r=new Random();
        for (int i=0; i<6; i++)
            for (int j=0; j<6; j++)
            {
                int aux=r.nextInt(36)+1;
                boolean repetido=true;
                while(repetido==true)
                {
                    repetido=false;
                    for (int x=0; x<6; x++)
                        for (int y=0; y<6; y++)
                        {
                            if(aux==matriz[x][y])
                            {
                                repetido=true;
                                aux=r.nextInt(36)+1;
                            }
                        }
            }
                matriz[i][j]=aux;
    }
        for (int i=0; i<6; i++)
            for (int j=0; j<6; j++)
                if(matriz[i][j]>18)
                    matriz[i][j]=matriz[i][j]-18;
        for (int i=0; i<6; i++)
            for (int j=0; j<6; j++)
            {
               ImageIcon icono=new ImageIcon(getClass().getResource("..\\paqImagenes\\revés.jpg"));
        Image imagen1=icono.getImage();
        ImageIcon icono_escalado=new ImageIcon(imagen1.getScaledInstance(80, 80, Image.SCALE_SMOOTH));
        botones[i][j].setIcon(icono_escalado);  
            }
    }    

    Integer coori1=0,coorj1=0,coori2=0,coorj2=0,click=0;
    
    MouseListener list=new MouseListener(){

        @Override
        public void mouseClicked(MouseEvent e) {
          for (int i=0; i<6; i++)
            for (int j=0; j<6; j++)
            {
                if(e.getComponent().equals(botones[i][j]))
                {
                 ImageIcon icono=new ImageIcon(getClass().getResource("..\\paqImagenes\\"+matriz[i][j]+".jpg"));
        Image imagen1=icono.getImage();
        ImageIcon icono_escalado=new ImageIcon(imagen1.getScaledInstance(80, 80, Image.SCALE_SMOOTH));
        botones[i][j].setIcon(icono_escalado);    
           
        if(click==0)
        {
          click=1; //cambio el valor para que el pc entienda para que la proxima vez ya no es la primera  
          coori1=i;
          coorj1=j;
        }
        
        else
        {
          click=0;  //cambio el valor para que entienda que vuelve a ser la primera  
          coori2=i;
          coorj2=j;
          
             if(matriz[coori1][coorj1]!=matriz[coori2][coorj2])
             {
                 JOptionPane.showMessageDialog(null,"intente de nuevo");
                 ImageIcon icono1=new ImageIcon(getClass().getResource("..\\paqImagenes\\revés.jpg"));
                 Image imagen2=icono1.getImage();
                 ImageIcon icono_escalado1=new ImageIcon(imagen2.getScaledInstance(80, 80, Image.SCALE_SMOOTH));
                   botones[coori1][coorj1].setIcon(icono_escalado1);
                    botones[coori2][coorj2].setIcon(icono_escalado1);
             }
             
            
        }
        
                
                }
            }  
        }









Añadiendo un if y dos contadores podemos hacer que nuestro juego muestre el número de intentos que tuvimos que hacer para completar el juegos, entonces el código quedaría de la siguiente manera:


public class JPares2 extends javax.swing.JFrame {

    Integer[][] matriz= new Integer[6][6];
     JLabel[][] botones=new JLabel[6][6];
    /**
     * Creates new form JPares2
     */
    public JPares2() {
        initComponents();
         this.setBounds(0,0, 600,620);
        
        for (int i=0; i<6; i++)
            for (int j=0; j<6; j++)
        {
            botones[i][j] = new JLabel();
        botones[i][j].setBounds(j*100,i*100, 80,80);
        this.add(botones[i][j]);
        botones[i][j].setVisible(true);
        botones[i][j].setText(i+1+"");
        botones[i][j].addMouseListener(list);
        ImageIcon icono=new ImageIcon(getClass().getResource("..\\paqImagenes\\revés.jpg"));
        Image imagen1=icono.getImage();
        ImageIcon icono_escalado=new ImageIcon(imagen1.getScaledInstance(80, 80, Image.SCALE_SMOOTH));
        botones[i][j].setIcon(icono_escalado);
        
        }
        
        for (int i=0; i<6; i++)
            for (int j=0; j<6; j++)
            {
              matriz[i][j]=0;
       }
        
        Random r=new Random();
        for (int i=0; i<6; i++)
            for (int j=0; j<6; j++)
            {
                int aux=r.nextInt(36)+1;
                boolean repetido=true;
                while(repetido==true)
                {
                    repetido=false;
                    for (int x=0; x<6; x++)
                        for (int y=0; y<6; y++)
                        {
                            if(aux==matriz[x][y])
                            {
                                repetido=true;
                                aux=r.nextInt(36)+1;
                            }
                        }
            }
                matriz[i][j]=aux;
    }
        for (int i=0; i<6; i++)
            for (int j=0; j<6; j++)
                if(matriz[i][j]>18)
                    matriz[i][j]=matriz[i][j]-18;
        for (int i=0; i<6; i++)
            for (int j=0; j<6; j++)
            {
               ImageIcon icono=new ImageIcon(getClass().getResource("..\\paqImagenes\\revés.jpg"));
        Image imagen1=icono.getImage();
        ImageIcon icono_escalado=new ImageIcon(imagen1.getScaledInstance(80, 80, Image.SCALE_SMOOTH));
        botones[i][j].setIcon(icono_escalado);  
            }
    }    

    Integer coori1=0,coorj1=0,coori2=0,coorj2=0,click=0;
    Integer Contador=0,Contador2=0;
    
    MouseListener list=new MouseListener(){

        @Override
        public void mouseClicked(MouseEvent e) {
          for (int i=0; i<6; i++)
            for (int j=0; j<6; j++)
            {
                if(e.getComponent().equals(botones[i][j]))
                {
                 ImageIcon icono=new ImageIcon(getClass().getResource("..\\paqImagenes\\"+matriz[i][j]+".jpg"));
        Image imagen1=icono.getImage();
        ImageIcon icono_escalado=new ImageIcon(imagen1.getScaledInstance(80, 80, Image.SCALE_SMOOTH));
        botones[i][j].setIcon(icono_escalado);    
           
        if(click==0)
        {
          click=1; //cambio el valor para que el pc entienda para que la proxima vez ya no es la primera  
          coori1=i;
          coorj1=j;
        }
        
        else
        {
          click=0;  //cambio el valor para que entienda que vuelve a ser la primera  
          coori2=i;
          coorj2=j;
          
           Contador=Contador+1;
             
           if(matriz[coori1][coorj1]!=matriz[coori2][coorj2])
             {
                 JOptionPane.showMessageDialog(null,"intente de nuevo");
                 ImageIcon icono1=new ImageIcon(getClass().getResource("..\\paqImagenes\\revés.jpg"));
                 Image imagen2=icono1.getImage();
                 ImageIcon icono_escalado1=new ImageIcon(imagen2.getScaledInstance(80, 80, Image.SCALE_SMOOTH));
                   botones[coori1][coorj1].setIcon(icono_escalado1);
                    botones[coori2][coorj2].setIcon(icono_escalado1);
                   
             }
             
           else
              Contador2=Contador2+1;
               
                       
        }
        
        if (Contador2==18)
        {
            JOptionPane.showMessageDialog(null,"Ud obtuvo los siguientes intentos"+Contador +", Felicitaciones"); 
        }
        
                }
                
            } 
           
        }




Ejercicios con filas y columnas de las matrices

Teniendo en cuenta que ya habíamos creado previamente una matriz de 6x6 los siguientes ejercicios consisten en llenar esas matrices con números. 

CON NÚMEROS DE 1 A 36

for(int i=0;i<6;i++) //DE UNO A 36
            for(int j=0;j<6;j++)
            {
              botones[i][j].setText(matriz[i][j]+"");  
            }
        Integer n=1;
        for(int i=0;i<6;i++)
            for(int j=0;j<6;j++)
            {
              matriz[i][j]=n;
              n++;
            }
        for(int i=0;i<6;i++)
            for(int j=0;j<6;j++)
            {
              botones[i][j].setText(matriz[i][j]+"");  
            } 




CON NÚMEROS DE 36 A 1


for(int i=0;i<6;i++) //DE 36 A UNO
            for(int j=0;j<6;j++)
            {
              botones[i][j].setText(matriz[i][j]+"");  
            }
        Integer n=36;
        for(int i=0;i<6;i++)
            for(int j=0;j<6;j++)
            {
              matriz[i][j]=n;
              n--;
            }
        for(int i=0;i<6;i++)
            for(int j=0;j<6;j++)
            {
              botones[i][j].setText(matriz[i][j]+"");  
            } 




CON NÚMEROS DE 2 A 72 

for(int i=0;i<6;i++) //DE 2 A 72
            for(int j=0;j<6;j++)
            {
              botones[i][j].setText(matriz[i][j]+"");  
            }
           Integer n=2;
        for(int i=0;i<6;i++)
            for(int j=0;j<6;j++)
            {
              matriz[i][j]=n;
            
              n++;
              n++; 
              
            }
        for(int i=0;i<6;i++)
            for(int j=0;j<6;j++)
            {
              botones[i][j].setText(matriz[j][i]+"");  
            } 
    




CON NÚMEROS EN LAS COLUMNAS PARES 1 Y EN LA COLUMNAS IMPARES 0

for(int i=0;i<6;i++) //columnas pares 1 y las impares con 0
            for(int j=0;j<6;j++)
            {
              botones[i][j].setText(matriz[i][j]+"");  
            }
        Integer n=1;
        for(int i=0;i<6;i++)
            for(int j=0;j<6;j++)
            {
              if(n==1)
             n=0;
                     
              else
               n=1;   
            matriz[j][i]=n;
        
            }
        for(int i=0;i<6;i++)
            for(int j=0;j<6;j++)
            {
              botones[i][j].setText(matriz[j][i]+"");  
            } 




CON NÚMEROS EN LAS COLUMNAS PARES 0 Y EN LA COLUMNAS IMPARES 1

for(int i=0;i<6;i++) //columnas pares 0 y las impares con 1
            for(int j=0;j<6;j++)
            {
              botones[i][j].setText(matriz[i][j]+"");  
            }
        Integer n=0;
        for(int i=0;i<6;i++)
            for(int j=0;j<6;j++)
            {
              if(n==1)
             n=0;
                     
              else
               n=1;   
            matriz[i][j]=n;
        
            }
        for(int i=0;i<6;i++)
            for(int j=0;j<6;j++)
            {
              botones[i][j].setText(matriz[i][j]+"");  
            }