import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Megame extends JFrame  {
    
    
    class Outr implements ActionListener,Runnable{
    
    
    public void actionPerformed(ActionEvent e) {
            if(e.getSource()==b1)
            { 
                Thread t1= new Thread(new Outr(),"t1");
               
            b1.setBackground(Color.red);
             t1.start();
            }
                  if(e.getSource()==b2)
            { 
                Thread t2= new Thread(new Outr(),"t2");
               
            b2.setBackground(Color.green);
             t2.start();
            }
                        if(e.getSource()==b3)
            { 
                Thread t3= new Thread(new Outr(),"t3");
               
            b3.setBackground(Color.blue);
             t3.start();
            }
                              if(e.getSource()==b4)
            { 
                Thread t4= new Thread(new Outr(),"t4");
               
            b4.setBackground(Color.PINK);
             t4.start();
            }
                                    if(e.getSource()==b5)
            { 
                Thread t5= new Thread(new Outr(),"t5");
               
            b5.setBackground(Color.PINK);
             t5.start();
            }
                                          if(e.getSource()==b6)
            { 
                Thread t6= new Thread(new Outr(),"t6");
               
            b6.setBackground(Color.blue);
             t6.start();
            }
                                                if(e.getSource()==b7)
            { 
                Thread t7= new Thread(new Outr(),"t7");
               
            b7.setBackground(Color.green);
             t7.start();
            }
                                                     
          if(e.getSource()==b8)
                                                    
            { 
                Thread t8= new Thread(new Outr(),"t8");
               
            b8.setBackground(Color.red);
             t8.start();
            }
                                                      
          if(e.getSource()==b9)
                                                    
            { 
                Thread t9= new Thread(new Outr(),"t9");
               
            b9.setBackground(Color.cyan);
             t9.start();
            }
                  
                                                       
          if(e.getSource()==b10)
                                                    
            { 
                Thread t10= new Thread(new Outr(),"t10");
               
            b10.setBackground(Color.MAGENTA);
             t10.start();
            }
          
                                                      
          if(e.getSource()==b11)
                                                    
            { 
                Thread t11= new Thread(new Outr(),"t11");
               
            b11.setBackground(Color.cyan);
             t11.start();
            }
                                                      
          if(e.getSource()==b12)
                                                    
            { 
                Thread t12= new Thread(new Outr(),"t12");
               
            b12.setBackground(Color.MAGENTA);
             t12.start();
            }
                                                      
          if(e.getSource()==b13)
                                                    
            { 
                Thread t13= new Thread(new Outr(),"t13");
               
            b13.setBackground(Color.YELLOW);
             t13.start();
            }
                                                      
          if(e.getSource()==b14)
                                                    
            { 
                Thread t14= new Thread(new Outr(),"t14");
               
            b14.setBackground(Color.BLACK);
             t14.start();
            }
                                                      
          if(e.getSource()==b15)
                                                    
            { 
                Thread t15= new Thread(new Outr(),"t15");
               
            b15.setBackground(Color.YELLOW);
             t15.start();
            }
                                                      
          if(e.getSource()==b16)
                                                    
            { 
                Thread t16= new Thread(new Outr(),"t16");
               
            b16.setBackground(Color.BLACK);
             t16.start();
            }
          
    
        }
    
    
     
         public void run() {
             count++;
          try{
           
             if((Thread.currentThread().getName()).equals("t1"))
                 
             {
                 
                 if(count%2==0)  
                 { Thread.sleep(200);
                     if(A2==1)
                            {
                            b1.setBackground(Color.red);
                           
                            b8.setBackground(Color.red);
                            }
              
                     else
                            {b1.setBackground(Color.white);
  A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
                            b8.setBackground(Color.white);
              }
              
              }
              else{
                     Thread.sleep(200);
                     b1.setBackground(Color.white);
             
              A1=1;
              }
              
              
                 
                
       }
             
            if((Thread.currentThread().getName()).equals("t2"))
             {if(count%2==0)  
              {Thread.sleep(200);
              if(C2==1)
              {b2.setBackground(Color.green);
                   b7.setBackground(Color.green);
              }
              
              else{b2.setBackground(Color.white);
              A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
                   b7.setBackground(Color.white);
              }
              
              }
              else{
                 Thread.sleep(200);
              b2.setBackground(Color.white);
              C1=1;
              }
           
       }  
               if((Thread.currentThread().getName()).equals("t3"))
             {if(count%2==0)  
              { Thread.sleep(200);
              if(D2==1)
              {
                 
                  b3.setBackground(Color.BLUE);
                   b6.setBackground(Color.BLUE);
              }
              
              else{b3.setBackground(Color.white);
              A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
                   b6.setBackground(Color.white);
              }
              
              }
              else{
                     Thread.sleep(200);
                     b3.setBackground(Color.white);
              
              D1=1;
              }
              }
             
            if((Thread.currentThread().getName()).equals("t4"))
             { if(count%2==0)  
                 { Thread.sleep(200);
                     if(E2==1)
                            {
                            b4.setBackground(Color.pink);
                            b5.setBackground(Color.pink);
                            }
              
                     else
                            {b4.setBackground(Color.white);
                            A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
                            b5.setBackground(Color.white);
              }
              
              }
              else{
                     Thread.sleep(200);
                     b4.setBackground(Color.white);
             
              E1=1;
              }
              }
            
              if((Thread.currentThread().getName()).equals("t5"))
             {if(count%2==0)  
              {Thread.sleep(200);
              if(E1==1)
              {b4.setBackground(Color.pink);
                   b5.setBackground(Color.pink);
              }
              
              else{b4.setBackground(Color.white);
              A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
                   b5.setBackground(Color.white);
              }
              
              }
              else{
                 Thread.sleep(200);
              b5.setBackground(Color.white);
              E2=1;
              }}
                if((Thread.currentThread().getName()).equals("t6"))
             {if(count%2==0)  
              {Thread.sleep(200);
              if(D1==1)
              {b3.setBackground(Color.BLUE);
                   b6.setBackground(Color.BLUE);
              }
              
              else{b3.setBackground(Color.white);
              A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
                   b6.setBackground(Color.white);
              }
              
              }
              else{
                 Thread.sleep(200);
              b6.setBackground(Color.white);
              D2=1;}
             }
               if((Thread.currentThread().getName()).equals("t7"))
             {if(count%2==0)  
              {Thread.sleep(200);
              if(C1==1)
              {b2.setBackground(Color.green);
                   b7.setBackground(Color.green);
              }
              
              else{b2.setBackground(Color.white);
              A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
              
                   b7.setBackground(Color.white);
              }
              
              }
              else{
                 Thread.sleep(200);
              b7.setBackground(Color.white);
              C2=1;
              }
           
       }
             if((Thread.currentThread().getName()).equals("t8"))
             { if(count%2==0)  
              {Thread.sleep(200);
              if(A1==1)
              {b1.setBackground(Color.red);
                   b8.setBackground(Color.red);
              }
              
              else{b1.setBackground(Color.white);
             A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
                   b8.setBackground(Color.white);
              }
              
              }
              else{
                 Thread.sleep(200);
              b8.setBackground(Color.white);
              A2=1;
              }
           
       }
             
               if((Thread.currentThread().getName()).equals("t10"))
                 
             {
                 
                 if(count%2==0)  
                 { Thread.sleep(200);
                     if(F2==1)
                            {
                            b10.setBackground(Color.MAGENTA);
                            b12.setBackground(Color.MAGENTA);
                            }
              
                     else
                            {b10.setBackground(Color.white);
                            A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
                            b12.setBackground(Color.white);
              }
              
              }
              else{
                     Thread.sleep(200);
                     b10.setBackground(Color.white);
             
              F1=1;
              }
                             
       }
          if((Thread.currentThread().getName()).equals("t12"))
             { if(count%2==0)  
              {Thread.sleep(200);
              if(F1==1)
              {b10.setBackground(Color.MAGENTA);
                   b12.setBackground(Color.MAGENTA);
              }
              
              else{b10.setBackground(Color.white);
              A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
                   b12.setBackground(Color.white);
              }
              
              }
              else{
                 Thread.sleep(200);
              b12.setBackground(Color.white);
              F2=1;
              }
           
       }
           if((Thread.currentThread().getName()).equals("t9"))
                 
             {
                 
                 if(count%2==0)  
                 { Thread.sleep(200);
                     if(G2==1)
                            {
                            b9.setBackground(Color.CYAN);
                            b11.setBackground(Color.CYAN);
                            }
              
                     else
                            {b9.setBackground(Color.white);
                            A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
                            b11.setBackground(Color.white);
              }
              
              }
              else{
                     Thread.sleep(200);
                     b9.setBackground(Color.white);
             
              G1=1;
              }
                             
       }
        
        if((Thread.currentThread().getName()).equals("t11"))
             { if(count%2==0)  
              {Thread.sleep(200);
              if(G1==1)
              {b9.setBackground(Color.CYAN);
                   b11.setBackground(Color.CYAN);
              }
              
              else{b9.setBackground(Color.white);
              A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
                   b11.setBackground(Color.white);
              }
              
              }
              else{
                 Thread.sleep(200);
              b11.setBackground(Color.white);
              G2=1;
              }
           
       }
        if((Thread.currentThread().getName()).equals("t13"))
                 
             {Thread.sleep(200);
                 
                 if(count%2==0)  
                 { 
                     if(H2==1)
                            {
                            b13.setBackground(Color.YELLOW);
                            b15.setBackground(Color.YELLOW);
                            }
              
                     else
                            {b13.setBackground(Color.white);
                            A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
                            b15.setBackground(Color.white);
              }
              
              }
              else{
                     Thread.sleep(200);
                     b13.setBackground(Color.white);
             
              H1=1;
              }
                             
       }
        if((Thread.currentThread().getName()).equals("t15"))
             { if(count%2==0)  
              {Thread.sleep(200);
              if(H1==1)
              {b13.setBackground(Color.YELLOW);
                   b15.setBackground(Color.YELLOW);
              }
              
              else{b13.setBackground(Color.white);
              A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
                   b15.setBackground(Color.white);
              }
              
              }
              else{
                 Thread.sleep(200);
              b15.setBackground(Color.white);
              H2=1;
              }
           
       }
        
        
        if((Thread.currentThread().getName()).equals("t14"))
                 
             {
                 
                 if(count%2==0)  
                 { Thread.sleep(200);
                     if(I2==1)
                            {
                            b14.setBackground(Color.BLACK);
                            b16.setBackground(Color.BLACK);
                            }
              
                     else
                            {b14.setBackground(Color.white);
                            A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
                            b16.setBackground(Color.white);
              }
              
              }
              else{
                     Thread.sleep(200);
                     b14.setBackground(Color.white);
             
              I1=1;
              }
                             
       }
        
        
        if((Thread.currentThread().getName()).equals("t16"))
             { if(count%2==0)  
              {Thread.sleep(200);
              if(I1==1)
              {b14.setBackground(Color.BLACK);
                   b16.setBackground(Color.BLACK);
              }
              
              else{b14.setBackground(Color.white);
              A1=0;A2=0;C1=0;C2=0;D1=0;D2=0;E1=0;E2=0;F1=0;F2=0;G1=0;G2=0;H1=0;H2=0;I1=0;
    I2=0;
                   b16.setBackground(Color.white);
              }
              
              }
              else{
                 Thread.sleep(200);
              b16.setBackground(Color.white);
              I2=1;
              }
           
       }
        
          }    
        catch(Exception e)
        {}
    
 
if( (b1.getBackground()==Color.red)&&(b8.getBackground()==Color.red)&&(b2.getBackground()==Color.green) 
        &&(b7.getBackground()==Color.green)&&(b3.getBackground()==Color.blue)&&(b6.getBackground()==Color.blue  )
        &&(b4.getBackground()==Color.pink)&&(b5.getBackground()==Color.pink)&&(b9.getBackground()==Color.cyan)&&(b11.getBackground()==Color.cyan)
        &&(b10.getBackground()==Color.MAGENTA)&&(b12.getBackground()==Color.MAGENTA)&&(b13.getBackground()==Color.yellow)&&(b15.getBackground()==Color.yellow)
        &&(b14.getBackground()==Color.black)&&(b16.getBackground()==Color.black))
  {
     JOptionPane j= new JOptionPane();
     j.showMessageDialog(rootPane, "YOU WON");
  
  }   
        
    
}
    
    
}
    Megame()
    { b1= new JButton();
    b1.setBackground(Color.white);
       b1.setBounds(50, 50, 50, 50);
    add(b1);
    Outr tim = new Outr();
    b1.addActionListener(tim);
    b2= new JButton();
    b2.setBackground(Color.white);
       b2.setBounds(100, 50, 50, 50);
    add(b2);
      b2.addActionListener(tim);
    b3= new JButton();
    b3.setBackground(Color.white);
       b3.setBounds(150, 50, 50, 50);
    add(b3);
      b3.addActionListener(tim);
     b4= new JButton();
 b4.setBackground(Color.white);
     
     b4.setBounds(50, 100, 50, 50);
    add(b4);
      b4.addActionListener(tim);
     b5= new JButton();
     b5.setBackground(Color.white);
       b5.setBounds(100, 100, 50, 50);
    add(b5);
     b5.addActionListener(tim);
    b6= new JButton();
    b6.setBackground(Color.white);
       b6.setBounds(150, 100, 50, 50);
    add(b6);
      b6.addActionListener(tim);
    b7= new JButton();
    b7.setBackground(Color.white);
       b7.setBounds(50, 150, 50, 50);
    add(b7);
      b7.addActionListener(tim);
     b8= new JButton();
     b8.setBackground(Color.white);
       b8.setBounds(100, 150, 50, 50);
    add(b8);
      b8.addActionListener(tim);
     b9= new JButton();
       b9.setBounds(150, 150, 50, 50);
    add(b9);
     b9.setBackground(Color.white);
     b9.addActionListener(tim);
      b10= new JButton();
       b10.setBounds(200, 50, 50, 50);
    add(b10);
     b10.setBackground(Color.white);
     b10.addActionListener(tim);
      b11= new JButton();
       b11.setBounds(200, 100, 50, 50);
    add(b11);
     b11.setBackground(Color.white);
     b11.addActionListener(tim);
      b12= new JButton();
       b12.setBounds(200, 150, 50, 50);
    add(b12);
     b12.setBackground(Color.white);
     b12.addActionListener(tim);
      b13= new JButton();
       b13.setBounds(50, 200, 50, 50);
    add(b13);
     b13.setBackground(Color.white);
     b13.addActionListener(tim);
      b14= new JButton();
       b14.setBounds(100, 200, 50, 50);
    add(b14);
     b14.setBackground(Color.white);
     b14.addActionListener(tim);
      b15= new JButton();
       b15.setBounds(150, 200, 50, 50);
    add(b15);
     b15.setBackground(Color.white);
     b15.addActionListener(tim);
     b16= new JButton();
       b16.setBounds(200, 200, 50, 50);
    add(b16);
     b16.setBackground(Color.white);
     b16.addActionListener(tim);
      setSize(400, 400);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(null);
    setVisible(true);
   }
    
   JButton b1;
   JButton b2;
   JButton b3;
   JButton b4;
   JButton b5;
   JButton b6;
   JButton b7;
   JButton b8;
   JButton b9;
   JButton b10;
   JButton b11;
   JButton b12;
   JButton b13;
   JButton b14;
   JButton b15;
   JButton b16;
   int A1=0,A2=0,C1=0,C2=0,D1=0,D2=0,E1=0,E2=0,F1=0,F2=0,G1=0,G2=0,H1=0,H2=0;
   int I1=0,I2=0;
  int count=0;
    
 
    
    public static void main(String args[])
            
    {
    Megame m1=new Megame();
 
    
    
    }
    
}