//------------------ myTypeUtility Class -------------------//

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

public class myTypeUtility
{
 final int     RR=0,  LL=1, DD= 2,UU =3;



public  void   doAddThisType(myPieceType type,int index,myPieceTypeClass gPieceType[])
{
 int       i,totalnum;
 
    totalnum=gPieceType[index].totalnum;
    
    for(i=0;i<5;i++){
       TP_DoRotateType(type,totalnum);
       TP_DoSortmyType(type,totalnum);
       TP_DomyCopyType(type,gPieceType[index].mytype[i+1]);
    }
 
}

public void  TP_DomyCopyType(myPieceType src,myPieceType dst)
{
 int i;
 
    for(i=0;i<12;i++){
       dst.loc[i].h=src.loc[i].h;
       dst.loc[i].v=src.loc[i].v;
       dst.halfindex[i][0]=src.halfindex[i][0];
       dst.halfindex[i][1]=src.halfindex[i][1];
       dst.up[i]=src.up[i];
       dst.full[i]=src.full[i];
     }

}

//------------ Check Match -----------//

public boolean TP_DoCheckMatchCell(myPieceType type,int index,int cellid,myCell gCell[])
{
 boolean up,half;
 int     val[];
 
        val=new int[2];
 
        up=gCell[cellid].DoGetUP();
        half=gCell[cellid].DoGetLeftOrRightEnd();
  
        if(up && !(type.up[index])) return false;
        if(!up && (type.up[index])) return false;
        if(half && (type.full[index])) return false;
        
        if(half && !(type.full[index])){
        
             gCell[cellid].DoGetCellHalfType(val);
             if(val[0]!=type.halfindex[index][0])  return false;
             if(val[1]!=type.halfindex[index][1])  return false;
        }
   
        return true;

}

//-------------- Sort -------------//

void    TP_DoSortmyType(myPieceType type,int totalN)
{
 int     i,j,num,minx,miny;
 myPoint p0=new myPoint(),p1=new myPoint();
 
     num=totalN;
      
     for(i=0;i< num-1;i++){
       for(j=i+1;j< num;j++){
       
         p0.h=type.loc[i].h; p0.v=type.loc[i].v;
         p1.h=type.loc[j].h; p1.v=type.loc[j].v;
         
         if(p1.v< p0.v) doswap(type,i,j);
         else if(p1.v==p0.v && p1.h< p0.h) doswap(type,i,j);
       
       }
     }
    
    minx=100; miny=100;
    for(i=0;i< num;i++){
       p0.h=type.loc[i].h;
       p0.v=type.loc[i].v;
       if(p0.h< minx) minx=p0.h;
       if(p0.v< miny) miny=p0.v;
    }
    for(i=0;i< num;i++) {  type.loc[i].h -=minx; type.loc[i].v -=miny; }
 
}

private void doswap(myPieceType type,int id0,int id1)
{
  myPoint   loc=new myPoint();
  int       halfindex[];
  boolean   up,full;
  
      halfindex=new int[2];

     loc.h=type.loc[id0].h;
     loc.v=type.loc[id0].v;
     halfindex[0]=type.halfindex[id0][0];
     halfindex[1]=type.halfindex[id0][1];
     up=type.up[id0];
     full=type.full[id0];
     
     type.loc[id0].h=type.loc[id1].h;
     type.loc[id0].v=type.loc[id1].v;
     type.halfindex[id0][0]=type.halfindex[id1][0];
     type.halfindex[id0][1]=type.halfindex[id1][1];
     type.up[id0]    =type.up[id1];
     type.full[id0]  =type.full[id1];
     
     type.loc[id1].h=loc.h;
     type.loc[id1].v=loc.v;
     type.halfindex[id1][0]=halfindex[0];
     type.halfindex[id1][1]=halfindex[1];
     type.up[id1]    =up;
     type.full[id1]  =full;
  
}

//-------------- Invert -------------//

public void   TP_DoCreateInvertType(myPieceType type,int Num)
{
  int i;
  
     for(i=0;i< Num;i++)  type.loc[i].h=-(type.loc[i].h);

     for(i=0;i< Num;i++){
         if(type.full[i]) continue;
         type.halfindex[i][0]=domyInverthalf(type.halfindex[i][0]);
         type.halfindex[i][1]=domyInverthalf(type.halfindex[i][1]);
     }
     
       TP_DoSortmyType(type,Num);
}
 
private  int domyInverthalf(int index)
{
 int dst=index;
 
    switch(index){
       case LL: dst=RR; break;
       case RR: dst=LL; break;
     }
     return dst;
}

//------------- Rotate --------------//

public void   TP_DoRotateType(myPieceType type,int Num)
{
 int      i,sx;
 myPoint   p0=new myPoint();
 boolean   up;
 
     p0.h=type.loc[0].h;
     p0.v=type.loc[0].v;
     sx=p0.h;  sx= sx & 0x0001;
     
     up=type.up[0];
     if(sx==1) up = !up;
    
    if(up) doRotateUp(type,Num);
    else   doRotateDown(type,Num);
    
    for(i=0;i< Num;i++){
       if(type.full[i]) continue;
       
        type.halfindex[i][0]=doGetRotHalfIndex(type.up[i],type.halfindex[i][0]);
        type.halfindex[i][1]=doGetRotHalfIndex(type.up[i],type.halfindex[i][1]);
    }
    
    for(i=0;i< Num;i++)  type.up[i]=!(type.up[i]);
    
}


private void doRotateUp(myPieceType type,int Num)
{
 int i,x=0,y=0;
 
    for(i=0;i< Num;i++){
     
        switch(type.loc[i].v){
        
            case 0: switch(type.loc[i].h){
                        case 0: y=0; x=0; break;
                        case 1: y=0; x=1; break;
                        case 2: y=1; x=1; break;
                        case 3: y=1; x=2; break;
                        case 4: y=2; x=2; break;
                        case 5: y=2; x=3; break;
                        case 6: y=3; x=3; break;
                        case 7: y=3; x=4; break;
                        case 8: y=4; x=4; break;
                    }
                    break;
                    
            case 1:  switch(type.loc[i].h){
                        case 0: y=0; x=-1; break;
                        case 1: y=1; x=-1; break;
                        case 2: y=1; x=0; break;
                        case 3: y=2; x=0; break;
                        case 4: y=2; x=1; break;
                        case 5: y=3; x=1; break;
                        case 6: y=3; x=2; break;
                        case 7: y=4; x=2; break;
                        case 8: y=4; x=3; break;
                    }
                    break;
                    
            case 2:  switch(type.loc[i].h){
                        case 0: y=1; x=-3; break;
                        case 1: y=1; x=-2; break;
                        case 2: y=2; x=-2; break;
                        case 3: y=2; x=-1; break;
                        case 4: y=3; x=-1; break;
                        case 5: y=3; x=0; break;
                        case 6: y=4; x=0; break;
                        case 7: y=4; x=1; break;
                        case 8: y=5; x=1; break;
                    }
                    break;
                    
                    
            case 3:  switch(type.loc[i].h){
                        case 0: y=1; x=-4; break;
                        case 1: y=2; x=-4; break;
                        case 2: y=2; x=-3; break;
                        case 3: y=3; x=-3; break;
                        case 4: y=3; x=-2; break;
                        case 5: y=4; x=-2; break;
                        case 6: y=4; x=-1; break;
                        case 7: y=5; x=-1; break;
                        case 8: y=5; x=0; break;
                    }
                    break;
   
           case 4:  switch(type.loc[i].h){
                        case 0: y=2; x=-6; break;
                        case 1: y=2; x=-5; break;
                        case 2: y=3; x=-5; break;
                        case 3: y=3; x=-4; break;
                        case 4: y=4; x=-4; break;
                        case 5: y=4; x=-3; break;
                        case 6: y=5; x=-3; break;
                        case 7: y=5; x=-2; break;
                        case 8: y=6; x=-2; break;
                    }
                    break;
                    
            case 5:  switch(type.loc[i].h){
                        case 0: y=2; x=-7; break;
                        case 1: y=3; x=-7; break;
                        case 2: y=3; x=-6; break;
                        case 3: y=4; x=-6; break;
                        case 4: y=4; x=-5; break;
                        case 5: y=5; x=-5; break;
                        case 6: y=5; x=-4; break;
                        case 7: y=6; x=-4; break;
                        case 8: y=6; x=-3; break;
                    }
                    break;
                    
            case 6:  switch(type.loc[i].h){
                        case 0: y=3; x=-9; break;
                        case 1: y=3; x=-8; break;
                        case 2: y=4; x=-8; break;
                        case 3: y=4; x=-7; break;
                        case 4: y=5; x=-7; break;
                        case 5: y=5; x=-6; break;
                        case 6: y=6; x=-6; break;
                        case 7: y=6; x=-5; break;
                        case 8: y=7; x=-5; break;
                    }
                    break;
                    
            case 7:  switch(type.loc[i].h){
                        case 0: y=3; x=-10; break;
                        case 1: y=4; x=-10; break;
                        case 2: y=4; x=-9; break;
                        case 3: y=5; x=-9; break;
                        case 4: y=5; x=-8; break;
                        case 5: y=6; x=-8; break;
                        case 6: y=6; x=-7; break;
                        case 7: y=7; x=-7; break;
                        case 8: y=7; x=-6; break;
                    }
                    break;


        }
        
        type.loc[i].h=x;
        type.loc[i].v=y;
    }


}


private  void doRotateDown(myPieceType type,int Num)
{
 int i,x=0,y=0;
 
    for(i=0;i< Num;i++){
     
        switch(type.loc[i].v){
        
            case 0: switch(type.loc[i].h){
                        case 0: y=0; x=0; break;
                        case 1: y=1; x=0; break;
                        case 2: y=1; x=1; break;
                        case 3: y=2; x=1; break;
                        case 4: y=2; x=2; break;
                        case 5: y=3; x=2; break;
                        case 6: y=3; x=3; break;
                        case 7: y=4; x=3; break;
                        case 8: y=4; x=4; break;
                    }
                    break;
                    
            case 1:  switch(type.loc[i].h){
                        case 0: y=1; x=-2; break;
                        case 1: y=1; x=-1; break;
                        case 2: y=2; x=-1; break;
                        case 3: y=2; x=0; break;
                        case 4: y=3; x=0; break;
                        case 5: y=3; x=1; break;
                        case 6: y=4; x=1; break;
                        case 7: y=4; x=2; break;
                        case 8: y=5; x=2; break;
                    }
                    break;
                    
            case 2:  switch(type.loc[i].h){
                        case 0: y=1; x=-3; break;
                        case 1: y=2; x=-3; break;
                        case 2: y=2; x=-2; break;
                        case 3: y=3; x=-2; break;
                        case 4: y=3; x=-1; break;
                        case 5: y=4; x=-1; break;
                        case 6: y=4; x=0; break;
                        case 7: y=5; x=0; break;
                        case 8: y=5; x=1; break;
                    }
                    break;
                    
                    
            case 3:  switch(type.loc[i].h){
                        case 0: y=2; x=-5; break;
                        case 1: y=2; x=-4; break;
                        case 2: y=3; x=-4; break;
                        case 3: y=3; x=-3; break;
                        case 4: y=4; x=-3; break;
                        case 5: y=4; x=-2; break;
                        case 6: y=5; x=-2; break;
                        case 7: y=5; x=-1; break;
                        case 8: y=6; x=-1; break;
                    }
                    break;
   
           case 4:  switch(type.loc[i].h){
                        case 0: y=2; x=-6; break;
                        case 1: y=3; x=-6; break;
                        case 2: y=3; x=-5; break;
                        case 3: y=4; x=-5; break;
                        case 4: y=4; x=-4; break;
                        case 5: y=5; x=-4; break;
                        case 6: y=5; x=-3; break;
                        case 7: y=6; x=-3; break;
                        case 8: y=6; x=-2; break;
                    }
                    break;
                    
            case 5:  switch(type.loc[i].h){
                        case 0: y=3; x=-8; break;
                        case 1: y=3; x=-7; break;
                        case 2: y=4; x=-7; break;
                        case 3: y=4; x=-6; break;
                        case 4: y=5; x=-6; break;
                        case 5: y=5; x=-5; break;
                        case 6: y=6; x=-5; break;
                        case 7: y=6; x=-4; break;
                        case 8: y=7; x=-4; break;
                    }
                    break;
                    
            case 6:  switch(type.loc[i].h){
                        case 0: y=3; x=-9; break;
                        case 1: y=4; x=-9; break;
                        case 2: y=4; x=-8; break;
                        case 3: y=5; x=-8; break;
                        case 4: y=5; x=-7; break;
                        case 5: y=6; x=-7; break;
                        case 6: y=6; x=-6; break;
                        case 7: y=7; x=-6; break;
                        case 8: y=7; x=-5; break;
                    }
                    break;
                    
            case 7:  switch(type.loc[i].h){
                        case 0: y=4; x=-11; break;
                        case 1: y=4; x=-10; break;
                        case 2: y=5; x=-10; break;
                        case 3: y=5; x=-9; break;
                        case 4: y=6; x=-9; break;
                        case 5: y=6; x=-8; break;
                        case 6: y=7; x=-8; break;
                        case 7: y=7; x=-7; break;
                        case 8: y=8; x=-7; break;
                    }
                    break;


        }
        
        type.loc[i].h=x;
        type.loc[i].v=y;
    }


}

private int doGetRotHalfIndex(boolean up,int index)
{
 int dst=0;
 
  if(up){
  
     switch(index){
        case LL: dst=UU; break;
        case RR: dst=RR; break;
        case DD: dst=LL; break;
     }
   
  }  else {
    
      switch(index){
         case LL: dst=LL; break;
         case RR: dst=DD; break;
         case UU: dst=RR; break;
      }
   }

   return dst;
}
}