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

public class tilt extends Applet implements Runnable{

   Thread th = null;
   Image os;
   Graphics og;
   Choice cho = new Choice();
   Button rep;
   Label spa;
   int td[][] = new int[6][6];
   int yw[][] = new int[5][6];
   int tw[][] = new int[6][5];
   int psx[][] = {{0,3},{4,3},{3,1},{1,4},{0,0}};
   int psy[][] = {{3,2},{5,5},{4,5},{3,3},{1,0}};
   int pex[][] = {{5,2},{1,2},{2,4},{4,1},{5,5}};
   int pey[][] = {{3,2},{5,5},{1,0},{2,2},{5,4}};
   int ywd[][][] = {{{0,0,0,0,0,0},{1,0,0,0,0,1},{0,0,1,1,0,0},{1,0,0,0,0,1},{0,0,0,0,0,0}},
                    {{1,0,0,0,0,0},{0,1,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,1,0},{0,0,0,0,0,1}},
                    {{0,0,1,0,0,0},{0,0,0,0,0,1},{0,1,0,0,1,0},{1,0,0,0,0,0},{0,0,0,1,0,0}},
                    {{0,1,1,0,0,0},{0,0,0,1,0,1},{0,1,0,0,1,0},{1,0,1,0,0,0},{0,0,0,1,1,0}},
                    {{0,0,1,0,0,0},{1,0,0,1,0,0},{0,1,0,0,1,0},{0,0,1,0,0,1},{0,0,0,1,0,0}}};
   int twd[][][] = {{{0,0,1,0,0},{1,0,0,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,0,0,1},{0,0,1,0,0}},
                    {{0,0,1,0,0},{0,0,0,0,1},{0,0,0,1,0},{0,1,0,0,0},{1,0,0,0,0},{0,0,1,0,0}},
                    {{0,0,0,1,0},{1,0,0,0,0},{0,0,1,0,1},{1,0,1,0,0},{0,0,0,0,1},{0,1,0,0,0}},
                    {{0,0,0,1,0},{1,0,1,0,0},{1,0,0,0,0},{0,0,0,0,1},{0,0,1,0,1},{0,1,0,0,0}},
                    {{0,0,0,1,0},{0,0,1,0,0},{0,1,0,0,1},{1,0,0,1,0},{0,0,1,0,0},{0,1,0,0,0}}};
   int no = 0;
   int ax1 , ay1 , ax2 , ay2;
   int mx1 = 0 , my1 = 0 , mx2 = 0 , my2 = 0;
   int sw1 = 0 , sw2 = 0;
   int dd = 4;

