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

public class c_four extends Applet{

    Button rep;
    int btd[][] = {{0,0,0,0,0,0},
                   {0,1,1,1,2,0},
                   {0,1,1,2,1,0},
                   {0,1,2,1,1,0},
                   {0,2,1,1,1,0},
                   {0,0,0,0,0,0}};
    int mtd[][] = new int[6][6];
    Image ball[] = new Image[2];
    Image arr[] = new Image[4];
    int dx,dy;
    Color bg = new Color(200,200,255);

    public void init(){

        setBackground(Color.white);

        ball[0] = getImage(getDocumentBase(),"Picture/ball2.gif");
        ball[1] = getImage(getDocumentBase(),"Picture/ball1.gif");
        arr[0] = getImage(getDocumentBase(),"Picture/are.gif");
        arr[1] = getImage(getDocumentBase(),"Picture/arn.gif");
        arr[2] = getImage(getDocumentBase(),"Picture/arw.gif");
        arr[3] = getImage(getDocumentBase(),"Picture/ars.gif");
        for(int px = 0;px < 6;px ++){
            for(int py = 0;py < 6;py ++){
                mtd[py][px] = btd[py][px];
            }
        }
        setLayout(new BorderLayout());
        Panel pan = new Panel();
        pan.setLayout(new FlowLayout());
        pan.add(rep = new Button("  replay  "));
        add("South",pan);
        rep.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                for(int px = 0;px < 6;px ++){
                    for(int py = 0;py < 6;py ++){
                        mtd[py][px] = btd[py][px];
                    }
                }
                repaint();
            }
        });

        addMouseListener(
            new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    dx = (e.getX() - 20) / 40;
                    dy = (e.getY() - 20) / 40;
                    if((dx > 1) && (dx < 6)){
                        if(dy == 0){
                            if((mtd[1][dx - 1] != 0) && (mtd[2][dx - 1] != 0) 
                                      && (mtd[3][dx - 1] != 0) && (mtd[4][dx - 1] != 0)){
                                mtd[0][dx - 1] = mtd[1][dx - 1];
                                mtd[1][dx - 1] = mtd[2][dx - 1];
                                mtd[2][dx - 1] = mtd[3][dx - 1];
                                mtd[3][dx - 1] = mtd[4][dx - 1];
                                mtd[4][dx - 1] = 0;
                                repaint(1,40 * dx + 20,60,40,200);
                            }
                            if((mtd[2][dx - 1] != 0) && (mtd[3][dx - 1] != 0)
                                       && (mtd[4][dx - 1] != 0) && (mtd[5][dx - 1] != 0)){
                                mtd[1][dx - 1] = mtd[2][dx - 1];
                                mtd[2][dx - 1] = mtd[3][dx - 1];
                                mtd[3][dx - 1] = mtd[4][dx - 1];
                                mtd[4][dx - 1] = mtd[5][dx - 1];
                                mtd[5][dx - 1] = 0;
                                repaint(1,40 * dx + 20,100,40,200);
                            }
                        }
                        if(dy == 7){
                            if((mtd[1][dx - 1] != 0) && (mtd[2][dx - 1] != 0) 
                                      && (mtd[3][dx - 1] != 0) && (mtd[4][dx - 1] != 0)){
                                mtd[5][dx - 1] = mtd[4][dx - 1];
                                mtd[4][dx - 1] = mtd[3][dx - 1];
                                mtd[3][dx - 1] = mtd[2][dx - 1];
                                mtd[2][dx - 1] = mtd[1][dx - 1];
                                mtd[1][dx - 1] = 0;
                                repaint(1,40 * dx + 20,100,40,200);
                            }
                            if((mtd[0][dx - 1] != 0) && (mtd[1][dx - 1] != 0)
                                       && (mtd[2][dx - 1] != 0) && (mtd[3][dx - 1] != 0)){
                                mtd[4][dx - 1] = mtd[3][dx - 1];
                                mtd[3][dx - 1] = mtd[2][dx - 1];
                                mtd[2][dx - 1] = mtd[1][dx - 1];
                                mtd[1][dx - 1] = mtd[0][dx - 1];
                                mtd[0][dx - 1] = 0;
                                repaint(1,40 * dx + 20,60,40,200);
                            }
                        }
                    }
                    if((dy > 1) && (dy < 6)){
                        if(dx == 0){
                            if((mtd[dy - 1][1] != 0) && (mtd[dy - 1][2] != 0) 
                                      && (mtd[dy - 1][3] != 0) && (mtd[dy - 1][4] != 0)){
                                mtd[dy - 1][0] = mtd[dy - 1][1];
                                mtd[dy - 1][1] = mtd[dy - 1][2];
                                mtd[dy - 1][2] = mtd[dy - 1][3];
                                mtd[dy - 1][3] = mtd[dy - 1][4];
                                mtd[dy - 1][4] = 0;
                                repaint(1,60,40 * dy + 20,200,40);
                            }
                            if((mtd[dy - 1][2] != 0) && (mtd[dy - 1][3] != 0) 
                                      && (mtd[dy - 1][4] != 0) && (mtd[dy - 1][5] != 0)){
                                mtd[dy - 1][1] = mtd[dy - 1][2];
                                mtd[dy - 1][2] = mtd[dy - 1][3];
                                mtd[dy - 1][3] = mtd[dy - 1][4];
                                mtd[dy - 1][4] = mtd[dy - 1][5];
                                mtd[dy - 1][5] = 0;
                                repaint(1,100,40 * dy + 20,200,40);
                            }
                        }
                        if(dx == 7){
                            if((mtd[dy - 1][1] != 0) && (mtd[dy - 1][2] != 0) 
                                      && (mtd[dy - 1][3] != 0) && (mtd[dy - 1][4] != 0)){
                                mtd[dy - 1][5] = mtd[dy - 1][4];
                                mtd[dy - 1][4] = mtd[dy - 1][3];
                                mtd[dy - 1][3] = mtd[dy - 1][2];
                                mtd[dy - 1][2] = mtd[dy - 1][1];
                                mtd[dy - 1][1] = 0;
                                repaint(1,100,40 * dy + 20,200,40);
                            }
                            if((mtd[dy - 1][0] != 0) && (mtd[dy - 1][1] != 0) 
                                      && (mtd[dy - 1][2] != 0) && (mtd[dy - 1][3] != 0)){
                                mtd[dy - 1][4] = mtd[dy - 1][3];
                                mtd[dy - 1][3] = mtd[dy - 1][2];
                                mtd[dy - 1][2] = mtd[dy - 1][1];
                                mtd[dy - 1][1] = mtd[dy - 1][0];
                                mtd[dy - 1][0] = 0;
                                repaint(1,60,40 * dy + 20,200,40);
                            }
                        }
                    }
                }
            }
        );

    }

    public void paint(Graphics g){
        for(int ix = 0;ix < 6;ix ++){
            for(int iy = 0;iy < 6;iy ++){
                if(mtd[iy][ix] > 0){
                    g.setColor(bg);
                    g.fillRect(ix * 40 + 60,iy * 40 + 60,40,40);
                    g.drawImage(ball[mtd[iy][ix]-1],ix * 40 + 60,iy * 40 + 60,this);
                }
            }
        }
        for(int ax = 0;ax < 4;ax ++){
            g.setColor(Color.darkGray);
            g.draw3DRect(ax * 40 + 100,60,40,240,true);
            g.draw3DRect(60,ax * 40 + 100,240,40,true);
            g.drawImage(arr[0],300,ax * 40 + 100,this);
            g.drawImage(arr[1],ax * 40 + 100,20,this);
            g.drawImage(arr[2],20,ax * 40 + 100,this);
            g.drawImage(arr[3],ax * 40 + 100,300,this);
        }
        g.setColor(Color.red);
        g.draw3DRect(140,140,80,80,true);
        g.draw3DRect(141,141,78,78,true);
    }
}