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

public class  c_tile extends Applet{

    int h = 20 , w = 35;
    Image bg;
    Graphics gr;

    int olx[] = {w * 3,w * 7,w * 9,w * 7,w * 3,w * 1,w * 3};
    int oly[] = {h * 1,h * 1,h * 7,h * 13,h * 13,h * 7,h * 1};
    int t1x[] = {w * 5,w * 8,w * 2,w * 5} , t1y[] = {h * 1,h * 10,h * 10,h * 1};
    int t2x[] = {w * 2,w * 8,w * 5,w * 2} , t2y[] = {h * 4,h *  4,h * 13,h * 4};
    int s1x[] = {w * 5,w * 6,w * 4,w * 5} , s1y[] = {h * 14,h * 17,h * 17,h * 14};
    int s2x[] = {w * 4,w * 6,w * 5,w * 4} , s2y[] = {h * 15,h * 15,h * 18,h * 15};
    int std[][] = {{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
    int tdf[][] = {{-1,-1,-1,-1},{-1,-1,-1,-1},{-1,-1,-1,-1},{-1,-1,-1,-1}};
    int tdr[][] = {{-1,-1,-1,-1},{-1,-1,-1,-1},{-1,-1,-1,-1},{-1,-1,-1,-1}};
    int pc[][] = {{1,1,1},{2,2,2},{3,3,3},{4,4,4},
                  {1,2,2},{1,3,3},{1,4,4},{2,3,3},
                  {2,4,4},{3,4,4},{1,1,2},{1,1,3},
                  {1,1,4},{2,2,3},{2,2,4},{3,3,4},
                  {1,2,3},{1,2,4},{1,3,4},{2,3,4},
                  {1,3,2},{1,4,2},{1,4,3},{2,4,3}};
    int f,n,c,t;
    int sd = 0,ss = 0,sn;
    int dx,dy,px,py,bx,by;

    public void init(){
        setBackground(Color.white);
        bg = createImage(650,400);
        gr = bg.getGraphics();
   
        for(int ix = 0;ix < 4;ix ++){
            for(int iy = 0;iy < 6;iy ++){
                if(std[iy][ix] == 1){
                    f = 1;
                }else{
                    f = 0;
                }
                Tri((ix * 2 + 11) * w,(iy * 3 +3) * h,0,f,pc[ix + iy * 4][0],pc[ix + iy * 4][1],pc[ix + iy * 4][2]);
            }
        }
        Tri(w * 5,h * 16,sd,ss,pc[sn][0],pc[sn][1],pc[sn][2]);

        addMouseListener(
            new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    dx = e.getX() ; dy = e.getY();
                    if((dx > 350) && (dx < 630) && (dy > 20) && (dy < 380)){
                        px = (dx - 350) / 70 ; py = (dy - 20) / 60;
                        if(std[py][px] == 1){
                            if(ss == 1){
                                bx = sn % 4;by = sn / 4;
                                std[by][bx] = 1;
                                Tri((bx * 2 + 11) * w,(by * 3 + 3) * h,0,1,pc[sn][0],pc[sn][1],pc[sn][2]);
                            }
                            n =  px + py * 4;
                            sn = n;ss = 1;
                            Tri(w * 5,h * 16,sd,ss,pc[sn][0],pc[sn][1],pc[sn][2]);
                            std[py][px] = 0;
                            Tri((px * 2 + 11) * w,(py * 3 + 3) * h,0,0,pc[sn][0],pc[sn][1],pc[sn][2]);
                        }else{
                            if(ss == 1){
                                bx = sn % 4;by = sn / 4;
                                std[by][bx] = 1;ss = 0;
                                Tri((bx * 2 + 11) * w,(by * 3 + 3) * h,0,1,pc[sn][0],pc[sn][1],pc[sn][2]);
                                Tri(w * 5,h * 16,sd,ss,pc[sn][0],pc[sn][1],pc[sn][2]);
                            }
                        }
                    }
                    if((dx > 140) && (dx < 210) && (dy > 280) && (dy < 360)){
                        Tri(w * 5,h * 16,sd,0,pc[sn][0],pc[sn][1],pc[sn][2]);
                        if(sd == 0){
                            sd = 1;
                        }else{
                            c = pc[sn][0];pc[sn][0] = pc[sn][2];pc[sn][2] = pc[sn][1];pc[sn][1] = c;
                            sd = 0;
                        }
                        Tri(w * 5,h * 16,sd,ss,pc[sn][0],pc[sn][1],pc[sn][2]);
                    }
                    if((dy > 20) && (dy < 260) && (12*dx-7*dy+560 > 0) && (12*dx-7*dy-2800 < 0) && (12*dx+7*dy-1400 > 0) && (12*dx+7*dy-4760 < 0)){
                        py = (dy - 20) / 60;
                        if(ss == 1){
                            if(sd == 0){
                                px = (dx - w * ((py + 1) % 2 + 1)) / 70;
                                if(tdf[py][px] > -1){
                                    t = tdf[py][px];tdf[py][px] = sn;sn = t;
                                }else{
                                    tdf[py][px] = sn;ss = 0;
                                }
                                Tri(px * 70 + ((py + 1) % 2 + 2) * w,py * 60 + 60,0,1,pc[tdf[py][px]][0],pc[tdf[py][px]][1],pc[tdf[py][px]][2]);
                                Tri(w * 5,h * 16,sd,ss,pc[sn][0],pc[sn][1],pc[sn][2]);
                            }else{
                                px = (dx - w * (py % 2 + 1)) / 70;
                                if(tdr[py][px] > -1){
                                    t = tdr[py][px];tdr[py][px] = sn;sn = t;
                                }else{
                                    tdr[py][px] = sn;ss = 0;
                                }
                                Tri(px * 70 + (py % 2 + 2) * w,py * 60 + 40,1,1,pc[tdr[py][px]][0],pc[tdr[py][px]][1],pc[tdr[py][px]][2]);
                                Tri(w * 5,h * 16,sd,ss,pc[sn][0],pc[sn][1],pc[sn][2]);
                            }
                        }else{
                            if(sd == 0){
                                px = (dx - w * ((py + 1) % 2 + 1)) / 70;
                                if(tdf[py][px] > -1){
                                    sn = tdf[py][px];tdf[py][px] = -1;ss = 1;
                                Tri(px * 70 + ((py + 1) % 2 + 2) * w,py * 60 + 60,0,0,pc[sn][0],pc[sn][1],pc[sn][2]);
                                Tri(w * 5,h * 16,sd,ss,pc[sn][0],pc[sn][1],pc[sn][2]);
                                }
                            }else{
                                px = (dx - w * (py % 2 + 1)) / 70;
                                if(tdr[py][px] > -1){
                                    sn = tdr[py][px];tdr[py][px] = -1;ss = 1;
                                Tri(px * 70 + (py % 2 + 2) * w,py * 60 + 40,1,0,pc[sn][0],pc[sn][1],pc[sn][2]);
                                Tri(w * 5,h * 16,sd,ss,pc[sn][0],pc[sn][1],pc[sn][2]);
                                }
                            } 
                        }                      
                    }
                }
            }
        );
    }

