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

public class origami extends Applet implements Runnable{

   Thread th = null;
   Image os;
   Graphics og;
   int fb = 0;
   int fd[][] = {{0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,5,3,3,5,0,0,0},
                 {0,0,0,1,0,0,1,0,0,0},
                 {0,0,0,1,0,0,6,0,0,0},
                 {0,0,0,5,3,4,5,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0}};
   int bd[][] = {{0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,6,6,4,4,0,0,0},
                 {0,0,0,2,0,0,2,0,0,0},
                 {0,0,0,4,0,0,2,0,0,0},
                 {0,0,0,3,2,1,6,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0}};
   int td[][][] = new int[2][10][10];
   int sw[] = new int[7];
   int ssw = 0,px,py,cr = 0,sn;
   int mx,my,cs,cn;
   Button rep,tur;
   Label sp;

   public void init(){
      os = createImage(400,440);
      og = os.getGraphics();
      setBackground(Color.white);
      og.setFont(new Font("",Font.PLAIN,38));
      R_set();

      setLayout(new BorderLayout());
      Panel pan = new Panel();
      pan.setLayout(new FlowLayout());
      pan.add(rep = new Button("  replay  "));
      pan.add(sp = new Label("       "));
      pan.add(tur = new Button("   turn   "));
      add("South",pan);

      addMouseListener(
         new MouseAdapter(){
            public void mousePressed(MouseEvent e){
               px = (e.getX()-10)/20;
               py = (e.getY()-10)/20;
               if(ssw == 1){
                  if(cr == 0){
                     if(e.getY() < sn*40){
                        for(int tx = 0;tx < 10;tx ++){
                           for(int ty = 0;ty < sn;ty ++){
                              if(td[fb][ty][tx] != 0){
                                 if(td[fb][2*sn-ty-1][tx] < 10){
                                    if(td[fb][2*sn-ty-1][tx] != 0){
                                       td[fb][2*sn-ty-1][tx] = td[(fb+1)%2][ty][9-tx];
                                    }else{
                                       td[fb][2*sn-ty-1][tx] = td[(fb+1)%2][ty][9-tx];
                                       td[(fb+1)%2][2*sn-ty-1][9-tx] = td[fb][ty][tx];
                                    }
                                    td[fb][ty][tx] = 0;
                                    td[(fb+1)%2][ty][9-tx] = 0;
                                 }else{
                                    ssw = 0;
                                 }
                              }
                           }
                        }
                     }else if(e.getY() > sn*40){
                        for(int tx = 0;tx < 10;tx ++){
                           for(int ty = 9;ty >= sn;ty --){
                              if(td[fb][ty][tx] != 0){
                                 if(td[fb][2*sn-ty-1][tx] >= 0){
                                    if(td[fb][2*sn-ty-1][tx] != 0){
                                       td[fb][2*sn-ty-1][tx] = td[(fb+1)%2][ty][9-tx];
                                    }else{
                                       td[fb][2*sn-ty-1][tx] = td[(fb+1)%2][ty][9-tx];
                                       td[(fb+1)%2][2*sn-ty-1][9-tx] = td[fb][ty][tx];
                                    }
                                    td[fb][ty][tx] = 0;
                                    td[(fb+1)%2][ty][9-tx] = 0;
                                 }else{
                                    ssw = 0;
                                 }
                              }
                           }
                        }
                     }
                  }else{
                     if(e.getX() < sn*40){
                        for(int ty = 0;ty < 10;ty ++){
                           for(int tx = 0;tx < sn;tx ++){
                              if(td[fb][ty][tx] != 0){
                                 if(td[fb][ty][2*sn-tx-1] < 10){
                                    if(td[fb][ty][2*sn-tx-1] != 0){
                                       td[fb][ty][2*sn-tx-1] = td[(fb+1)%2][ty][9-tx];
                                    }else{
                                       td[fb][ty][2*sn-tx-1] = td[(fb+1)%2][ty][9-tx];
                                       td[(fb+1)%2][ty][10-2*sn+tx] = td[fb][ty][tx];
                                    }
                                    td[fb][ty][tx] = 0;
                                    td[(fb+1)%2][ty][9-tx] = 0;
                                 }else{
                                    ssw = 0;
                                 }
                              }
                           }
                        }
                     }else if(e.getX() > sn*40){
                        for(int ty = 0;ty < 10;ty ++){
                           for(int tx = 9;tx >= sn;tx --){
                              if(td[fb][ty][tx] != 0){
                                 if(td[fb][ty][2*sn-tx-1] >= 0){
                                    if(td[fb][ty][2*sn-tx-1] != 0){
                                       td[fb][ty][2*sn-tx-1] = td[(fb+1)%2][ty][9-tx];
                                    }else{
                                       td[fb][ty][2*sn-tx-1] = td[(fb+1)%2][ty][9-tx];
                                       td[(fb+1)%2][ty][10-2*sn+tx] = td[fb][ty][tx];
                                    }
                                    td[fb][ty][tx] = 0;
                                    td[(fb+1)%2][ty][9-tx] = 0;
                                 }else{
                                    ssw = 0;
                                 }
                              }
                           }
                        }
                     }
                  }
                  ssw = 0;
               }else{
                  if((px%2==0)&&(py%2==1)){
                     cr = 0;
                     sn = (py+1)/2;
                     ssw = 1;
                  }else if((px%2==1)&&(py%2==0)){
                     cr = 1;
                     sn = (px+1)/2;
                     ssw = 1;
                  }
               }

               repaint();
            }
         }
      );

      tur.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
            fb = (fb+1)%2;
            ssw = 0;
            repaint();
         } 
      });
      rep.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
            R_set();
            repaint();
         } 
      });
   }

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

      for(int k1 = 0;k1 < 10;k1 ++){
         for(int k2 = 0;k2 < 10;k2 ++){
            if(td[fb][k2][k1] == 0){
               og.setColor(Color.lightGray);
               og.drawRect(k1*40,k2*40,40,40);
            }else{
               og.setColor(new Color(128,255,128));
               og.fill3DRect(k1*40,k2*40,40,40,true);
               og.setColor(Color.black);
               switch(td[fb][k2][k1]){
                  case 1 :og.drawString("œ",k1*40+2,k2*40+35);break;
                  case 2 :og.drawString("~",k1*40+2,k2*40+35);break;
                  case 3 :og.drawString(" ",k1*40+2,k2*40+35);break;
                  case 4 :og.drawString("‡",k1*40+2,k2*40+35);break;
                  case 5 :og.drawString("›",k1*40+2,k2*40+35);break;
                  case 6 :og.drawString("¡",k1*40+2,k2*40+35);break;
               }
            }
         }
      }

      if(ssw == 1){
         og.setColor(Color.red);
         if(cr == 0){
            og.drawLine(0,sn*40,400,sn*40);
         }else{
            og.drawLine(sn*40,0,sn*40,400);
         }
      }
      check();

      if(sw[1] == 0){
         og.setColor(Color.red);
      }else{
         og.setColor(Color.blue);
      }
      og.drawString("œ",80,435);
      if(sw[2] == 0){
         og.setColor(Color.red);
      }else{
         og.setColor(Color.blue);
      }
      og.drawString("~",120,435);
      if(sw[3] == 0){
         og.setColor(Color.red);
      }else{
         og.setColor(Color.blue);
      }
      og.drawString(" ",160,435);
      if(sw[4] == 0){
         og.setColor(Color.red);
      }else{
         og.setColor(Color.blue);
      }
      og.drawString("‡",200,435);
      if(sw[5] == 0){
         og.setColor(Color.red);
      }else{
         og.setColor(Color.blue);
      }
      og.drawString("›",240,435);
      if(sw[6] == 0){
         og.setColor(Color.red);
      }else{
         og.setColor(Color.blue);
      }
      og.drawString("¡",280,435);

      g.drawImage(os,0,0,this);
   }

   public void check(){
      mx = 10;my = 10;cs = 0;
      for(int k1 = 0;k1 < 10;k1 ++){
         for(int k2 = 0;k2 < 10;k2 ++){
            if(td[fb][k2][k1] != 0){
               if(mx >= k1)mx = k1;
               if(my >= k2)my = k2;
               cs ++;
            }
         }
      }
      if(cs == 4){
         cn = td[fb][my][mx];
         if((td[fb][my][mx+1]==cn)&&(td[fb][my+1][mx]==cn)&&(td[fb][my+1][mx+1]==cn)){
            sw[cn] = 1;
         }
      }
   }

   public void R_set(){
      for(int k1 = 0;k1 < 10;k1 ++){
         for(int k2 = 0;k2 < 10;k2 ++){
            td[0][k2][k1] = fd[k2][k1];
            td[1][k2][k1] = bd[k2][k1];
         }
      }
   }

   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 ) ;
   }
}