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

public class o_cards extends Applet implements Runnable{

   int td[][] = {{1,2,3,4,5,6,7,8},{0,0,0,0,0,0,0,0}};
   int crx[] = new int[7];
   int cry[] = new int[7];
   int che[] = new int[8];
   int sw,mn;
   Image im[] = new Image[7];
   int cx,cy,cn,ss;
   int mx,my,px,py;
   Thread th = null;
   Image os;
   Graphics og;

   public void init(){
      os = createImage(615,180);
      og = os.getGraphics();
      setBackground(Color.white);
      for(int i = 0;i < 7;i ++){
         im[i] = getImage(getDocumentBase(),"Picture/mk"+(i+1)+".gif");
      }
      addMouseListener(
         new MouseAdapter(){
            public void mousePressed(MouseEvent e){
               px = (e.getX()-15) / 75;
               py = (e.getY()-15) / 75;
               if(sw == 0){
                  if(td[py][px] != 0){
                     sw = 1;
                     mn = td[py][px];
                     td[py][px] = 0;
                  }
               }else{
                  if(td[py][px] == 0){
                     sw = 0;
                     td[py][px] = mn;
                  }
               }
               repaint();
            }
         }
      );
      addMouseMotionListener(
         new MouseMotionAdapter(){
            public void mouseMoved(MouseEvent e){
               mx = e.getX() - 30;
               my = e.getY() - 20;
               repaint();
            }
         }
      );  
   }

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

      og.setColor(Color.black);
      for(int i1 = 0;i1 < 8;i1 ++){
         og.drawRect(i1*75+10,85,70,70);
      }
      for(int i1 = 0;i1 < 8;i1 ++){
         for(int i2 = 0;i2 < 2;i2 ++){
            if(td[i2][i1] != 0){
               c_draw(i1*75+15,i2*75+15,td[i2][i1]);
            }
         }
      }
      if(sw == 1){
         c_draw(mx,my,mn);
      }

      for(int i0 = 0;i0 < 8;i0 ++){
         if(td[1][i0] == 0){
            che[i0] = 0;
         }else if(td[1][i0] < 5){
            che[i0] = -1; 
         }else{
            che[i0] = 1;
         }
      }
      for(int i1 = 0;i1 < 8;i1 ++){
         if(che[i1] != 0){
            switch(td[1][i1]){
               case 1 : if(i1 < 6){
                           if((che[i1+1] == 1) && (che[i1+2] == 1)){
                              og.setColor(Color.blue);
                           }else{
                              og.setColor(Color.red);
                           }
                        }else{
                           og.setColor(Color.red);
                        }
                        break;
               case 2 : if(i1 < 6){
                           if(che[i1+1] * che[i1+2] < 0){
                              og.setColor(Color.blue);
                           }else{
                              og.setColor(Color.red);
                           }
                        }else{
                           og.setColor(Color.red);
                        }
                        break;
               case 3 : if((i1 > 0) && (i1 < 7)){
                           if((che[i1-1] == che[i1+1]) && (che[i1-1] != 0)){
                              og.setColor(Color.blue);
                           }else{
                              og.setColor(Color.red);
                           }
                        }else{
                           og.setColor(Color.red);
                        }
                        break;
               case 4 : if(i1 < 6){
                           ss = 0;
                           for(int i2 = 0;i2 < (7-i1);i2 ++){
                              if(che[i1+i2+1] == 1)ss ++;
                           }
                           if(ss == 2){
                              og.setColor(Color.blue);
                           }else{
                              og.setColor(Color.red);
                           }
                        }else{
                           og.setColor(Color.red);
                        }
                        break;
               case 5 : if((i1 > 0) && (i1 < 7)){
                           if((che[i1-1] == che[i1+1]) && (che[i1-1] != 0)){
                              og.setColor(Color.blue);
                           }else{
                              og.setColor(Color.red);
                           }
                        }else{
                           og.setColor(Color.red);
                        }
                        break;
               case 6 : if(i1 > 0){
                           if(che[i1-1] == -1){
                              og.setColor(Color.blue);
                           }else{
                              og.setColor(Color.red);
                           }
                        }else{
                           og.setColor(Color.red);
                        }
                        break;
               case 7 : if(i1 < 6){
                           if(che[i1+1]*che[i1+2] > 0){
                              og.setColor(Color.blue);
                           }else{
                              og.setColor(Color.red);
                           }
                        }else{
                           og.setColor(Color.red);
                        }
                        break;
               case 8 : if(i1 > 0){
                           if(che[i1-1] == 1){
                              og.setColor(Color.blue);
                           }else{
                              og.setColor(Color.red);
                           }
                        }else{
                           og.setColor(Color.red);
                        }
                        break;
               default : break;
            }
            og.drawRect(i1*75+10,85,70,70);
            og.drawRect(i1*75+9,84,72,72);
         }
      }

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

   public void c_draw(int x,int y,int n){
      cx = x ; cy = y ; cn = n;
      crx[0] = cx    ; cry[0] = cy    ;
      crx[1] = cx+30 ; cry[1] = cy    ;
      crx[2] = cx+45 ; cry[2] = cy+15 ;
      crx[3] = cx+60 ; cry[3] = cy+15 ;
      crx[4] = cx+60 ; cry[4] = cy+40 ;
      crx[5] = cx    ; cry[5] = cy+40 ;
      crx[6] = cx    ; cry[6] = cy    ;
      if(cn < 5){
         og.setColor(Color.red);
      }else{
         og.setColor(Color.blue);
      }
      og.fillPolygon(crx,cry,7);
      cry[4] = cy+60 ; cry[5] = cy+60;
      og.setColor(Color.black);
      og.drawPolygon(crx,cry,7);
      og.setFont(new Font("Century",Font.BOLD,12));
      og.setColor(Color.white);
      og.drawString("-"+cn+"-",cx+10,cy+15);
      switch(cn){
         case 1 : og.drawImage(im[0],cx+1,cy+41,this);break;
         case 2 : og.drawImage(im[1],cx+1,cy+41,this);break;
         case 3 : og.drawImage(im[2],cx+1,cy+41,this);break;
         case 4 : og.drawImage(im[3],cx+1,cy+41,this);break;
         case 5 : og.drawImage(im[2],cx+1,cy+41,this);break;
         case 6 : og.drawImage(im[4],cx+1,cy+41,this);break;
         case 7 : og.drawImage(im[5],cx+1,cy+41,this);break;
         case 8 : og.drawImage(im[6],cx+1,cy+41,this);break;
         default : break;
      }
   }

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