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

public class WacharoPazzle2 extends Applet implements MouseListener,ActionListener {

    int [] x,y;
    int count,startX;
    int size,pnlSize,width,height,nullx,nully;
    int a,b,s,t,k,m,n;
    int block=3;
    final int speed=10;
    int seed;
    int [][] num,start;
    boolean flagX,flagY,plusFlag,btnFlag,endFlag;
    Image img;
    Graphics2D offGr;
    Shape rect;
    Button btn1,btn2;
    Thread th;
    RandomPanel ran;
    Check chk;
    String strS,strL;
    ArrayList array,arrayCopy;
    Integer integer;
    
    Image wacharoImg,gameClearImg;
    
    int imgX[][] = new int 5][5];
    int imgY[][] = new int 5][5];
    int offimgX[][] = new int 5][5];
    int offimgY[][] = new int 5][5];
    
    public void init() {
        setSize(300,400);
        setLayout(null);
        setBackground(Color.LIGHT_GRAY);
        
        width = getWidth();
        height = getHeight();
        if (width > height) {
            size = height;
        } else {
            size = width;
        }
        pnlSize = size / block;
        
        btn1 = new Button("スタート");
        btn1.setBounds(60,350,60,30);
        btn1.addActionListener(this);
        add(btn1);
        
        btn2 = new Button("リセット");
        btn2.setBounds(180,350,60,30);
        btn2.addActionListener(this);
        add(btn2);
        
        clear();
    }
    
    public void clear() {
        flagX=false;
        flagY=false;
        plusFlag=false;
        btnFlag=false;
        endFlag=false;
        
        x = new int[block + 1];
        y = new int[block + 1];

        num = new int[block + 2][block + 2];
        start = new int[block + 2][block + 2];
        for (int i = 1; i < block+1; i++) {
            x[i] = (i - 1) * pnlSize;
            y[i] = (i - 1) * pnlSize;
        }
        
        for(int j=1;j<block+1;j++) {
            for(int i=1;i<block+1;i++) {
                imgX[i][j] = pnlSize*(i-1);
                imgY[i][j] = pnlSize*(j-1);
                offimgX[i][j] = pnlSize*(i-1);
                offimgY[i][j] = pnlSize*(j-1);
            }
        }
        img = createImage(300,300);
        offGr = (Graphics2D)img.getGraphics();
        wacharoImg = getImage(getDocumentBase(),"image/WacharoImageMovie1.gif");
        gameClearImg = getImage(getDocumentBase(),"image/GameClear.gif");
        addMouseListener(this);
    }
    
    public void update(Graphics g) {
        paint(g);
    }
    public void paint(Graphics Gr) {
        offGr.setColor(Color.DARK_GRAY);
        offGr.fillRect(0,0,size,size);
        showStatus("ワチャロパズル");
        
        if(!endFlag) {
            for(int j=1;j<block+1;j++) {
                for(int i=1;i<block+1;i++) {
                    if(num[i][j]!=block*block) {
                        if(num[i][j]==num[s][t]&&(flagX||flagY)&&btnFlag) {
                            if(plusFlag) {
                                if(flagX) {x[s]+=++m*5;imgX[s][t]+=5;}
                                if(flagY) {y[t]+=++n*5;imgY[s][t]+=5;}
                            } else {
                                if(flagX) {x[s]+=--m*5;imgX[s][t]-=5;}
                                if(flagY) {y[t]+=--n*5;imgY[s][t]-=5;}
                            }
                        }
                        
                        offGr.drawImage(wacharoImg,imgX[i][j],imgY[i][j],imgX[i][j]+pnlSize,imgY[i][j]+pnlSize,offimgX[i][j],offimgY[i][j],offimgX[i][j]+pnlSize,offimgY[i][j]+pnlSize,this);
                        
                        rect = new Rectangle2D.Double(x[i],y[j],pnlSize,pnlSize);
                        Stroke st = offGr.getStroke();
                        offGr.setColor(Color.black);
                        if(btnFlag) {
                            offGr.fill(st.createStrokedShape(rect));
                        }
                        
                        if(flagX&&x[s]==nullx) {
                            flagX=false;
                            x[s]-=m*5;
                            nullx=x[s];
                            Change.change(num,s,j,a,b);
                            Change.change(imgX,s,j,a,b);
                            Change.change(offimgX,s,j,a,b);
                            Change.change(imgY,i,t,a,b);
                            Change.change(offimgY,i,t,a,b);
                            m=0;
                        }
                        if(flagY&&y[t]==nully) {
                            flagY=false;
                            y[t]-=n*5;
                            nully=y[t];
                            Change.change(num,i,t,a,b);
                            Change.change(imgX,s,j,a,b);
                            Change.change(offimgX,s,j,a,b);
                            Change.change(imgY,i,t,a,b);
                            Change.change(offimgY,i,t,a,b);
                            n=0;
                        }
                        
                        if(num[i][j]==num[s][t]&&(flagX||flagY)) {x[s]-=m*5;y[t]-=n*5;}
                        
                    }
                    if(num[i][j]==block*block) {
                        nullx=x[i];
                        nully=y[j];
                        a=i;b=j;
                    }
                }
            }
        }
        
        if(endFlag) {
            offGr.setColor(Color.DARK_GRAY);
            offGr.fillRect(0,0,size,size);
            offGr.drawImage(gameClearImg,20,50,this);
        }
        Gr.drawLine(0,size,size,size);
        Gr.drawImage(img,0,0,this);
        
        if(btnFlag&&!flagX&&!flagY&&chk.currentCheck(num,block)) {
            clear();
            endFlag=true;
        }
    }
    
    public void mouseClicked(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mousePressed(MouseEvent e) {
        int xx=0,yy=0;
        xx=e.getX();
        yy=e.getY();
        if(btnFlag && !flagX && !flagY && !endFlag) {
            if(xx<nullx || xx>nullx+pnlSize || yy<nully || yy>nully+pnlSize) {
                for(int j=1;j<4;j++) {
                    for(int i=1;i<4;i++) {
                        if(xx>x[i] && xx<x[i]+pnlSize && yy>y[j] && yy<y[j]+pnlSize) {
                            if(num[i+1][j]==block*block||num[i-1][j]==block*block||num[i][j+1]==block*block||num[i][j-1]==block*block) {
                                if(a!=i) {flagX=true;s=i;t=j;}
                                if(b!=j) {flagY=true;s=i;t=j;}
                                if(num[i+1][j]==block*block||num[i][j+1]==block*block) {
                                    plusFlag=true;
                                } else {
                                    plusFlag=false;
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    public void mouseReleased(MouseEvent e) {
//         TODO 自動生成されたメソッド・スタブ
    }

    /* (非 Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent e) {
        // TODO 自動生成されたメソッド・スタブ
        boolean bool = false;
        int kk=0;
        int k=0;
        if(e.getSource().equals(btn1) && !btnFlag && !endFlag) {
            while(!bool) {
                showStatus("しばらくお待ちください。");
                ran = new RandomPanel();
                ran.setSeed(++seed + (new Date()).getTime());
                chk = new Check(block);
                array = ran.random(block * block -1);
                
                arrayCopy = (ArrayList)array.clone();
                strS = chk.toString(arrayCopy,block);
                
                count = chk.sort(arrayCopy,block);
                
                strL = chk.toString(arrayCopy,block);
                if(count%2==0 && strL.equals(chk.getLast())) {
                    bool=true;
                }
                
                System.out.println(count);
                System.out.println(strS);
                System.out.println(strL);
            }
            chk.OK_FLAG = true;
            for(int j=1;j<block+1;j++) {
                for(int i=1;i<block+1;i++) {
                    if(kk!=block*block-1) {
                        num[i][j] = ((Integer)array.get(kk++)).intValue();
                    } else {
                        num[i][j] = block*block;
                    }
                    System.out.println(num[i][j]);
                }
            }
            
            startX = 0;
            for (int j = 1; j < block + 1; j++) {
                for (int i = 1; i < block + 1; i++) {
                    k++;
                    if (i != block || j != block) {
                        integer = (Integer) array.get(k - 1);
                        start[i][j] = integer.intValue();

                        for (int z = 0; z < block; z++) {
                            if (start[i][j] > block * z
                                && start[i][j] < block * (z + 1) + 1) {
                                startX = start[i][j] - block * z;
                            }
                        }
                        offimgX[i][j] = (startX-1) * pnlSize;
                        offimgY[i][j] = (start[i][j]-1)/block * pnlSize;
                    }
                }
            }

            btnFlag = true;
            
        }
        
        if(e.getSource().equals(btn2)) {
            clear();
        }
    }
}
		

top

3077