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

public class enclosure extends Applet implements Runnable{

   Thread th = null;
   Image os;
   Graphics og;
   int td[][] = new int[6][6] , tt[][] = new int[6][6] ;
   int ex[] = new int[3] , ey[] = new int[3] ;
   int mx,my,px,py,pd,cx,cy;
   int fg,ef;
   Button re;

   public void init(){
      os = createImage(300,310);
      og = os.getGraphics();
      setBackground(Color.white);

      d_set();
      setLayout(new BorderLayout());
      Panel pan = new Panel();
      pan.setLayout(new FlowLayout());
      pan.add(re = new Button("  reset  "));
      add("South",pan);

      addMouseListener(
         new MouseAdapter(){
            public void mousePressed(MouseEvent e){
               if((td[py][px] == 1) && (ef == 0)){
                  switch(pd){
                     case 1 : if(td[py][px + 1] == 0){
                                 td[py][px + 1] = 1 ; td[py][px] = 0 ;
                                 fg = 0;
                               }
                               break;
                     case 2 : if(td[py - 1][px] == 0){
                                 td[py - 1][px] = 1 ; td[py][px] = 0 ;
                                 fg = 0;
                               }
                               break;
                     case 3 :  if(td[py][px - 1] == 0){
                                 td[py][px - 1] = 1 ; td[py][px] = 0 ;
                                 fg = 0;
                               }
                               break;
                     case 4 : if(td[py + 1][px] == 0){
                                 td[py + 1][px] = 1 ; td[py][px] = 0 ;
                                 fg = 0;
                               }
                               break;
                  }
                  repaint();
                  if(fg == 0){
                     for(int c1 = 0;c1 < 2;c1 ++){
                        for(int r1 = 0;r1 < 4;r1 ++){
                           if(fg == 0){
                              if((ex[c1] == 1) && (ey[c1] == 2) && (td[2][2] == 0)){
                                 td[2][1] = 0 ; td[2][2] = 2 ;
                                 ex[c1] = 2 ;
                                 fg = 1 ;
                              }
                           }
                           t_turn();
                        }
                     }
                     for(int c1 = 0;c1 < 2;c1 ++){
                        for(int r1 = 0;r1 < 4;r1 ++){
                           if(fg == 0){
                              if((ex[c1] == 1) && (ey[c1] == 3) && (td[3][2] == 0)){
                                 td[3][1] = 0 ; td[3][2] = 2 ;
                                 ex[c1] = 2 ;
                                 fg = 1 ;
                              }
                           }
                           t_turn();
                        }
                     }
                     for(int c1 = 0;c1 < 2;c1 ++){
                        for(int r1 = 0;r1 < 4;r1 ++){
                           if(fg == 0){
                              if((ex[c1] == 2) && (ey[c1] == 2) && (td[2][3] == 0)){
                                 td[2][2] = 0 ; td[2][3] = 2 ;
                                 ex[c1] = 3 ;
                                 fg = 1 ;
                              }
                           }
                           t_turn();
                        }
                     }
                     for(int c1 = 0;c1 < 2;c1 ++){
                        for(int r1 = 0;r1 < 4;r1 ++){
                           if(fg == 0){
                              if((ex[c1] == 2) && (ey[c1] == 2) && (td[3][2] == 0)){
                                 td[2][2] = 0 ; td[3][2] = 2 ;
                                 ey[c1] = 3 ;
                                 fg = 1 ;
                              }
                           }
                           t_turn();
                        }
                     }
                     for(int c1 = 0;c1 < 2;c1 ++){
                        for(int r1 = 0;r1 < 4;r1 ++){
                           if(fg == 0){
                              if((ex[c1] == 1) && (ey[c1] == 2) && (td[3][1] == 0)){
                                 td[2][1] = 0 ; td[3][1] = 2 ;
                                 ey[c1] = 3 ;
                                 fg = 1 ;
                              }
                           }
                           t_turn();
                        }
                     }
                     for(int c1 = 0;c1 < 2;c1 ++){
                        for(int r1 = 0;r1 < 4;r1 ++){
                           if(fg == 0){
                              if((ex[c1] == 1) && (ey[c1] == 3) && (td[2][1] == 0)){
                                 td[3][1] = 0 ; td[2][1] = 2 ;
                                 ey[c1] = 2 ;
                                 fg = 1 ;
                              }
                           }
                           t_turn();
                        }
                     }
                     for(int c1 = 0;c1 < 2;c1 ++){
                        for(int r1 = 0;r1 < 4;r1 ++){
                           if(fg == 0){
                              if((ex[c1] == 1) && (ey[c1] == 1) && (td[2][1] == 0)){
                                 td[1][1] = 0 ; td[2][1] = 2 ;
                                 ey[c1] = 2 ;
                                 fg = 1 ;
                              }
                           }
                           t_turn();
                        }
                     }
                     for(int c1 = 0;c1 < 2;c1 ++){
                        for(int r1 = 0;r1 < 4;r1 ++){
                           if(fg == 0){
                              if((ex[c1] == 1) && (ey[c1] == 1) && (td[1][2] == 0)){
                                 td[1][1] = 0 ; td[1][2] = 2 ;
                                 ex[c1] = 2 ;
                                 fg = 1 ;
                              }
                           }
                           t_turn();
                        }
                     }
                     for(int c1 = 0;c1 < 2;c1 ++){
                        for(int r1 = 0;r1 < 4;r1 ++){
                           if(fg == 0){
                              if((ex[c1] == 1) && (ey[c1] == 2) && (td[1][1] == 0)){
                                 td[2][1] = 0 ; td[1][1] = 2 ;
                                 ey[c1] = 1 ;
                                 fg = 1 ;
                              }
                           }
                           t_turn();
                        }
                     }
                     for(int c1 = 0;c1 < 2;c1 ++){
                        for(int r1 = 0;r1 < 4;r1 ++){
                           if(fg == 0){
                              if((ex[c1] == 1) && (ey[c1] == 3) && (td[4][1] == 0)){
                                 td[3][1] = 0 ; td[4][1] = 2 ;
                                 ey[c1] = 4 ;
                                 fg = 1 ;
                              }
                           }
                           t_turn();
                        }
                     }
                     for(int c1 = 0;c1 < 2;c1 ++){
                        for(int r1 = 0;r1 < 4;r1 ++){
                           if(fg == 0){
                              if((ex[c1] == 2) && (ey[c1] == 2) && (td[2][1] == 0)){
                                 td[2][2] = 0 ; td[2][1] = 2 ;
                                 ex[c1] = 1 ;
                                 fg = 1 ;
                              }
                           }
                           t_turn();
                        }
                     }
                     for(int c1 = 0;c1 < 2;c1 ++){
                        for(int r1 = 0;r1 < 4;r1 ++){
                           if(fg == 0){
                              if((ex[c1] == 2) && (ey[c1] == 2) && (td[1][2] == 0)){
                                 td[2][2] = 0 ; td[1][2] = 2 ;
                                 ey[c1] = 1 ;
                                 fg = 1 ;
                              }
                           }
                           t_turn();
                        }
                     }
                  }
                  if(fg == 0)ef = 1;
                  repaint();
               }
            }
         }
      );

      addMouseMotionListener(
         new MouseMotionAdapter(){
            public void mouseMoved(MouseEvent e){
               mx = e.getX()       ; my = e.getY()       ;
               px = (mx + 30) / 60 ; py = (my + 30) / 60 ;
               cx = px * 60        ; cy = py * 60        ;
               if(((mx - cx) > (my - cy)) && ((mx - cx) > (cy - my))){
                  pd = 1;
               }else if(((mx - cx) > (my - cy)) && ((mx - cx) < (cy - my))){
                  pd = 2;
               }else if(((mx - cx) < (my - cy)) && ((mx - cx) < (cy - my))){
                  pd = 3;
               }else if(((mx - cx) < (my - cy)) && ((mx - cx) > (cy - my))){
                  pd = 4;
               }
               repaint();
            }
         }
      );

      re.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
            d_set();
            repaint();
         } 
      });

   }

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

      for(int d1 = 0;d1 < 6;d1 ++){
         for(int d2 = 0;d2 < 6;d2 ++){
            if(td[d2][d1] >= 0){
                for(int bc = 0;bc < 60;bc ++){
                   og.setColor(new Color(bc/2+190,bc/2+190,120));
                   og.drawLine(d1*60-30+bc,d2*60-30,d1*60-30,d2*60-30+bc);
                   og.setColor(new Color(bc/2+210,bc/2+210,120));
                   og.drawLine(d1*60+30,d2*60-30+bc,d1*60-30+bc,d2*60+30);
                }
            }
            if(td[d2][d1] == 1){
                for(int bc = 0;bc < 21;bc ++){
                   og.setColor(new Color(0,0,bc*6+130));
                   og.fillOval(d1*60-20+bc,d2*60-20+bc,40-2*bc,40-2*bc);
                }
            }else if(td[d2][d1] == 2){
                for(int bc = 0;bc < 21;bc ++){
                   og.setColor(new Color(bc*6+130,0,0));
                   og.fillOval(d1*60-20+bc,d2*60-20+bc,40-2*bc,40-2*bc);
                }
            }
         }
      }
      if(td[py][px] == 1){
         og.setColor(Color.black);
         switch(pd){
            case 1 : if(td[py][px + 1] == 0){
                        og.drawLine(cx+40,cy,cx+20,cy-20);
                        og.drawLine(cx+40,cy,cx+20,cy+20);
                        og.drawLine(cx+45,cy,cx+25,cy-20);
                        og.drawLine(cx+45,cy,cx+25,cy+20);
                      }
                      break;
            case 2 : if(td[py - 1][px] == 0){
                        og.drawLine(cx,cy-40,cx-20,cy-20);
                        og.drawLine(cx,cy-40,cx+20,cy-20);
                        og.drawLine(cx,cy-45,cx-20,cy-25);
                        og.drawLine(cx,cy-45,cx+20,cy-25);
                      }
                      break;
            case 3 :  if(td[py][px - 1] == 0){
                        og.drawLine(cx-40,cy,cx-20,cy-20);
                        og.drawLine(cx-40,cy,cx-20,cy+20);
                        og.drawLine(cx-45,cy,cx-25,cy-20);
                        og.drawLine(cx-45,cy,cx-25,cy+20);
                      }
                      break;
            case 4 : if(td[py + 1][px] == 0){
                        og.drawLine(cx,cy+40,cx-20,cy+20);
                        og.drawLine(cx,cy+40,cx+20,cy+20);
                        og.drawLine(cx,cy+45,cx-20,cy+25);
                        og.drawLine(cx,cy+45,cx+20,cy+25);
                      }
                      break;
         }
      }
      if(ef == 1){
         og.setFont(new Font("",Font.BOLD,54));
         og.setColor(Color.black);
         og.drawString("GET !!",72,172);
         og.setColor(Color.red);
         og.drawString("GET !!",70,170);
      }
      g.drawImage(os,0,0,this);
   }

   public void t_turn(){
      for(int t1 = 1;t1 < 5;t1 ++){
         for(int t2 = 1;t2 < 5;t2 ++){
            tt[t1][5-t2] = td[t2][t1];
         }
      }
      for(int t1 = 1;t1 < 5;t1 ++){
         for(int t2 = 1;t2 < 5;t2 ++){
            td[t2][t1] = tt[t2][t1];
         }
      }
      ex[2] = 5 - ey[0] ; ey[2] = ex[0] ;
      ex[0] = ex[2]     ; ey[0] = ey[2] ;
      ex[2] = 5 - ey[1] ; ey[2] = ex[1] ;
      ex[1] = ex[2]     ; ey[1] = ey[2] ;
   }

   public void d_set(){
      fg = 1 ; ef = 0 ;
      for(int i1 = 0;i1 < 6;i1 ++){
         for(int i2 = 0;i2 < 6;i2 ++){
            td[i2][i1] = 0;
         }
      }
      for(int i1 = 0;i1 < 6;i1 ++){
         td[0][i1] = -1 ; td[5][i1] = -1 ; td[i1][0] = -1 ; td[i1][5] = -1 ;
      }
      td[1][1] = 1 ; td[1][4] = 1 ; td[4][1] = 1 ; td[4][4] = 1 ;
      td[2][2] = 2 ; td[3][3] = 2 ;
      ex[0] = 2 ; ey[0] = 2 ; ex[1] = 3 ; ey[1] = 3 ;
   }

   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(50);
         }
         catch (InterruptedException e){
         }
      }
   }

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