   public void init(){
      os = createImage(320,320);
      og = os.getGraphics();
      setBackground(new Color(200,250,200));
      setLayout(new BorderLayout());
      Panel pan = new Panel();
      pan.setLayout(new FlowLayout());
      pan.add(rep = new Button("  replay  "));
      pan.add(spa = new Label("       "));
      pan.add(cho);
      cho.addItem("    No.1    ");
      cho.addItem("    No.2    ");
      cho.addItem("    No.3    ");
      cho.addItem("    No.4    ");
      cho.addItem("    No.5    ");
      cho.addItemListener(new MyListener());
      add("South",pan);
      R_set();
      rep.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
            R_set();
            repaint();
         }
      });
      addMouseListener(
         new MouseAdapter(){
            public void mousePressed(MouseEvent e){
               if((sw1 == 0) && (sw2 == 0)){
                  if((e.getY() < e.getX()) && (e.getY() > 320 - e.getX())){
                     dd =1  ; sw1 = 1 ; sw2 = 1 ; th.resume();
                  }else if((e.getY() > e.getX()) && (e.getY() > 320 - e.getX())){
                     dd = 2 ; sw1 = 1 ; sw2 = 1 ; th.resume();
                  }else if((e.getY() > e.getX()) && (e.getY() < 320 - e.getX())){
                     dd = 3 ; sw1 = 1 ; sw2 = 1 ; th.resume();
                  }else if((e.getY() < e.getX()) && (e.getY() < 320 - e.getX())){
                     dd = 4 ; sw1 = 1 ; sw2 = 1 ; th.resume();
                  }
               }
            }
         }
      );
   }
   class MyListener implements ItemListener{
      public void itemStateChanged(ItemEvent e){
         Choice cho = (Choice)e.getSource();
         switch(cho.getSelectedIndex()){
            case 0 : no = 0 ; R_set() ; repaint() ; break;
            case 1 : no = 1 ; R_set() ; repaint() ; break;
            case 2 : no = 2 ; R_set() ; repaint() ; break;
            case 3 : no = 3 ; R_set() ; repaint() ; break;
            case 4 : no = 4 ; R_set() ; repaint() ; break;
         }
      }
   }

   public void paint(Graphics g){
      og.setColor(new Color(200,250,200)); 
      og.fillRect(0,0,320,320);
      og.setColor(Color.white);
      og.fillRect(10,10,300,300);
      og.setColor(new Color(52,52,52));
      og.fill3DRect(38,38,245,245,true);
      og.setColor(new Color(245,240,250));
      og.fillRect(41,41,239,239);
      og.setColor(Color.black);
      for(int dx = 0;dx < 6;dx ++){
         for(int dy = 0;dy < 5;dy ++){
            if(yw[dy][dx] == 1){
               og.fill3DRect(dx*40+40,dy*40+79,40,2,true);
            }
         }
      }
      for(int dx = 0;dx < 5;dx ++){
         for(int dy = 0;dy < 6;dy ++){
            if(tw[dy][dx] == 1){
               og.fill3DRect(dx*40+79,dy*40+40,2,40,true);
            }
         }
      }
      og.setColor(Color.red);
      og.drawRect(pex[no][0]*40+50,pey[no][0]*40+50,20,20);
      og.setColor(Color.blue);
      og.drawRect(pex[no][1]*40+50,pey[no][1]*40+50,20,20);
      og.setColor(Color.red);
      og.fill3DRect(ax1*40+43+mx1,ay1*40+43+my1,34,34,true);
      og.setColor(Color.blue);
      og.fill3DRect(ax2*40+43+mx2,ay2*40+43+my2,34,34,true);
      g.drawImage(os,0,0,this);
   }

   public void R_set(){
      for(int idx = 0;idx < 6;idx ++){
         for(int idy = 0;idy < 6;idy ++){
            td[idy][idx] = 0;
         }
      }
      ax1 = psx[no][0] ; ay1 = psy[no][0] ;
      ax2 = psx[no][1] ; ay2 = psy[no][1] ;
      td[ay1][ax1] = 1;
      td[ay2][ax2] = 2;
      for(int idx = 0;idx < 6;idx ++){
         for(int idy = 0;idy < 5;idy ++){
            yw[idy][idx] = ywd[no][idy][idx];
         }
      }
      for(int idx = 0;idx < 5;idx ++){
         for(int idy = 0;idy < 6;idy ++){
            tw[idy][idx] = twd[no][idy][idx];
         }
      }      
      return;
   }

   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{
            if((sw1 == 0) && (sw2 == 0)){
               th.suspend();
            }else{

               if(dd == 1){
                  if((ax1 + mx1 / 40 == 5) || ((td[ay1][ax1 + mx1 / 40 + 1] != 0) && (sw2 == 0)) || (tw[ay1][ax1 + mx1 / 40] == 1)){
                     sw1 = 0;
                     td[ay1][ax1 + mx1 / 40] = 1;
                     if(mx1 / 40 > 0)td[ay1][ax1] = 0;
                     ax1 = ax1 + mx1 / 40;
                     mx1 = 0;
                  }else if(sw1 == 1){
                     mx1 = mx1 + 4;
                  }
                  if((ax2 + mx2 / 40 == 5) || ((td[ay2][ax2 + mx2 / 40 + 1] != 0) && (sw1 == 0)) || (tw[ay2][ax2 + mx2 / 40] == 1)){
                     sw2 = 0;
                     td[ay2][ax2 + mx2 / 40] = 2;
                     if(mx2 / 40 > 0)td[ay2][ax2] = 0;
                     ax2 = ax2 + mx2 / 40;
                     mx2 = 0;
                  }else if(sw2 == 1){
                     mx2 = mx2 + 4;
                  }
               }else if(dd == 2){
                  if((ay1 + my1 / 40 == 5) || ((td[ay1 + my1 / 40 + 1][ax1] != 0) && (sw2 == 0)) || (yw[ay1 + my1 / 40][ax1] == 1)){
                     sw1 = 0;
                     td[ay1 + my1 / 40][ax1] = 1;
                     if(my1 / 40 > 0)td[ay1][ax1] = 0;
                     ay1 = ay1 + my1 / 40;
                     my1 = 0;
                  }else if(sw1 == 1){
                     my1 = my1 + 4;
                  }
                  if((ay2 + my2 / 40 == 5) || ((td[ay2 + my2 / 40 + 1][ax2] != 0) && (sw1 == 0)) || (yw[ay2 + my2 / 40][ax2] == 1)){
                     sw2 = 0;
                     td[ay2 + my2 / 40][ax2] = 2;
                     if(my2 / 40 > 0)td[ay2][ax2] = 0;
                     ay2 = ay2 + my2 / 40;
                     my2 = 0;
                  }else if(sw2 == 1){
                     my2 = my2 + 4;
                  }
               }else if(dd == 3){
                  if((ax1 + mx1 / 40 == 0) || ((td[ay1][ax1 + mx1 / 40 - 1] != 0) && (sw2 == 0)) || (tw[ay1][ax1 + mx1 / 40 - 1] == 1)){
                     sw1 = 0;
                     td[ay1][ax1 + mx1 / 40] = 1;
                     if(mx1 / 40 < 0)td[ay1][ax1] = 0;
                     ax1 = ax1 + mx1 / 40;
                     mx1 = 0;
                  }else if(sw1 == 1){
                     mx1 = mx1 - 4;
                  }
                  if((ax2 + mx2 / 40 == 0) || ((td[ay2][ax2 + mx2 / 40 - 1] != 0) && (sw1 == 0)) || (tw[ay2][ax2 + mx2 / 40 - 1] == 1)){
                     sw2 = 0;
                     td[ay2][ax2 + mx2 / 40] = 2;
                     if(mx2 / 40 < 0)td[ay2][ax2] = 0;
                     ax2 = ax2 + mx2 / 40;
                     mx2 = 0;
                  }else if(sw2 == 1){
                     mx2 = mx2 - 4;
                  }
               }else if(dd == 4){
                  if((ay1 + my1 / 40 == 0) || ((td[ay1 + my1 / 40 - 1][ax1] != 0) && (sw2 == 0)) || (yw[ay1 + my1 / 40 - 1][ax1] == 1)){
                     sw1 = 0;
                     td[ay1 + my1 / 40][ax1] = 1;
                     if(my1 / 40 < 0)td[ay1][ax1] = 0;
                     ay1 = ay1 + my1 / 40;
                     my1 = 0;
                  }else if(sw1 == 1){
                     my1 = my1 - 4;
                  }
                  if((ay2 + my2 / 40 == 0) || ((td[ay2 + my2 / 40 - 1][ax2] != 0) && (sw1 == 0)) || (yw[ay2 + my2 / 40 - 1][ax2] == 1)){
                     sw2 = 0;
                     td[ay2 + my2 / 40][ax2] = 2;
                     if(my2 / 40 < 0)td[ay2][ax2] = 0;
                     ay2 = ay2 + my2 / 40;
                     my2 = 0;
                  }else if(sw2 == 1){
                     my2 = my2 - 4;
                  }
               }
            }               
            Thread.sleep(30);
         }         
         catch (InterruptedException e){ }
         repaint();
      }
   }

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

}