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

public class r_block extends Applet implements Runnable{

   Image os;
   Graphics og;
   Thread th = null;
   int td[][] = new int[8][8];
   int bd[][][] = {{{ 1, 0, 0,-1, 0, 0,-1, 0},
                    { 0, 0, 0,-1,-1, 0, 0, 0},
                    {-1, 0, 0, 0, 0, 0, 0, 0},
                    {-1, 0,-1,-1, 0, 0, 0, 0},
                    { 0, 0, 0, 0,-1,-1, 0,-1},
                    { 0, 0, 0, 0, 0, 0, 0,-1},
                    { 0, 0, 0,-1,-1, 0, 0, 0},
                    { 0,-1, 0, 0,-1, 0, 0, 2}},
                   {{ 1, 0, 0, 0, 0, 0, 0, -1},
                    { 0, 0, 0, 0,-1, 0, 0, 0},
                    { 0, 0,-1, 0, 0, 0, 0, 0},
                    { 0, 0, 0, 0,-1,-1, 0, 0},
                    { 0,-1, 0, 0,-1,-1, 0,-1},
                    { 0,-1, 0,-1,-1, 0, 0, 0},
                    { 0, 0, 0, 0, 0, 0, 0, 0},
                    { 0, 0, 0, 0,-1, 0, 0, 2}},
                   {{ 0, 0, 0, 0, 0,-1, 0, 0},
                    { 0,-1,-1, 0, 0, 0, 0, 0},
                    { 0, 0, 0, 0, 0,-1, 0, 0},
                    { 0, 2, 1,-1, 0, 0, 0, 0},
                    {-1, 0, 0,-1, 0, 0, 0,-1},
                    {-1, 0,-1, 0, 0,-1, 0, 0},
                    { 0, 0, 0, 0,-1, 0, 0, 0},
                    { 0, 0, 0, 0, 0, 0, 0, 0}},
                   {{ 0, 0, 0, 0, 0, 0, 0, 0},
                    { 1, 0,-1, 0, 0, 2, 0, 0},
                    { 0, 0, 0, 0,-1, 0,-1, 0},
                    { 0,-1, 0, 0, 0, 0, 0, 0},
                    { 0,-1, 0,-1, 0, 0, 0, 0},
                    { 0, 0, 0, 0, 0,-1,-1, 0},
                    { 0, 0, 0,-1, 0, 0, 0, 0},
                    {-1, 0, 0, 0, 0, 0, 0, 0}}};
   Color wo = new Color(102,132,132);
   Color bg = new Color(224,224,204);
   Image ar[] = new Image[4];
   int no = 0,fg = 0,st = 1;
   int bx,by,dd,px,py,gx,gy;
   double pi = 3.141592,r2 = 1.414214,r5 = 2.236068,ang;
   int x1,y1,x2,y2,lx1,ly1,lx2,ly2;

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

      R_set();

      ar[0] = getImage(getDocumentBase(),"Picture/arw.gif");
      ar[1] = getImage(getDocumentBase(),"Picture/are.gif");
      ar[2] = getImage(getDocumentBase(),"Picture/arn.gif");
      ar[3] = getImage(getDocumentBase(),"Picture/ars.gif");

