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

/**
 * @author ワチャロー
 *
 * この生成されたコメントの挿入されるテンプレートを変更するため
 * ウィンドウ > 設定 > Java > コード生成 > コードとコメント
 */
public class Pazzle9 extends Applet implements Runnable, MouseListener ,ActionListener,ItemListener {

    private boolean flagX, flagY, p,btnFlag;
    private int size, pnlSize, nullx, nully, width, height;
    private int a, b, k, m, n, s, t, aa, bb;
    private int[] x, y;
    private int[][] cirX, cirY, num,start;
    private int startX,count;
    private long seed;

    private Area areaRect, area1, area2;
    private Stroke st;
    private Shape rect;
    private Image img;
    private Thread th;
    private Graphics2D offGr;
    private Button btn1,btn2;
    private Checkbox cb1,cb2;
    private CheckboxGroup cbg;
    private ArrayList array,arrayCopy;
    private Integer integer,in;
    private RandomPanel ran;
    private Check chk;
    private String strS,strL;

    private final int speed = 10;
    private final int speedPanel = 10;
    private int block = 3;
    private final int speedCircle = 1;

    /* (非 Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void init() {
        setSize(300,400);
        setLayout(null);

        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);
        
        cbg = new CheckboxGroup();
        
        cb1 = new Checkbox("3×3", cbg, true);
        cb2 = new Checkbox("5×5", cbg, false);
        
        cb1.setBounds(60,320,60,30);
        cb2.setBounds(180,320,60,30);
        
        cb1.addItemListener(this);
        cb2.addItemListener(this);
        
        add(cb1);
        add(cb2);
                
        width = getWidth();
        height = getHeight();
        if (width > height) {
            size = height;
        } else {
            size = width;
        }

        clear();
    }
    
    public void clear() {
        flagX = false;
        flagY = false;
        p=false;
        a=0; b=0; k=0; m=0; n=0; s=0; t=0; aa=0; bb=0;
        
        pnlSize = size / block;
        if (pnlSize % speedPanel != 0) {
            System.out.println("画面サイズをパネル数で割った値がパネル移動量で割り切れるように設定して下さい");
            throw new RuntimeException("画面サイズとパネル数が一致しません!!");
        }
        bb = size / 2;
        x = new int[block + 1];
        y = new int[block + 1];
        cirX = new int[block + 1][block + 1];
        cirY = new int[block + 1][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;
        }
        
        img = createImage(size, size);
        offGr = (Graphics2D) img.getGraphics();
        addMouseListener(this);
    }
    
    public void start() {
        if (th == null) {
            th = new Thread(this);
        }
    }
    public void update(Graphics g) {
        paint(g);
    }
    public void paint(Graphics Gr) {
        offGr.clearRect(0,0,size,size);
        showStatus("スライドパズル");
        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)) {
                        if(p) {
                            if(flagX) {x[s]+=++m*speedPanel;cirX[s][t]+=speedPanel;}
                            if(flagY) {y[t]+=++n*speedPanel;cirY[s][t]+=speedPanel;}
                        } else {
                            if(flagX) {x[s]+=--m*speedPanel;cirX[s][t]-=speedPanel;}
                            if(flagY) {y[t]+=--n*speedPanel;cirY[s][t]-=speedPanel;}
                        }
                    }
                    offGr.setColor(Color.gray);
                    rect = new Rectangle2D.Double(x[i],y[j],pnlSize,pnlSize);
                    st = offGr.getStroke();
                    areaRect = new Area(rect);
                    offGr.fill(areaRect);
                    
                    offGr.setColor(Color.black);
                    offGr.fill(st.createStrokedShape(areaRect));
                    
                    area1 = Circle.circle(areaRect,cirX[i][j],cirY[i][j],aa,size);
                    area2 = Circle.circle(areaRect,cirX[i][j],cirY[i][j],bb,size);
                    
                    if(flagX&&x[s]==nullx) {
                        flagX=false;
                        x[s]-=m*speedPanel;
                        nullx=x[s];
                        Change.change(num,s,j,a,b);
                        Change.change(cirX,s,j,a,b);
                        Change.change(cirY,i,t,a,b);
                        m=0;
                    }
                    if(flagY&&y[t]==nully) {
                        flagY=false;
                        y[t]-=n*speedPanel;
                        nully=y[t];
                        Change.change(num,i,t,a,b);
                        Change.change(cirX,s,j,a,b);
                        Change.change(cirY,i,t,a,b);
                        n=0;
                    }
                    
                    if(num[i][j]==num[s][t]&&(flagX||flagY)) {x[s]-=m*speedPanel;y[t]-=n*speedPanel;}
                    
                    if(aa>=size) aa=0;
                    if(bb>=size) bb=0;
                    
                    offGr.setPaint(new GradientPaint(0,0,Color.yellow,size,size,Color.green));
                    offGr.fill(area1);
                    offGr.fill(area2);
                }
                if(num[i][j]==block*block) {
                    nullx=x[i];
                    nully=y[j];
                    a=i;b=j;
                }
            }
        }
        Gr.drawImage(img,0,0,this);
    }
    public void run() {
        // TODO 自動生成されたメソッド・スタブ
        while(th == Thread.currentThread()) {
            try {
                Thread.sleep(speed);
            } catch (InterruptedException e) {
            }
            repaint();
            aa += speedCircle;
            bb += speedCircle;
        }
    }

    /* (非 Javadoc)
     * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
     */
    public void mouseClicked(MouseEvent e) {
        // TODO 自動生成されたメソッド・スタブ
    }