    public void paint(Graphics g){
        g.drawImage(bg,0,0,this);
        g.drawPolygon(olx,oly,7);
        g.drawPolygon(t1x,t1y,4);
        g.drawPolygon(t2x,t2y,4);
        g.drawLine(w * 3,h * 1,w * 7,h * 13);
        g.drawLine(w * 7,h * 1,w * 3,h * 13);
        g.drawLine(w * 1,h * 7,w * 9,h * 7);
        g.drawOval(125,270,100,100);
        if(sd == 0){
            g.drawPolygon(s1x,s1y,4);
        }else{
            g.drawPolygon(s2x,s2y,4);
        }
        g.drawString("<<",170,275);
        g.drawString(">>",170,375);
    }

    public void Tri(int cx , int cy , int dir , int sw , int co1 , int co2 , int co3){
   
        int h = 20 , w = 35;
        int tx[] = new int[4] , ty[] = new int[4];
        int px[] = new int[4] , py[] = new int[4];

        tx[0] = cx ; ty[0] = cy;

        if(dir == 0){
            tx[1] = tx[0] + 0 * w ; ty[1] = ty[0] - 2 * h;
            tx[2] = tx[0] - 1 * w ; ty[2] = ty[0] + 1 * h;
            tx[3] = tx[0] + 1 * w ; ty[3] = ty[0] + 1 * h;
        }else{
            tx[1] = tx[0] - 1 * w ; ty[1] = ty[0] - 1 * h;
            tx[2] = tx[0] + 0 * w ; ty[2] = ty[0] + 2 * h;
            tx[3] = tx[0] + 1 * w ; ty[3] = ty[0] - 1 * h;
        }
        px[0] = tx[0] ; py[0] = ty[0] ;
        px[1] = tx[1] ; py[1] = ty[1] ;
        px[2] = tx[2] ; py[2] = ty[2] ;
        px[3] = tx[0] ; py[3] = ty[0] ;
        if(sw > 0){
            switch(co1){
                case 1 :  gr.setColor(Color.blue);break;
                case 2 :  gr.setColor(Color.cyan);break;
                case 3 :  gr.setColor(Color.green);break;
                case 4 :  gr.setColor(Color.red);break;
            }
            gr.fillPolygon(px,py,4);
            gr.setColor(Color.black);
            gr.drawPolygon(px,py,4);
        }else{
            gr.setColor(Color.white);
            gr.fillPolygon(px,py,4);
            gr.drawLine(px[1],py[1],px[2],py[2]);
        }

        px[0] = tx[0] ; py[0] = ty[0] ;
        px[1] = tx[2] ; py[1] = ty[2] ;
        px[2] = tx[3] ; py[2] = ty[3] ;
        px[3] = tx[0] ; py[3] = ty[0] ;
        if(sw > 0){
            switch(co2){
                case 1 :  gr.setColor(Color.blue);break;
                case 2 :  gr.setColor(Color.cyan);break;
                case 3 :  gr.setColor(Color.green);break;
                case 4 :  gr.setColor(Color.red);break;
            }
            gr.fillPolygon(px,py,4);
            gr.setColor(Color.black);
            gr.drawPolygon(px,py,4);
        }else{
            gr.setColor(Color.white);
            gr.fillPolygon(px,py,4);
            gr.drawLine(px[1],py[1],px[2],py[2]);
        }

        px[0] = tx[0] ; py[0] = ty[0] ;
        px[1] = tx[3] ; py[1] = ty[3] ;
        px[2] = tx[1] ; py[2] = ty[1] ;
        px[3] = tx[0] ; py[3] = ty[0] ;
        if(sw > 0){
            switch(co3){
                case 1 :  gr.setColor(Color.blue);break;
                case 2 :  gr.setColor(Color.cyan);break;
                case 3 :  gr.setColor(Color.green);break;
                case 4 :  gr.setColor(Color.red);break;
            }
            gr.fillPolygon(px,py,4);
            gr.setColor(Color.black);
            gr.drawPolygon(px,py,4);
        }else{
            gr.setColor(Color.white);
            gr.fillPolygon(px,py,4);
            gr.drawLine(px[1],py[1],px[2],py[2]);
        }
        repaint();
    }
}