      addMouseListener(
         new MouseAdapter(){
            public void mousePressed(MouseEvent e){
               px = e.getX();
               py = e.getY();
               if(fg == 0){
                  if((Math.abs(px-370)<20)&&(Math.abs(py-170)<20)){
                     if((st == 1)&&(td[by][bx+1] >= 0)&&(td[by][bx+2] >= 0)){
                        dd = 1;fg = 1;th.resume();
                     }else if((st == 2)&&(td[by][bx+2] >= 0)){
                        dd = 5;fg = 1;th.resume();
                     }else if((st == 3)&&(td[by][bx+1] >= 0)&&(td[by+1][bx+1] >= 0)){
                        dd = 9;fg = 1;th.resume();
                     }
                  }else if((Math.abs(px-340)<20)&&(Math.abs(py-120)<20)){
                     if((st == 1)&&(td[by-1][bx] >= 0)&&(td[by-2][bx] >= 0)){
                        dd = 2;fg = 1;th.resume();
                     }else if((st == 2)&&(td[by-1][bx] >= 0)&&(td[by-1][bx+1] >= 0)){
                        dd = 6;fg = 1;th.resume();
                     }else if((st == 3)&&(td[by-1][bx] >= 0)){
                        dd = 10;fg = 1;th.resume();
                     }
                  }else if((Math.abs(px-310)<20)&&(Math.abs(py-170)<20)){
                     if((st == 1)&&(td[by][bx-1] >= 0)&&(td[by][bx-2] >= 0)){
                        dd = 3;fg = 1;th.resume();
                     }else if((st == 2)&&(td[by][bx-1] >= 0)){
                        dd = 7;fg = 1;th.resume();
                     }else if((st == 3)&&(td[by][bx-1] >= 0)&&(td[by+1][bx-1] >= 0)){
                        dd = 11;fg = 1;th.resume();
                     }
                  }else if((Math.abs(px-340)<20)&&(Math.abs(py-220)<20)){
                     if((st == 1)&&(td[by+1][bx] >= 0)&&(td[by+2][bx] >= 0)){
                        dd = 4;fg = 1;th.resume();
                     }else if((st == 2)&&(td[by+1][bx] >= 0)&&(td[by+1][bx+1] >= 0)){
                        dd = 8;fg = 1;th.resume();
                     }else if((st == 3)&&(td[by+2][bx] >= 0)){
                        dd = 12;fg = 1;th.resume();
                     }
                  }else if((Math.abs(px-340)<50)&&(Math.abs(py-268)<13)){
                     no ++ ;
                     if(no > 3)no = 0 ;
                     R_set() ;
                  }
               }
               repaint();
            }
         }
      );

   }

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

      if((bx == gx)&&(by == gy)&&(st == 1)){
         og.setColor(Color.red);
      }else{
         og.setColor(wo);
      }
      og.fill3DRect(20,20,260,260,true);
      og.setColor(bg);
      og.fillRect(30,30,240,240);

      for(int px = 0;px < 8;px ++){
         for(int py = 0;py < 8;py ++){
            if(td[py][px] == -1){
               og.setColor(wo);
               og.fill3DRect(px*30+30,py*30+30,30,30,true);
            }else if(td[py][px] == 1){
               og.setColor(Color.red);
               og.drawRect(px*30+35,py*30+35,20,20);
            }else if(td[py][px] == 2){
               og.setColor(Color.blue);
               og.drawRect(px*30+35,py*30+35,20,20);
            }
         }
      }
      og.drawImage(ar[0],290,150,this);
      og.drawImage(ar[1],350,150,this);
      og.drawImage(ar[2],320,100,this);
      og.drawImage(ar[3],320,200,this);

      og.setColor(Color.red);
      og.drawRect(290,25,20,20);
      og.setColor(Color.blue);
      og.drawRect(290,65,20,20);
      og.setFont(new Font("",Font.BOLD,16));
      og.setColor(Color.black);
      og.drawString("START",320,40);
      og.drawString("GOAL",320,80);

      og.drawRect(290,255,100,25);
      og.drawString("NO."+(no+1),325,273);

      og.setColor(Color.yellow);
      og.fillRect(x1,y1,lx1,ly1);
      og.fillRect(x2,y2,lx2,ly2);
      og.setColor(Color.black);
      og.drawRect(x1,y1,lx1,ly1);
      og.drawRect(x2,y2,lx2,ly2);

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

   public void R_set(){
      for(int idx = 0;idx < 8;idx ++){
         for(int idy = 0;idy < 8;idy ++){
            td[idy][idx] = bd[no][idy][idx];
            if(td[idy][idx] == 1){
               bx = idx ; by = idy ;
            }
            if(td[idy][idx] == 2){
               gx = idx ; gy = idy ;
            }
         }
      }
      ang = 0.5;
      dd = 1;
      x1 = bx * 30 + 30 ; y1 = by * 30 + 30 ;
      x2 = x1 ; y2 = y1 ;
      lx1 = 0 ; ly1 = 30 ; lx2 = 30 ; ly2 = 30 ;
   }

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

   public void stop(){
      th = null;
   }

   public void run(){
      while(th != null){
         try{
            ang = ang + pi / 90;
            if(ang > pi / 2){
               switch(dd){
                  case 1 : bx = bx + 1 ; st = 2 ; break ;
                  case 2 : by = by - 2 ; st = 3 ; break ;
                  case 3 : bx = bx - 2 ; st = 2 ; break ;
                  case 4 : by = by + 1 ; st = 3 ; break ;
                  case 5 : bx = bx + 2 ; st = 1 ; break ;
                  case 6 : by = by - 1 ; st = 2 ; break ;
                  case 7 : bx = bx - 1 ; st = 1 ; break ;
                  case 8 : by = by + 1 ; st = 2 ; break ;
                  case 9 : bx = bx + 1 ; st = 3 ; break ;
                  case 10 : by = by - 1 ; st = 1 ; break ;
                  case 11 : bx = bx - 1 ; st = 3 ; break ;
                  case 12 : by = by + 2 ; st = 1 ; break ;
               }
               fg = 0 ; ang = 0 ;
               th.suspend();
            }

            if(dd == 1){
               x1 = bx * 30 + 60 - (int)(Math.round(30 * Math.cos(ang)));
               y1 = by * 30 + 30;
               x2 = bx * 30 + 60 - (int)(Math.round(30 * r5 * Math.cos(ang + 1.10715)));
               y2 = by * 30 + 30;
               lx1 = x2 - x1;
               ly1 = 30;
               lx2 = bx * 30 + 60 + (int)(Math.round(60 * Math.cos(pi/2 - ang))) - x2;
               ly2 = 30;
            }else if(dd == 2){
               x1 = bx * 30 + 30;
               y1 = by * 30 + 30 - (int)(Math.round(60 * Math.cos(pi/2 - ang)));
               x2 = bx * 30 + 30;
               y2 = by * 30 + 30 + (int)(Math.round(30 * r5 * Math.cos(ang + 1.10715)));
               lx1 = 30;
               ly1 = y2 - y1;
               lx2 = 30;
               ly2 = by * 30 + 30 + (int)(Math.round(30 * Math.cos(ang))) - y2;
            }else if(dd == 3){
               x1 = bx * 30 + 30 - (int)(Math.round(60 * Math.cos(pi/2 - ang)));
               y1 = by * 30 + 30;
               x2 = bx * 30 + 30 + (int)(Math.round(30 * r5 * Math.cos(ang + 1.10715)));
               y2 = by * 30 + 30;
               lx1 = x2 - x1;
               ly1 = 30;
               lx2 = bx * 30 + 30 + (int)(Math.round(30 * Math.cos(ang))) - x2;
               ly2 = 30;
            }else if(dd == 4){
               x1 = bx * 30 + 30;
               y1 = by * 30 + 60 - (int)(Math.round(30 * Math.cos(ang)));
               x2 = bx * 30 + 30;
               y2 = by * 30 + 60 - (int)(Math.round(30 * r5 * Math.cos(ang + 1.10715)));
               lx1 = 30;
               ly1 = y2 - y1;
               lx2 = 30;
               ly2 = by * 30 + 60 + (int)(Math.round(60 * Math.cos(pi/2 - ang))) - y2;
            }else if(dd == 5){
               x1 = bx * 30 + 90 - (int)(Math.round(60 * Math.cos(ang)));
               y1 = by * 30 + 30;
               x2 = bx * 30 + 90 - (int)(Math.round(30 * r5 * Math.cos(ang + 0.4636476)));
               y2 = by * 30 + 30;
               lx1 = x2 - x1;
               ly1 = 30;
               lx2 = bx * 30 + 90 + (int)(Math.round(30 * Math.cos(pi/2 - ang))) - x2;
               ly2 = 30;
            }else if(dd == 6){
               x1 = bx * 30 + 30;
               y1 = by * 30 + 30 - (int)(Math.round(30 * Math.cos(pi/2 - ang)));
               x2 = bx * 30 + 30;
               y2 = by * 30 + 30 + (int)(Math.round(30 * r2 * Math.cos(ang + pi/4)));
               lx1 = 60;
               ly1 = y2 - y1;
               lx2 = 60;
               ly2 = by * 30 + 30 + (int)(Math.round(30 * Math.cos(ang))) - y2;
            }else if(dd == 7){
               x1 = bx * 30 + 30 - (int)(Math.round(30 * Math.cos(pi/2 - ang)));
               y1 = by * 30 + 30;
               x2 = bx * 30 + 30 + (int)(Math.round(30 * r5 * Math.cos(ang + 0.4636476)));
               y2 = by * 30 + 30;
               lx1 = x2 - x1;
               ly1 = 30;
               lx2 = bx * 30 + 30 + (int)(Math.round(60 * Math.cos(ang))) - x2;
               ly2 = 30;
            }else if(dd == 8){
               x1 = bx * 30 + 30;
               y1 = by * 30 + 60 - (int)(Math.round(30 * Math.cos(ang)));
               x2 = bx * 30 + 30;
               y2 = by * 30 + 60 - (int)(Math.round(30 * r2 * Math.cos(ang + pi/4)));
               lx1 = 60;
               ly1 = y2 - y1;
               lx2 = 60;
               ly2 = by * 30 + 60 + (int)(Math.round(30 * Math.cos(pi/2 - ang))) - y2;
            }else if(dd == 9){
               x1 = bx * 30 + 60 - (int)(Math.round(30 * Math.cos(ang)));
               y1 = by * 30 + 30;
               x2 = bx * 30 + 60 - (int)(Math.round(30 * r2 * Math.cos(ang + pi/4)));
               y2 = by * 30 + 30;
               lx1 = x2 - x1;
               ly1 = 60;
               lx2 = bx * 30 + 60 + (int)(Math.round(30 * Math.cos(pi/2 - ang))) - x2;
               ly2 = 60;
            }else if(dd == 10){
               x1 = bx * 30 + 30;
               y1 = by * 30 + 30 - (int)(Math.round(30 * Math.cos(pi/2 - ang)));
               x2 = bx * 30 + 30;
               y2 = by * 30 + 30 + (int)(Math.round(30 * r5 * Math.cos(ang + 0.4636476)));
               lx1 = 30;
               ly1 = y2 - y1;
               lx2 = 30;
               ly2 = by * 30 + 30 + (int)(Math.round(60 * Math.cos(ang))) - y2;
            }else if(dd == 11){
               x1 = bx * 30 + 30 - (int)(Math.round(30 * Math.cos(pi/2 - ang)));
               y1 = by * 30 + 30;
               x2 = bx * 30 + 30 + (int)(Math.round(30 * r2 * Math.cos(ang + pi/4)));
               y2 = by * 30 + 30;
               lx1 = x2 - x1;
               ly1 = 60;
               lx2 = bx * 30 + 30 + (int)(Math.round(30 * Math.cos(ang))) - x2;
               ly2 = 60;
            }else if(dd == 12){
               x1 = bx * 30 + 30;
               y1 = by * 30 + 90 - (int)(Math.round(60 * Math.cos(ang)));
               x2 = bx * 30 + 30;
               y2 = by * 30 + 90 - (int)(Math.round(30 * r5 * Math.cos(ang + 0.4636476)));
               lx1 = 30;
               ly1 = y2 - y1;
               lx2 = 30;
               ly2 = by * 30 + 90 + (int)(Math.round(30 * Math.cos(pi/2 - ang))) - y2;
            }

            Thread.sleep(30);
         }
         catch (InterruptedException e){}
         repaint();
      }
   }

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