16个格子对应一个4,4坐标系1,1     1,2     1,3     1,4
2,1     2,2     2,3     2,4
3,1     3,2     3,3     3,4
4,1     4,2     4,3     4,4记住当前#button的坐标,只有坐标值x+y之和,同#所在坐标X+Y之和相减的绝对值=1时(=0也不行),其对应坐标中的按钮才能够移动到#原来的位置。然后将#坐标重新变一下。这就是移动的规则。至于判断是否完成的规则就太简单了,遍历坐标系一便就成了。

解决方案 »

  1.   

    反正没事,给你写了一下,实现如下:
    程序稍改了一下,如改成JApplet了,你可再改回去。写了main方法,可直接运行。
    两按钮相邻的方法没有用 advanced(超越) 的方法,(4,1)和(2,4)和差一,显然不在一块。没有多想,用了笨办法。程序由按“#”按钮开始。我没那个本事玩通关,哈哈。import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;public class Puzzle extends JApplet implements java.awt.event.ActionListener {
        private Panel panel;
        private Button button[];    public Puzzle() {
            panel=new Panel();
            button=new Button[16];
            int i;
            for (i=0;i<button.length;i++) {
                button[i]=new Button(Integer.toString(i));
                button[i].setBackground(new Color(204,204,204));
            }
            button[0].setLabel("#");        panel.setLayout(new GridLayout(4,4));
            panel.setBackground(Color.blue);
            panel.setSize(300,300);
            getContentPane().add(panel);
            for (i=button.length-1;i>=0;i--)
                panel.add(button[i]);    }    public void init() {
    // Puzzle puzzle = new Puzzle();
            for(int i=0;i<16;i++) {
                button[i].addActionListener(this);
            }
        }    private int sharpIndex = -1;    public void actionPerformed(ActionEvent e) {
            Button b = (Button)e.getSource();
            int index = 0;
            for(int i=0;i<16;i++) {
                if(button[i].equals(b))
                    index = i;
            }        //start
            if(b.getLabel().equals("#")) {
                sharpIndex = index;
                b.setBackground(Color.red);
            } else {
                if(sharpIndex == -1 || index == sharpIndex)
                    return;
                if(isNearSharp(index)) {
                    button[sharpIndex].setBackground(new Color(204,204,204));
                    button[sharpIndex].setLabel(b.getLabel());
                    b.setBackground(Color.red);
                    b.setLabel("#");
                    sharpIndex = index;
                    checkSuccess();
                } else {
                    JOptionPane.showMessageDialog(this,"move error!","Error",JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }    private boolean isNearSharp(int clickedIndex) {
            switch (clickedIndex) {
                case 0:
                    if(sharpIndex == 1 || sharpIndex == 4)
                        return true;
                    break;
                case 1:
                    if(sharpIndex == 0 || sharpIndex == 2 || sharpIndex == 5)
                        return true;
                    break;
                case 2:
                    if(sharpIndex == 1 || sharpIndex == 3 || sharpIndex == 6)
                        return true;
                    break;
                case 3:
                    if(sharpIndex == 2 || sharpIndex == 7)
                        return true;
                    break;
                case 4:
                    if(sharpIndex == 0 || sharpIndex == 5 || sharpIndex == 8)
                        return true;
                    break;
                case 5:
                    if(sharpIndex == 1 || sharpIndex == 4 || sharpIndex == 6 || sharpIndex == 9)
                        return true;
                    break;
                case 6:
                    if(sharpIndex == 2 || sharpIndex == 5 || sharpIndex == 7 || sharpIndex == 10)
                        return true;
                    break;
                case 7:
                    if(sharpIndex == 3 || sharpIndex == 6 || sharpIndex == 11)
                        return true;
                    break;
                case 8:
                    if(sharpIndex == 4 || sharpIndex == 9 || sharpIndex == 12)
                        return true;
                    break;
                case 9:
                    if(sharpIndex == 5 || sharpIndex == 8 || sharpIndex == 10 || sharpIndex == 13)
                        return true;
                    break;
                case 10:
                    if(sharpIndex == 6 || sharpIndex == 9 || sharpIndex == 11 || sharpIndex == 14)
                        return true;
                    break;
                case 11:
                    if(sharpIndex == 7 || sharpIndex == 10 || sharpIndex == 15)
                        return true;
                    break;
                case 12:
                    if(sharpIndex == 8 || sharpIndex == 13)
                        return true;
                    break;
                case 13:
                    if(sharpIndex == 9 || sharpIndex == 12 || sharpIndex == 14)
                        return true;
                    break;
                case 14:
                    if(sharpIndex == 10 || sharpIndex == 13 || sharpIndex == 15)
                        return true;
                    break;
                case 15:
                    if(sharpIndex == 11 || sharpIndex == 14)
                        return true;
                    break;
                default:
                    break;
            }
            return false;
        }    private void checkSuccess() {
            if(button[0].getLabel().equals("#")) {
                boolean isSuccess = true;
                for(int i=1;i<16;i++) {
                    if(!button[i].getLabel().equals(""+(16-i))) {
                        isSuccess = false;
                        break;
                    }
                }
                if(isSuccess) {
                    JOptionPane.showMessageDialog(this,"game successed!","Success",JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }    public static void main(String[] args) {
            Puzzle applet = new Puzzle();
            applet.init();
            applet.start();
            JFrame frame = new JFrame();
            frame.setContentPane(applet.getContentPane());
            frame.setSize(new Dimension(900,650));
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            frame.setVisible(true);
        }
    }
      

  2.   

    to  nil2000(我爱北京天安门) 你的代码太长了,顶多用一半代码量就够了。
      

  3.   

    不长啊,就是判断相临的方法isNearSharp(int clickedIndex)长了些,你可以改写算法,应该10行搞定。 我怕想。
    我玩到最后,也像 namowen(寒号不已) 一样,有两个块倒了。好像游戏不能通关。