    /* (非 Javadoc)
     * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
     */
    public void mouseEntered(MouseEvent e) {
        // TODO 自動生成されたメソッド・スタブ

    }

    /* (非 Javadoc)
     * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
     */
    public void mouseExited(MouseEvent e) {
        // TODO 自動生成されたメソッド・スタブ

    }

    /* (非 Javadoc)
     * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
     */
    public void mousePressed(MouseEvent e) {
        // TODO 自動生成されたメソッド・スタブ
        int xx = 0, yy = 0;
        xx = e.getX();
        yy = e.getY();
        if (!flagX && !flagY) {
            if (xx<nullx || xx>nullx+pnlSize || yy<nully || yy>nully+pnlSize) {
                for (int j = 1; j < block + 1; j++) {
                    for (int i = 1; i < block + 1; 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) {
                                    p = true;
                                } else {
                                    p = false;
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    /* (非 Javadoc)
     * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
     */
    public void mouseReleased(MouseEvent e) {
        // TODO 自動生成されたメソッド・スタブ

    }
    
    public void actionPerformed(ActionEvent e) {
        boolean bool = false;
        if(e.getSource().equals(btn1) && !btnFlag) {
            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);
        //        bool = chk.check(arrayCopy,block);
            }

            
            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;
                            }
                        }

                        cirX[i][j] = (k - startX - (j - 1) * block) * pnlSize;
                                    
                        cirY[i][j] = ((k - 1) / block
                        - (start[i][j] - 1) / block)
                        * pnlSize;
                                    
                    }
                    num[i][j] = k;
                }
            }
            if(th == null) {
                th = new Thread(this);
            }
            th.start();
            btnFlag = true;
        }
        
        if(e.getSource().equals(btn2) && btnFlag) {
            btnFlag = false;
            clear();
            repaint();
            th = null;
        }
    }
    
    public void itemStateChanged(ItemEvent e) {
        if(cb1 == e.getSource() && !btnFlag) {
            block = 3;
            clear();
            repaint();
        }
        
        if(cb2==e.getSource() && !btnFlag) {
            block = 5;
            clear();
            repaint();
        }
        
        if(cb1 == e.getSource() && btnFlag) {
            if(block == 5) {
                cb2.setState(true); 
            }
        }
        
        if(cb2 == e.getSource() && btnFlag) {
            if(block == 3) {
                cb1.setState(true); 
            }
        }
    }
}
		

Change.java
public class Change {
    public static void change(int num[][],int a,int b,int c,int d) {
        int temp;
        temp = num[a][b];
        num[a][b] = num[c][d];
        num[c][d] = temp;
    }
}
		

Circle.java
import java.awt.*;
import java.awt.geom.*;

public class Circle {
    
    static Area circle(Area area,int x,int y,int a,int size) {
        final int wide = 20;
        int b = a-wide;
        int aX = x+size/2-a/2;
        int aY = y+size/2-a/2;
        int bX = aX+10;
        int bY = aY+10;
        Shape shp1 = new Ellipse2D.Double(aX,aY,a,a);
        Shape shp2 = new Ellipse2D.Double(bX,bY,b,b);
        Area area1 = new Area(shp1);
        Area area2 = new Area(shp2);
     // area1.exclusiveOr(area2);
        area1.subtract(area2);
        area1.intersect(area);
        return area1;
    }
}
		

RandomPanel.java
import java.util.*;

public class RandomPanel extends Random {
    ArrayList array;
    
    public RandomPanel() {
        array = new ArrayList();
    }
    
    public ArrayList random(int num) {
        int a = num;
        for(int i = 0; i < num; i++) {
            a = nextInt(num)+1;
            while(array.contains(new Integer(a))) {
                a = nextInt(num)+1;
            }
             array.add(new Integer(a));
        }
        return array;
    }
}
    

Check.java
import java.util.*;

public class Check {
    
    private ArrayList arrayL;
    private Integer integer;
    private String last;
    private int number,count;
    
    public Check() {
    }
    
    public Check(int block) {
        arrayL = new ArrayList();
        for(int i=1;i<=block*block;i++) {
            arrayL.add(new Integer(i));
        }
        last = toString(arrayL,block);
    }
    
    public String toString(ArrayList array,int block) {
        StringBuffer strBuf = new StringBuffer();
        for(int i=0;i<block*block-1;i++) {
            strBuf.append("s");
            strBuf.append(array.get(i));
            if(i%block==block-1) {
                strBuf.append("s#");
            }
            if(i==block*block-2) {
                strBuf.append("s");
                strBuf.append(block*block);
            }
        }
        return strBuf.toString();
    }
    
    public int sort(ArrayList array,int block) {
        for(int j=0;j<block;j++) {
            for(int i=0;i<block*block-1;i++) {
                number = ((Integer)array.get(i)).intValue();
                if(i+1 != number) {
                    integer = (Integer)array.get(number-1);
                    array.set(number-1,new Integer(number));
                    array.set(i,integer);
                    count++;
                }
            }
        }
        return count;
    }
    
    public String getLast() {
        return last;    
    }
}
    

top

2820