//------------------ myCell Class -------------------//

import java.awt.*;
import java.applet.*;
import java.io.*;
import java.lang.String;
import java.util.Random;
import java.util.Date;



public class myCell{

     int         doorNB[][],pieceID[][],direction[],offY,leftindex,rightindex;
     myPoint     loc,LOC,p0,p1;
     boolean     open[],up,leftend,rightend,edge;
     final int   LEFT=20,  TOP=10,  WW =14, HH= 12, 
                 RR=0,  LL=1, DD= 2,UU =3;
     
      public int randomise(Random nrand,int range)
	 {
	    if(range==0) return 0;
		return( java.lang.Math.abs(nrand.nextInt()) % range);
	 }

public void   DoInit()
{
 int i;
  
  doorNB =new int[3][2];
  pieceID=new int[3][2];
  direction=new int[3];
  
  loc=new myPoint();
  LOC=new myPoint();
  open =new boolean[3];
  p0=new myPoint();
  p1=new myPoint();
   
    for(i=0;i<3;i++) direction[i]=-1;
   
    for(i=0;i<3;i++) {
            pieceID[i][0]=-1;
            pieceID[i][1]=-1;
            
            open[i]=false;
            doorNB[i][0]=-1;//
     }
     
    
     leftend=rightend=false;
     
     leftindex=rightindex=0;
  
}

public void   DoSetLoc(int xx,int yy)
{
   loc.h=xx;
   loc.v=yy;
}

public void  DoGetLoc(myPoint dstP)
 {
     dstP.h=loc.h;
     dstP.v=loc.v;
 
  }
 
public  void  DoSetLoc2(int xx,int yy)
 {
    LOC.h=xx;
    LOC.v=yy;
 }
 
public  void  DoGetLoc2(myPoint dstP)
 {
     dstP.h=LOC.h;
     dstP.v=LOC.v;
 }
         
public  void  DoSetOpen(int index,boolean bb)
 {
    open[index]=bb;
  }
  
public   boolean  DoGetOpen(int index)
 {
    return open[index];
 }
  
public   void  DoSetUP(boolean bb)
  {
   up=bb;
 }
 
public  boolean  DoGetUP()
 {
    return up;
 }
 
public void   DoSetDoorNBID(int id0,int id1,int val)
{

    doorNB[id0][id1]=val;

}

public int  DoGetDoorNBID(int id0,int id1)
{
   return doorNB[id0][id1];

}

public void  DoSetDirection(int index,int val)
{
    direction[index]=val;
 }
 
public  int  DoGetDirection(int index)
 {
    return direction[index];
  }
  
public  void  DoSetPieceID(int id0,int id1,int val)
 {
   pieceID[id0][id1]=val;
 }
 
public  int  DoGetPieceID(int id0,int id1)
 {
   return pieceID[id0][id1];
}

public int  doGetEmptyNum()
{
 int i,num=0;
 
   if(!leftend && !rightend){
     for(i=0;i<3;i++){
        if(pieceID[i][0]==-1) num ++;
        if(pieceID[i][1]==-1) num ++;
     }
     
     if(num==6) return 2;
     else if(num==0) return 0;
     return 1;
   }
    //---------------------------//
   for(i=0;i<3;i++){
        if(pieceID[i][0]!=-1) return 0;
        if(pieceID[i][1]!=-1) return 0;
    }
    return 1;
    //--------------------------//
}


public void   doSetCellID(int ID,Random nrand)
{
 int num,i,j,s0;
 
     num=doGetEmptyNum();
     if(num==0) return;
     
     if(num==1){
     
       if(!leftend && !rightend){ /////////////////////
          for(i=0;i<3;i++) {
            for(j=0;j<2;j++) {
               if(pieceID[i][j]==-1) pieceID[i][j]=ID;
            }
          }
       }else{//---------------------------//
    
          for(i=0;i<3;i++) for(j=0;j<2;j++) pieceID[i][j]=ID;////
       
       }//---------------------------------//
    
     }else { 
       
        s0=randomise(nrand,3);
        pieceID[s0][0]=ID;
        pieceID[s0][1]=ID;
        s0=s0+randomise(nrand,2)+1; s0=s0 % 3;
        pieceID[s0][0]=ID;
    }

}

public int     DoGetPieceNum()
{
 
   if(leftend  || rightend) return 1; /////////////////
   
   if(pieceID[0][0]==pieceID[1][0] && 
      pieceID[1][0]==pieceID[2][0]) return 1;
    
     return 2;
}

public int     DoGetDoorNBbyCell(int cellid)
{
 int i;
 
   for(i=0;i<3;i++){
      if(doorNB[i][0]==cellid) return doorNB[i][1];
   }
    return -1;
}

public int    DoGetDirectionByPiece(int srcID)
{
 int i;
 
   for(i=0;i<3;i++){
      if(pieceID[i][0]==srcID && pieceID[i][1]==srcID) return direction[i];
   }
   
   return -1;

}

public boolean   DoExistThisPiece(int srcID)
{
 int i;

    for(i=0;i<3;i++){
      if(pieceID[i][0]==srcID && pieceID[i][1]==srcID) return true;
    }
    return false;
}

public void DoResetPieceID()
{
 int i,j;
 
   for(i=0;i<3;i++) for(j=0;j<2;j++) pieceID[i][j]=-1;

}

public void  DoGetPieceID2(int dst[])
{
 int i,s0;
 
   dst[0]=pieceID[0][0];
   
   for(i=0;i<3;i++){
      s0=pieceID[i][0];
      if(s0!=dst[0]) {  dst[1]=s0; return; }
    }

}

public void   DoSetLeftEnd(boolean bb)
{
   leftend=bb;
}

public boolean DoGetLeftEnd()
{
  return leftend;

}

public void    DoSetRightEnd(boolean bb)
{
  rightend=bb;

}

public boolean DoGetRightEnd()
 {
   return rightend;
 
 }
 
public  boolean  DoGetLeftOrRightEnd()
 {
     if(rightend || leftend) return true;
    
      return false;
 }
 
public void    DoSetRightLeftEndID(int srcID)
{
 int i,j;
 
   for(i=0;i<3;i++) for(j=0;j<2;j++)    pieceID[i][j]=srcID;
 
}

public int   DoGetRightLeftEndID()
{

   return pieceID[0][0];
}

public void   DoSetupUpDown()
 {
 
    up= DoGetUpOrDown(loc);
 
 }

public int    DoGetDownCell()
{
  int i;
  
    for(i=0;i<3;i++){
      if(direction[i]==DD){
           return (doorNB[i][0]);
      }
    }
    return -1;
 }

public int      DoGetUpCell()
{
 int i;
  
    for(i=0;i<3;i++){
      if(direction[i]==UU){
           return (doorNB[i][0]);
      }
    }
    return -1;

}

public boolean    DoExistThisNextCell(int srcID)
{
 int i;
 
    for(i=0;i<3;i++){
      if(doorNB[i][0]==srcID) return true;
    }

     return false;

}


public void  DoSetUpmyLoc(myCell gCell[])
{
 int    s0,sx,sy,i;
// myPoint    p0,p1;
 
     p0.h=loc.h; p0.v=loc.v;
    
      //----------------------------------//
     if(p0.v<4)    sx= LEFT + (WW/2)*7 + (3 - p0.v)*(WW + WW/2) + p0.h*(WW/2);
     else if(p0.v<11) sx=LEFT + (10-p0.v)*(WW/2) + p0.h*(WW/2);
     else if(p0.v<19) sx=LEFT - WW/2 + p0.h*(WW/2);
     else if(p0.v<26) sx=LEFT + (p0.v-19)*(WW/2) + p0.h*(WW/2);
     else sx=LEFT + (WW/2)*7 + (p0.v-26)*(WW + WW/2) + p0.h *(WW/2);
     //-----------------------------------//
        sy= TOP  + p0.v*HH + HH/2;
        
      LOC.h=sx;LOC.v=sy; // DoSetLoc2(sx,sy);
    // up=bb; //DoSetUP(bb);  
       
     for(i=0;i<3;i++){
       int   id;
       
        if(!open[i]) continue;
    
        id=DoGetDoorNBID(i,0); //doorNB[i][0];
        if(id<0 || id>=1278) continue; 
         
         gCell[id].DoGetLoc(p1);  ///////// 5/26 
      //  DomyGetCellLoc(id,p1);
        
        if(p0.v==p1.v) {
            if(p0.h=11 && loc.v<19) leftindex=0;
        else if(loc.v<4){
          if(loc.h==0) leftindex=1;
          if(loc.h==1) leftindex=2;
        }else {
          if(loc.h==0) leftindex=1;
          if(loc.h==1) leftindex=2;
         }
     }
     
     if(rightend){
     
        sx=DoGetRightEndIndex(loc);
  
        if(loc.v>=11 && loc.v<19) rightindex=0;
        else if(loc.v<4){
          if(loc.h==sx)   rightindex=1;
          if(loc.h==sx-1) rightindex=2;
        }else {
          if(loc.h==sx)   rightindex=1;
          if(loc.h==sx-1) rightindex=2;
         }
      
     }

}

private boolean hidalihasi(myPoint p0)
{
   if(p0.h==0) {
      if(p0.v>=4 && p0.v<11) return false;
      if(p0.v>=19 && p0.v<26) return false;
       return true;
   }
  
   if(p0.v<4 || p0.v>=26 ){
           if(p0.h==1) return true;
    }
    
    return false;
}

private boolean migihasi(myPoint p0)
{
  if(p0.v>=11 && p0.v<19){ if(p0.h==52) return true;}
  
   switch(p0.v){
   
        case 0: case 29: if(p0.h==17 || p0.h==18) return true; break;
        case 1: case 28: if(p0.h==23 || p0.h==24) return true; break;
        case 2: case 27: if(p0.h==29 || p0.h==30) return true; break;
        case 3: case 26: if(p0.h==35 || p0.h==36) return true; break;
         
    }
  
    return false;
    
}

private boolean uehasi(myPoint p0)
{
  if(p0.v==0) return true;
  
   switch(p0.v){
      case 1: if(p0.h==1 || p0.h==17) return true; break;
      case 2: if(p0.h==1 || p0.h==23) return true; break;
      case 3: if(p0.h==1 || p0.h==29) return true; break;
      case 4: if(p0.h==1 || p0.h==35) return true; break;
   }
  
   return false;

}

private boolean sitahasi(myPoint p0)
{
  if(p0.v==29) return true;
  
  switch(p0.v){
      case 29: if(p0.h==1 || p0.h==17) return true; break;
      case 28: if(p0.h==1 || p0.h==23) return true; break;
      case 27: if(p0.h==1 || p0.h==29) return true; break;
      case 26: if(p0.h==1 || p0.h==35) return true; break;
   }
   
  return false;

}

private boolean DoGetUpOrDown(myPoint P0)
{
 int     sx,s0,ss;
 boolean up;
 
     sx=DoGetCellDistW(P0);
    
     s0= sx & 0x0001;
     if(s0==1) up=true;
     else up=false;
     
     ss=P0.v; ss=ss & 0x0001;   if(ss==1) up=!up; 
       
     return up;
 }
 
 
private int DoGetRightEndIndex(myPoint P0)
{

   switch(P0.v){
     case 0: case 29: return 18;
     case 1: case 28: return 24;
     case 2: case 27: return 30;
     case 3: case 26: return 36;
     case 4: case 25: return 38;
     case 5: case 24: return 40;
     case 6: case 23: return 42;
     case 7: case 22: return 44;
     case 8: case 21: return 46;
     case 9: case 20: return 48;
     case 10: case 19: return 50;
     default: return 52;
   }

}

private int DoGetCellDistW(myPoint p0)
{
 int x0=0;
 
    switch(p0.v){
     
      case 0:  case 29: x0=17; break;
      case 1:  case 28: x0=14; break;
      case 2:  case 27: x0=11; break;
      case 3:  case 26: x0=8;  break;
      case 4:  case 25: x0=7;  break;
      case 5:  case 24: x0=6;  break;
      case 6:  case 23: x0=5;  break;
      case 7:  case 22: x0=4;  break;
      case 8:  case 21: x0=3;  break;
      case 9:  case 20: x0=2;  break;
      case 10: case 19: x0=1;  break;
      
      case 11: case 12: case 13: case 14:
      case 15: case 16: case 17: case 18:
              x0=0; break;
      
   }
    x0 +=p0.h;
    
   return x0;
    
}

//---------------- DRAW CELL ----------------------//
public void  DoDrawFrame(Graphics G,int offset)
{
 int  xx,yy;
 
  offY=offset;
 
   xx=LOC.h;
   yy=LOC.v;
   
   G.setColor(Color.white);
  
   if(up){
   
      if(leftend){
      
       
      switch(leftindex){
            case 0:
                G.drawLine(xx,yy + HH/2 + offY,      xx+WW/2,yy + HH/2 + offY);
                G.drawLine(xx+WW/2,yy + HH/2 + offY, xx,     yy - HH/2 + offY);
                G.drawLine(xx,     yy - HH/2 + offY, xx,     yy + HH/2 + offY);
                break;
      
            case 1:
                 G.drawLine(xx-WW/2,yy + HH/2 + offY, xx+WW/2, yy + HH/2 + offY);
                 G.drawLine(xx+WW/2,yy + HH/2 + offY, xx+WW/4, yy + offY);
                 G.drawLine(xx+WW/4,yy + offY,        xx-WW/2, yy + HH/2 + offY);
                 break;
                
            case 2:
                 G.drawLine(xx-WW/4,yy + offY,        xx+WW/2,yy + HH/2 + offY);
                 G.drawLine(xx+WW/2,yy + HH/2 + offY, xx,     yy - HH/2 + offY);
                 G.drawLine(xx,     yy - HH/2 + offY, xx-WW/4,yy  + offY);
                 break;
          }
     
     
     
      }else if(rightend){
      
             
          switch(rightindex){
         
            case 0:
               G.drawLine(xx-WW/2,yy + HH/2 + offY, xx,     yy + HH/2 + offY);
               G.drawLine(xx,yy + HH/2 + offY,      xx,     yy - HH/2 + offY);
               G.drawLine(xx,     yy - HH/2 + offY, xx-WW/2,yy + HH/2 + offY);
               break;
            case 1:
            
               G.drawLine(xx-WW/2,yy + HH/2 + offY, xx+WW/2,   yy + HH/2 + offY);
               G.drawLine(xx+WW/2,yy + HH/2 + offY, xx-WW/4,   yy + offY);
               G.drawLine(xx-WW/4, yy + offY,       xx-WW/2,   yy + HH/2 + offY);
               break;
            case 2:
            
               G.drawLine(xx-WW/2,yy + HH/2 + offY, xx+WW/4,     yy  + offY);
               G.drawLine(xx+WW/4,yy  + offY,       xx,     yy - HH/2 + offY);
               G.drawLine(xx,     yy - HH/2 + offY, xx-WW/2,yy + HH/2 + offY);
               break;
          }
          
          
          
      }else{
          G.drawLine(xx-WW/2,yy + HH/2 + offY, xx+WW/2,yy + HH/2 + offY);
          G.drawLine(xx+WW/2,yy + HH/2 + offY, xx,     yy - HH/2 + offY);
          G.drawLine(xx,     yy - HH/2 + offY, xx-WW/2,yy + HH/2 + offY);
            
      }
  }else{
  
     if(leftend){
        
          switch(leftindex){
          
           case 0:
                G.drawLine(xx,     yy - HH/2 + offY,  xx,     yy + HH/2 + offY);
                G.drawLine(xx,     yy + HH/2 + offY,  xx+WW/2,yy - HH/2 + offY);
                G.drawLine(xx+WW/2,yy - HH/2 + offY,  xx     ,yy - HH/2 + offY);
                break;
                
           case 1:
                G.drawLine(xx-WW/2, yy - HH/2 + offY,  xx+WW/4,     yy  + offY);
                G.drawLine(xx+WW/4, yy + offY,         xx+WW/2,yy - HH/2 + offY);
                G.drawLine(xx+WW/2, yy - HH/2 + offY,  xx-WW/2     ,yy - HH/2 + offY);
                break;
           case 2:
           
                G.drawLine(xx-WW/4,     yy  + offY,   xx,     yy + HH/2 + offY);
                G.drawLine(xx,     yy + HH/2 + offY,  xx+WW/2,yy - HH/2 + offY);
                G.drawLine(xx+WW/2,yy - HH/2 + offY,  xx-WW/4     ,yy  + offY);
                break;
         }
      
     }else if(rightend){
         
         switch(rightindex){
          case 0:
             G.drawLine(xx-WW/2,yy - HH/2 + offY,  xx,     yy + HH/2 + offY);
             G.drawLine(xx,     yy + HH/2 + offY,  xx     ,yy - HH/2 + offY);
             G.drawLine(xx     ,yy - HH/2 + offY,  xx-WW/2,yy - HH/2 + offY);
             break;
             
          case 1:
          
             G.drawLine(xx-WW/2,yy - HH/2 + offY,  xx-WW/4,     yy  + offY);
             G.drawLine(xx-WW/4,     yy  + offY,   xx+WW/2     ,yy - HH/2 + offY);
             G.drawLine(xx+WW/2,yy - HH/2 + offY,  xx-WW/2,yy - HH/2 + offY);
             break;
          
          case 2: 
          
             G.drawLine(xx-WW/2,yy - HH/2 + offY,  xx,     yy + HH/2 + offY);
             G.drawLine(xx,     yy + HH/2 + offY,  xx+WW/4     ,yy  + offY);
             G.drawLine(xx+WW/4 ,yy  + offY,       xx-WW/2,yy - HH/2 + offY);
             break;
       }
         
     
     }else{
        G.drawLine(xx-WW/2,yy - HH/2 + offY,  xx,     yy + HH/2 + offY);
        G.drawLine(xx,     yy + HH/2 + offY,  xx+WW/2,yy - HH/2 + offY);
        G.drawLine(xx+WW/2,yy - HH/2 + offY,  xx-WW/2,yy - HH/2 + offY);
    }
  }

}

private void  DoGetFrame(int XX[],int YY[])
{
 int   xx,yy;
  
   xx=LOC.h;
   yy=LOC.v;
  
  if(up){//--------------------------//
  
      if(leftend){
      
          switch(leftindex){
            case 0: XX[0]=xx;        YY[0]=yy + HH/2;
             	    XX[1]=xx + WW/2; YY[1]=yy + HH/2;
             	    XX[2]=xx;        YY[2]=yy - HH/2;
                    break;
      
            case 1: XX[0]=xx-WW/2;   YY[0]=yy + HH/2;
            		XX[1]=xx+WW/2;   YY[1]=yy + HH/2;
            		XX[2]=xx+WW/4;   YY[2]=yy;
                    break;
                
            case 2: XX[0]=xx-WW/4;  YY[0]=yy;
                    XX[1]=xx+WW/2;  YY[1]=yy + HH/2;
                    XX[2]=xx;       YY[2]=yy - HH/2;
                    break;
          }
        
      }else if(rightend){
      
         switch(rightindex){
         
            case 0: XX[0]=xx-WW/2; YY[0]=yy + HH/2;
            		XX[1]=xx;      YY[1]=yy + HH/2;
            		XX[2]=xx;      YY[2]=yy - HH/2;
                    break;
            case 1: XX[0]=xx-WW/2;  YY[0]=yy + HH/2;
                    XX[1]=xx+WW/2;  YY[1]=yy + HH/2;
                    XX[2]=xx-WW/4;  YY[2]=yy;
                    break;
            case 2: XX[0]=xx-WW/2; YY[0]=yy + HH/2;
                    XX[1]=xx+WW/4; YY[1]=yy;
                    XX[2]=xx;      YY[2]=yy - HH/2;
                    break;
           }
             
      }else { 
          XX[0]=xx-WW/2; YY[0]=yy + HH/2;
          XX[1]=xx+WW/2; YY[1]=yy + HH/2;
          XX[2]=xx;      YY[2]=yy - HH/2;
      }
      
  }else{//-----------------------------//
  
    if(rightend){
      
       switch(rightindex){
          case 0: XX[0]=xx- WW/2; YY[0]=yy - HH/2;
                  XX[1]=xx;       YY[1]=yy + HH/2;
                  XX[2]=xx;       YY[2]=yy - HH/2;
                  break;
             
          case 1: XX[0]=xx-WW/2;  YY[0]=yy - HH/2;
                  XX[1]=xx-WW/4;  YY[1]=yy;
                  XX[2]=xx+WW/2;  YY[2]=yy - HH/2;
                  break;
          
          case 2: XX[0]=xx-WW/2;  YY[0]=yy - HH/2;
                  XX[1]=xx;       YY[1]=yy + HH/2;
                  XX[2]=xx + WW/4;YY[2]=yy;
                  break;
       }
             
    }else if(leftend){
    
        switch(leftindex){
           case 0: XX[0]=xx;      YY[0]=yy - HH/2;
                   XX[1]=xx;      YY[1]=yy + HH/2;
                   XX[2]=xx+WW/2; YY[2]=yy - HH/2;
                   break;
           case 1: XX[0]=xx-WW/2; YY[0]=yy - HH/2;
                   XX[1]=xx+WW/4; YY[1]=yy;
                   XX[2]=xx+WW/2; YY[2]=yy - HH/2;
                   break;
           case 2: XX[0]=xx-WW/4; YY[0]=yy;
                   XX[1]=xx;      YY[1]=yy + HH/2;
                   XX[2]=xx+WW/2; YY[2]=yy - HH/2;
                   break;
         }
             
             
    }else {
       XX[0]=xx-WW/2; YY[0]=yy - HH/2;
       XX[1]=xx;      YY[1]=yy + HH/2;
       XX[2]=xx+WW/2; YY[2]=yy - HH/2;
     }
    
  }
}   	        
   	  
public  void DoDrawCell2(Graphics G,int offY)
{
     Polygon	poly;
     int      XX[],YY[],xx,yy;
          
            XX=new int[3];
            YY=new int[3];
     	     
     	     xx=LOC.h;
     	     yy=LOC.v;
	          
              if(up){//--------------- UP ---------------------/
              
                 if(leftend){
           		   
       			  switch(leftindex){
          			  case 0:
             		         XX[0]= xx;      YY[0]=yy + HH/2 + offY;
    					     XX[1]= xx+WW/2; YY[1]=yy + HH/2 + offY;
     					     XX[2]= xx;      YY[2]=yy - HH/2 + offY;
                 		     break;
          			
          			  case 1:
             
               			     XX[0]=xx-WW/2; YY[0]= yy + HH/2 + offY;
                			 XX[1]=xx+WW/2; YY[1]= yy + HH/2 +offY;
                			 XX[2]=xx+WW/4; YY[2]= yy + offY;
                		     break;
                
           			 case 2:
              				 XX[0]=xx-WW/4; YY[0]=yy + offY;
                			 XX[1]=xx+WW/2; YY[1]=yy + HH/2 + offY;
               			     XX[2]=xx;      YY[2]=yy - HH/2 + offY;
               			     break;
         		   }
                  
                 }else if(rightend){
               	   
     			    switch(rightindex){
         
           		      case 0:
          		         XX[0]= xx-WW/2; YY[0]=yy + HH/2 + offY;
    			         XX[1]= xx;      YY[1]=yy + HH/2 + offY;
     			         XX[2]= xx;      YY[2]=yy - HH/2 + offY;
                         break;
            		  case 1:
             			 XX[0]=xx-WW/2; YY[0]= yy + HH/2 + offY;
             			 XX[1]=xx+WW/2; YY[1]= yy + HH/2 + offY;
             			 XX[2]=xx-WW/4; YY[2]= yy + offY;
             			 break;
          			  case 2:
            	 		 XX[0]=xx-WW/2; YY[0]=yy + HH/2 + offY;
              			 XX[1]=xx+WW/4; YY[1]=yy + offY;
              			 XX[2]=xx;      YY[2]=yy - HH/2 + offY;
            			 break;
         			 }
                 
                 }else {
          		   XX[0]= xx-WW/2; YY[0]=yy + HH/2 + offY;
    			   XX[1]= xx+WW/2; YY[1]=yy + HH/2 + offY;
     			   XX[2]= xx;      YY[2]=yy - HH/2 + offY;
     			}
      
 			  }else{//------------ DOWN ------------------//
 			  
 			    if(leftend){
 			 		  
     			  switch(leftindex){
         		    case 0:
            			  XX[0]=xx;      YY[0]=yy - HH/2 + offY;
     					  XX[1]=xx;      YY[1]=yy + HH/2 + offY;
     					  XX[2]=xx+WW/2; YY[2]=yy - HH/2 + offY;
            			  break;
          		   case 1:
           			 	  XX[0]=xx-WW/2; YY[0]= yy - HH/2 + offY;
           				  XX[1]=xx+WW/4; YY[1]= yy + offY;
           				  XX[2]=xx+WW/2; YY[2]= yy - HH/2 + offY;
             			  break;
         		   case 2:
           				  XX[0]=xx-WW/4; YY[0]= yy + offY;
            		      XX[1]=xx;      YY[1]= yy + HH/2 + offY;
           				  XX[2]=xx+WW/2; YY[2]= yy - HH/2 + offY;
             			  break;
        		   }
 			    
 			    }else if(rightend){
 			    
 		 		  
     			   switch(rightindex){
       				   case 0:
          				  XX[0]=xx-WW/2; YY[0]=yy - HH/2 + offY;
     					  XX[1]=xx;      YY[1]=yy + HH/2 + offY;
     					  XX[2]=xx;      YY[2]=yy - HH/2 + offY;
           				  break;
             
        				 case 1:
           				  XX[0]=xx-WW/2; YY[0]= yy - HH/2 + offY;
            			  XX[1]=xx-WW/4; YY[1]= yy + offY;
           				  XX[2]=xx+WW/2; YY[2]= yy - HH/2 + offY;
           				  break;
          
         				 case 2: 
            			  XX[0]=xx-WW/2; YY[0]= yy - HH/2 + offY;
          				  XX[1]=xx;      YY[1]= yy + HH/2 + offY;
           				  XX[2]=xx+WW/4; YY[2]= yy + offY;
             			  break;
    				  }
 			    
 			    }else {
     			  XX[0]=xx-WW/2; YY[0]=yy - HH/2 + offY;
     			  XX[1]=xx;      YY[1]=yy + HH/2 + offY;
     			  XX[2]=xx+WW/2; YY[2]=yy - HH/2 + offY;
     			}
 		      }
             
           //  G.setColor(Color.black);
             poly=new Polygon(XX,YY,3);
             G.fillPolygon(poly);
}	  
   	  
   	   
//----------------------------------------------//
 public void    DoGetFrameRgn2(int myID,int XX[],int YY[])
{
 int     i,j,id;
 boolean   bb=true;
 
   
     id=pieceID[0][0];
     for(i=0;i<3;i++) for(j=0;j<2;j++)   if(id!=pieceID[i][j]) bb=false;
     
     if(rightend || leftend) {
       bb=true;
       for(i=0;i<3;i++) {   id=pieceID[i][0]; if(id!=-1) break;}
     }
     
      
     if(bb) DoGetFrame(XX,YY);
     else {
       if(up) doDrawCellUp3(myID,XX,YY);
        else  doDrawCellDown3(myID,XX,YY);
     }
     
 
}


//--------------------- UP ----------------------//
private boolean  doDrawCellUp3(int myID,int XX[],int YY[])
{
 int i,id=-1,s0,id0=-1,id1=-1,id2=-1;
 
      for(i=0;i<3;i++) {  if(pieceID[i][0]!=pieceID[i][1]) { id=i; break;} }
      if(id==-1) return  false; 
      s0=direction[id];
      
      for(i=0;i<3;i++) {  if(direction[i]==LL) { id0=i;break;} }
      if(id0==-1) return false;
      
      for(i=0;i<3;i++) {  if(direction[i]==RR) { id1=i;break;} }
      if(id1==-1) return false;

      for(i=0;i<3;i++) {  if(direction[i]==DD) { id2=i;break;} }
      if(id2==-1) return false;
      
      switch(s0){
         case DD: doDrawCellUpDD3(pieceID[id0][0],pieceID[id1][0],myID,XX,YY); break;
         case LL: doDrawCellUpLL3(pieceID[id1][0],pieceID[id2][0],myID,XX,YY); break;
         case RR: doDrawCellUpRR3(pieceID[id2][0],pieceID[id0][0],myID,XX,YY); break;
      }
      
      return true;

}

private void  doDrawCellUpDD3(int id0,int id1,int myID,int XX[],int YY[])
{
 int xx,yy;
 
        xx=LOC.h;   yy=LOC.v;
        
      if(myID==id0){
        XX[0]=xx-WW/2; YY[0]=yy + HH/2;
        XX[1]=xx;      YY[1]=yy + HH/2;
        XX[2]=xx;      YY[2]=yy - HH/2;
      }else {
        XX[0]=xx;      YY[0]=yy + HH/2;
        XX[1]=xx+WW/2; YY[1]=yy + HH/2;
        XX[2]=xx;      YY[2]=yy - HH/2;
      }
}

private void     doDrawCellUpLL3(int id0,int id1,int myID,int XX[],int YY[])
{
 int xx,yy;
 
         xx=LOC.h;  yy=LOC.v;
    	     
    	if(myID==id0) {
    	  XX[0]= xx-WW/4; YY[0]=yy;
    	  XX[1]= xx+WW/2; YY[1]=yy + HH/2;
    	  XX[2]= xx;      YY[2]=yy - HH/2;
    	}  else {
    	  XX[0]= xx-WW/2;  YY[0]=yy + HH/2;
    	  XX[1]= xx+WW/2;  YY[1]=yy + HH/2;
    	  XX[2]= xx-WW/4;  YY[2]=yy;
        }
}

private void      doDrawCellUpRR3(int id0,int id1,int myID,int XX[],int YY[])
{
int xx,yy;
 
       xx=LOC.h;  yy=LOC.v;
         
     if(myID==id0) {
         XX[0]=xx-WW/2;   YY[0]=yy + HH/2;
         XX[1]=xx+WW/2;   YY[1]=yy + HH/2;
         XX[2]=xx+WW/4;   YY[2]=yy;
    } else  {
         XX[0]= xx-WW/2;  YY[0]=yy + HH/2;
         XX[1]= xx+WW/4;  YY[1]=yy;
         XX[2]= xx;       YY[2]=yy - HH/2;
     }
 
}
         
//-------------------------------------------------//
private boolean  doDrawCellDown3(int myID,int XX[],int YY[])
{
 int i,id=-1,s0,id0=-1,id1=-1,id2=-1;
 
      for(i=0;i<3;i++) {  if(pieceID[i][0]!=pieceID[i][1]) { id=i; break;} }
      if(id==-1) return  false; 
      s0=direction[id];
      
      for(i=0;i<3;i++) {  if(direction[i]==LL) { id0=i;break;} }
      if(id0==-1) return false;
      
      for(i=0;i<3;i++) {  if(direction[i]==RR) { id1=i;break;} }
      if(id1==-1) return false;

      for(i=0;i<3;i++) {  if(direction[i]==UU) { id2=i;break;} }
      if(id2==-1) return false;
      
      switch(s0){
         case UU: doDrawCellDownUU3(pieceID[id0][0],pieceID[id1][0],myID,XX,YY); break;
         case LL: doDrawCellDownLL3(pieceID[id1][0],pieceID[id2][0],myID,XX,YY); break;
         case RR: doDrawCellDownRR3(pieceID[id2][0],pieceID[id0][0],myID,XX,YY); break;
      }
      
      return true;
}

private void    doDrawCellDownUU3(int id0,int id1,int myID,int XX[],int YY[])
{
 int xx,yy;
 
        xx=LOC.h;   yy=LOC.v;
        
       if(myID==id0){
           XX[0]= xx-WW/2; YY[0]=yy - HH/2;
           XX[1]= xx;      YY[1]=yy + HH/2;
           XX[2]= xx;      YY[2]=yy - HH/2;
      } else {
           XX[0]= xx;      YY[0]=yy - HH/2;
           XX[1]= xx;      YY[1]=yy + HH/2;
           XX[2]= xx+WW/2; YY[2]=yy - HH/2;
      }
       
}

private void    doDrawCellDownLL3(int id0,int id1,int myID,int XX[],int YY[])
{
 int xx,yy;
 
        xx=LOC.h;   yy=LOC.v;
        
      if(myID==id0)  {
          XX[0]= xx-WW/4; YY[0]=yy;
          XX[1]= xx;      YY[1]=yy + HH/2;
          XX[2]= xx+WW/2; YY[2]=yy - HH/2;
     } else  {
          XX[0]= xx-WW/2; YY[0]=yy - HH/2;
          XX[1]= xx-WW/4; YY[1]=yy;
          XX[2]= xx+WW/2; YY[2]=yy - HH/2;
      }
}

private void    doDrawCellDownRR3(int id0,int id1,int myID,int XX[],int YY[])
{
 int xx,yy;
 
        xx=LOC.h;   yy=LOC.v;
        
      if(myID==id0) {
          XX[0]= xx-WW/2; YY[0]=yy - HH/2;
          XX[1]= xx+WW/4; YY[1]=yy;
          XX[2]= xx+WW/2; YY[2]=yy - HH/2;
       }else {
        XX[0]= xx-WW/2; YY[0]=yy - HH/2;
        XX[1]= xx;      YY[1]=yy + HH/2;
        XX[2]= xx+WW/4; YY[2]=yy;
      }
}
 
   	   
   	        

//-------------------------------------------------------------//
//-------------------------------------------------------------//

public boolean   DoCheckExistNotMe(int srcID)
{
 int i,j;
 
   if(leftend || rightend){
       if(pieceID[0][0]!=srcID) return true;
       else    return false;
   }
  
   for(i=0;i<3;i++) {
      for(j=0;j<2;j++){
         if(pieceID[i][j]!=srcID) return true;
      }
   }
    return false;

}

public boolean   DoCheckExistNotMeAndMe(int srcID)
{
 int i,j,num;
 
  num=DoGetPieceNum();
  if(num==1) return false;
  
   for(i=0;i<3;i++) {
      for(j=0;j<2;j++){
         if(pieceID[i][j]==srcID) return true;
      }
   }
    return false;

}

public void     DoSetPieceIDAll(int id)
{
 int i,j;
    
   for(i=0;i<3;i++) {
      for(j=0;j<2;j++){
          pieceID[i][j]=id;
      }
   }

}

public void      DoSetPieceIDbyType(int id,int halfindex[])
{
 int i,j;
 
   if(leftend || rightend){
       for(i=0;i<3;i++) for(j=0;j<2;j++) pieceID[i][j]=id;
       return;
   }
 
   for(i=0;i<3;i++){
   
     if(direction[i]==halfindex[0]){
       pieceID[i][0]=pieceID[i][1]=id;
     }
      
     if(direction[i]==halfindex[1]){
       if(pieceID[i][0]==-1) pieceID[i][0]=id;
       else  pieceID[i][1]=id;
     }
   } 

}

public void      DoGetCellHalfType(int val[])
{
     if(!DoGetLeftOrRightEnd())  return;

     if(leftend){
     
        switch(leftindex){
           case 0:  if(up){ val[0]=RR; val[1]=DD;}
           			else  { val[0]=RR; val[1]=UU;}
           			break;
           			
           case 1:  if(loc.v<14){ val[0]=DD; val[1]=RR;}
                    else { val[0]=UU; val[1]=RR;}
                    break;
                   
           case 2:  val[0]=RR; val[1]=LL; 
           			break;
         }
       
     }else {
     
        switch(rightindex){
        
            case 0: if(up){ val[0]=LL; val[1]=DD;}
            	    else  { val[0]=LL; val[1]=UU;}
            	    break;
            	    
            case 1: if(loc.v<14){ val[0]=DD; val[1]=LL;}
            		else { val[0]=UU; val[1]=LL;}
            		break;
            		
            case 2:  val[0]=LL; val[1]=RR;
                     break;
        }
      
     }
     
     
}



public boolean   DoCheckIfExistNotOpen()
{
 int i;
 
   for(i=0;i<3;i++)  if(!open[i]) return true;
   return false;

}

public int     DoGetDirNotOpen()
{
 int i;
 
    for(i=0;i<3;i++){ if(!open[i]) return i;}
    
    return -1;

}

public void      DoSetEdge()
{
      edge=false;
  
       if(leftend || rightend) edge=true;

       if(loc.h==DoGetRightEndIndex(loc)) edge=true;
       if(loc.h==0)  edge=true;
            
       if(loc.v==0 || loc.v==29) {
            if(loc.h==3  || loc.h==5  || loc.h==7 || loc.h==9 ||
               loc.h==11 || loc.h==13 || loc.h==15)   edge=true;
       }

}

public boolean   DoGetIfEdge()
{

  return edge;
}

public boolean   DoCheckIfExistEmpty()
{
 int i,j;
 
   for(i=0;i<3;i++){
      for(j=0;j<2;j++) {
        if(pieceID[i][j]==-1) return true;
      }
   }
    return false;
}

public  boolean    DoCheckIfExistSomething()
 {
  int i,j;
 
   for(i=0;i<3;i++){
      for(j=0;j<2;j++) {
        if(pieceID[i][j]!=-1) return true;
      }
   }
    return false;
 
 }
 
public boolean   DoCheckIfExistHalfEmptybyType(int halfindex[])
{
int i;

  if(leftend || rightend) { if(DoCheckIfExistSomething()) return false;}

   for(i=0;i<3;i++){
   
       if(direction[i]==halfindex[0]){
          if(pieceID[i][0]!=-1 || pieceID[i][1]!=-1) return false;
       }
      
       if(direction[i]==halfindex[1]){
          if(pieceID[i][0]!=-1 && pieceID[i][1]!=-1) return false;
       }
   } 
   
    return true;
}

public int     DoGetDirNotExistPiece()
{
 int i;
 
    for(i=0;i<3;i++){
      if(pieceID[i][0]==-1 && pieceID[i][1]==-1) return i;
    }

    return -1;
}

public int     DoGetDirExistNotExist()
{
 int i;
 
   for(i=0;i<3;i++){
   
     if((pieceID[i][0]==-1 && pieceID[i][1]!=-1) ||
        (pieceID[i][0]!=-1 && pieceID[i][1]==-1)){
        
        return direction[i];
     }
   }
   return -1;

}

public void      DoErasePieceMe(int id)
{
 int i,j;
 
   for(i=0;i<3;i++) for(j=0;j<2;j++) if(pieceID[i][j]==id) pieceID[i][j]=-1;

}

}