import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class c_panel extends Applet implements Runnable{

   int cd[][][] = {{{1,2,0},{0,0,3},{0,4,0}},
                   {{0,2,0},{0,0,4},{3,0,1}},
                   {{1,0,0},{0,2,0},{0,3,4}},
                   {{0,2,0},{0,4,0},{3,0,1}}};
   int xt[] = {30,30,270,270};
   int yt[] = {30,150,30,150};
   int csw[] = {0,0,0,0};
   int sn,fg,cf;
   int px,py,pn,en;
   int upn[] = {4,4,4,4};
   int rep[][] = new int[3][3];
   int che[][] = new int[3][3];
   Thread th = null;
   Image os;
   Graphics og;

   public void init(){
      os = createImage(390,270);
      og = os.getGraphics();
      setBackground(Color.white);
      addMouseListener(
         new MouseAdapter(){
            public void mousePressed(MouseEvent e){
               px = e.getX();
               py = e.getY();
               if((px > 30) && (px < 120)){
                  if((py > 30) && (py < 120)){
                     pn = 0;
                  }else if((py > 150) && (py < 240)){
                     pn = 1;
                  }else{
                     pn = 5;
                  }
               }else if((px > 150) && (px < 240)){
                  if((py > 90) && (py < 180)){
                     en = pn ; pn = 4;
                  }else{
                     pn = 5;
                  }
               }else if((px > 270) && (px < 360)){
                  if((py > 30) && (py < 120)){
                     pn = 2;
                  }else if((py > 150) && (py < 240)){
                     pn = 3;
                  }else{
                  pn = 5;
                  }
               }
               if(fg == 0){
                  if(pn == 4){
                     if(upn[3] != 4){
                        csw[upn[3]] = 0; upn[3] = 4; fg = 0;
                     }else if(upn[2] != 4){
                        csw[upn[2]] = 0; upn[2] = 4; fg = 0;
                     }else if(upn[1] != 4){
                        csw[upn[1]] = 0; upn[1] = 4; fg = 0;
                     }else if(upn[0] != 4){
                        csw[upn[0]] = 0; upn[0] = 4; fg = 0;
                     }else{
                        fg = 0;
                     }
                  }else{
                     sn = pn; fg = 1;
                  }
               }else if(fg == 1){
                  if(pn == 5){
                     fg = 0;
                  }else if(pn == 4){
                     if((upn[0] == 4) && (csw[en] == 0)){
                        upn[0] = en; csw[en] = 1; fg = 0;
                     }else if((upn[1] == 4) && (csw[en] == 0)){
                        upn[1] = en; csw[en] = 1; fg = 0;
                     }else if((upn[2] == 4) && (csw[en] == 0)){
                        upn[2] = en; csw[en] = 1; fg = 0;
                     }else if((upn[3] == 4) && (csw[en] == 0)){
                        upn[3] = en; csw[en] = 1; fg = 0;
                     }else{
                        fg = 0;
                     }
                  }else if(sn != pn){
                     sn = pn;
                  }else if((sn == pn) && (csw[pn] == 0)){
                     for(int k1 = 0;k1 < 3;k1 ++){
                        for(int k2 = 0;k2 < 3;k2 ++){
                           rep[k2][k1] = cd[sn][k1][2-k2];
                        }
                     }
                     for(int k1 = 0;k1 < 3;k1 ++){
                        for(int k2 = 0;k2 < 3;k2 ++){
                           cd[sn][k2][k1] = rep[k2][k1];
                        }
                     }
                  }
               }
               repaint();
            }
         }
      );    
   }

   public void paint(Graphics g){
      og.setColor(Color.white); 
      og.fillRect(0,0,390,270);

      if((fg == 1) && (sn == 4)){
         og.setColor(Color.darkGray);
      }else{
         og.setColor(Color.lightGray);
      }
      og.fill3DRect(145,85,100,100,true);
      og.setColor(Color.white);
      og.fillRect(150,90,90,90);
      
      for(int i1 = 0;i1 < 4;i1 ++){
         if((fg == 1) && (sn == i1)){
            og.setColor(Color.darkGray);
         }else{
            og.setColor(Color.lightGray);
         }
         og.fill3DRect(xt[i1]-5,yt[i1]-5,100,100,true);
         og.setColor(Color.white);
         og.fillRect(xt[i1],yt[i1],90,90);
         if(csw[i1] == 0){
            for(int i2 = 0;i2 < 3;i2 ++){
               for(int i3 = 0;i3 < 3;i3 ++){
                  switch(cd[i1][i3][i2]){
                     case 1 : og.setColor(Color.red);break;
                     case 2 : og.setColor(Color.green);break;
                     case 3 : og.setColor(Color.yellow);break;
                     case 4 : og.setColor(Color.blue);break;
                     default : break;
                  }
                  if(cd[i1][i3][i2] != 0){
                     og.fill3DRect(30*i2+xt[i1],30*i3+yt[i1],30,30,true);
                  }
               }
            }
         }
      }
      for(int c = 0;c < 4;c ++){
         if(upn[c] != 4){
            for(int k1 = 0;k1 < 3;k1 ++){
               for(int k2 = 0;k2 < 3;k2 ++){
                  if(cd[upn[c]][k2][k1] != 0){
                     switch(cd[upn[c]][k2][k1]){
                        case 1 : og.setColor(Color.red);break;
                        case 2 : og.setColor(Color.green);break;
                        case 3 : og.setColor(Color.yellow);break;
                        case 4 : og.setColor(Color.blue);break;
                        default : break;
                     }
                     og.fill3DRect(30*k1+150,30*k2+90,30,30,true);
                  }
               }
            }
         }
      }
      for(int c1 = 0;c1 < 3;c1 ++){
         for(int c2 = 0;c2 < 3;c2 ++){
            for(int c3 = 0;c3 < 4;c3 ++){
               if((upn[3] != 4) && (cd[upn[3]][c2][c1] != 0)){
                  che[c2][c1] = cd[upn[3]][c2][c1];
               }else if((upn[2] != 4) && (cd[upn[2]][c2][c1] != 0)){
                  che[c2][c1] = cd[upn[2]][c2][c1];
               }else if((upn[1] != 4) && (cd[upn[1]][c2][c1] != 0)){
                  che[c2][c1] = cd[upn[1]][c2][c1];
               }else if((upn[0] != 4) && (cd[upn[0]][c2][c1] != 0)){
                  che[c2][c1] = cd[upn[0]][c2][c1];
               }else{
                  che[c2][c1] = 0;
               }
            }
         }
      }
      cf = 1;
      for(int c1 = 0;c1 < 3;c1 ++){
         for(int c2 = 0;c2 < 3;c2 ++){
            cf = cf * che[c2][c1];
         }
      }
      if(cf != 0){
         if((che[1][1] != che[1][0]) && (che[1][1] != che[2][1]) && (che[1][1] != che[1][2]) && (che[1][1] != che[0][1])){
            if((che[0][0] != che[0][1]) && (che[0][0] != che[1][0])){
               if((che[0][2] != che[0][1]) && (che[0][2] != che[1][2])){
                  if((che[2][2] != che[2][1]) && (che[2][2] != che[1][2])){
                     if((che[2][0] != che[1][0]) && (che[2][0] != che[2][1])){
                        og.setColor(Color.red);
                        og.drawRect(144,84,102,102);
                        og.drawRect(142,82,106,106);
                     }
                  }
               }
            }
         }
      }
      g.drawImage(os,0,0,this);
   }

   public void start(){    
      if(th == null){
         th = new Thread(this);
         th.start();
      }
   }

   public void stop(){
      th = null;
   }

   public void run(){
      while(th != null){
         try{
            Thread.sleep(100);
         }
         catch (InterruptedException e){
         }
      }
   }

   public void update( Graphics g ){
      paint( g ) ;
   }
}