8-puzzle Java方法

时间:2015-01-08 12:11:35

标签: java swing applet 8-puzzle

我无法在我的代码中获取我的shuffling方法的逻辑,我的图像方法idk我需要做些什么来解决这个问题。我几乎尝试了所有的东西,但总是有例外。

我的isFinished()方法几乎正常工作,但它有一个小问题,当我赢了,我需要点击按钮获取消息。

以下是我的代码:

public class Puzzle extends JFrame implements ActionListener
{
        private JPanel centerPanel = new JPanel();
        private JPanel northPanel = new JPanel();
        private JPanel southPanel = new JPanel();
        private JButton newGame = new JButton("New Game"); 
        private JButton[] btArray = new JButton[8];
        private JLabel moves = new JLabel("Moves : ");
        private JLabel label;
        private Container mainPanel = this.getContentPane();
        private int[][] pos;
        private int count;
        private String str;

        int width, height;

        public Puzzle() {
            super("Puzzle Game");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            initUI();
        }
        public void initUI() {
            pos = new int[][]{
                    {0, 1, 2},
                    {3, 4, 5},
                    {6, 7, 8},
                        {9,10,11}
                };
            /** Initialize button array */
            for (int i = 0; i < 8; i++)
            {
                 //   image = createImage(new FilteredImageSource(source.getSource(),
                   //     new CropImageFilter(i*width/3, i*height/4, 
                     //       (width/3)+1, height/4)));
                     // btArray.setIcon(new ImageIcon(image));
            btArray[i] = new JButton("B" + i);
//            btArray[i].setIcon(new ImageIcon(image));
         }
            /** North Panel */
            newGame.setFocusable(false);
            newGame.addActionListener(this);
            northPanel.setBackground(Color.red);
            northPanel.add(newGame);
            mainPanel.add(northPanel, BorderLayout.NORTH);

            /** South Panel */
            southPanel.add(moves);
            southPanel.setBackground(Color.LIGHT_GRAY);
            mainPanel.add(southPanel, BorderLayout.SOUTH);

            /** Game Panel */
        centerPanel.setLayout(new GridLayout(3, 3, 0, 0));
            centerPanel.setBackground(Color.BLACK);
            /** Add actionListeners to buttons */
            for (int i = 0; i < 8; i++)
        {
                btArray[i].addActionListener(this);
        }
            newGame();
        mainPanel.revalidate();
        label = new JLabel("");
            label.setBackground(Color.BLACK);
            centerPanel.add(label);
        mainPanel.add(centerPanel);
        }
        public void newGame()
        {
           count = 0;
            for (int j = 7; j >= 0; j--)
            {
                centerPanel.remove(btArray[j]);
            }
            for (int j = 7; j >= 0; j--)
            {
                centerPanel.add(btArray[j]);
            }
            centerPanel.revalidate();
        }
        public boolean isFinished() {
            if ((btArray[0].getY() == btArray[1].getY() && btArray[1].getY() == btArray[2].getY()) && (btArray[3].getY() == btArray[4].getY() && btArray[4].getY() == btArray[5].getY())
                    && (btArray[6].getY() == btArray[7].getY()))
            {
                if (btArray[0].getX() == btArray[3].getX() && btArray[3].getX() == btArray[6].getX()
                        && btArray[1].getX() == btArray[4].getX() && btArray[4].getX() == btArray[7].getX()
                    && btArray[2].getX() == btArray[5].getX())
                {
                    return true;
                }
        }

            return false;
        }
            }
        public static void main(String[] args)
        {
        Puzzle puz = new Puzzle();
            puz.setBounds(20, 20, 300, 325);
            puz.setVisible(true);
        }
        public void actionPerformed(ActionEvent ae)
        {
            JButton button = (JButton) ae.getSource();
            Dimension size = button.getSize();

            if (isFinished())
            {
            JOptionPane.showMessageDialog(null, "You have Won the game");
            }

            if (ae.getSource() == newGame)
            {
                newGame();
            }

            int labelX = label.getX();
            int labelY = label.getY();
            int buttonX = button.getX();
       int buttonY = button.getY();
            int buttonPosX = buttonX / size.width;
            int buttonPosY = buttonY / size.height;
       int buttonIndex = pos[buttonPosY][buttonPosX];

       if (labelX == buttonX && (labelY - buttonY) == size.height) {

                int labelIndex = buttonIndex + 3;

                centerPanel.remove(buttonIndex);
                centerPanel.add(label, buttonIndex);
                centerPanel.add(button, labelIndex);
                centerPanel.validate();
                count++;

            }

            if (labelX == buttonX && (labelY - buttonY) == -size.height) {

                int labelIndex = buttonIndex - 3;
                centerPanel.remove(labelIndex);
                centerPanel.add(button, labelIndex);
                centerPanel.add(label, buttonIndex);
                centerPanel.validate();
                count++;

            }

        if (labelY == buttonY && (labelX - buttonX) == size.width) {

                int labelIndex = buttonIndex + 1;

                centerPanel.remove(buttonIndex);
            centerPanel.add(label, buttonIndex);
                centerPanel.add(button, labelIndex);
           centerPanel.validate();
           count++;

            }

            if (labelY == buttonY && (labelX - buttonX) == -size.width) {

                int labelIndex = buttonIndex - 1;

                centerPanel.remove(buttonIndex);
                centerPanel.add(label, labelIndex);
                centerPanel.add(button, labelIndex);
                centerPanel.validate();
                count++;
            }

        str = String.valueOf(count);
            moves.setText("Moves : " + str);
        }
    }

改组的方法,但我得到一个错误,我无法将我的按钮转换为int

 public void shuffle()
     {
         int aux[]   =   new int[8];
         int i, j, counter = 0;
         for(i=0; i<16; i++)
         {
             aux[i]  =   (int)(Math.random()*15);
         }
         for(i=0; i<8; i++)
         {
             for(j=0; j<i; j++)
             {
                 if(aux[i]==aux[j])
                 {
                     aux[i]  =   (int)(Math.random()*16);
                     j=-1;
                }
            }
         }
         for(i=0; i<7; i++)
         {
                                  btArray[i] =   aux[counter];
                 counter++;

         }

对于我的图片,我试过这段代码,但没有工作

ImageIcon sid = new ImageIcon(PuzzleBoard.class.getResource("flower.jpg"));
            centerPanel = new JPanel();
            centerPanel.setLayout(new GridLayout(4, 4, 0, 0));
            source = sid.getImage();
            width = sid.getIconWidth();
            height = sid.getIconHeight();
   for ( int j = 0; j < 7; j++) {
                                    button = new JButton();
                    button.addActionListener(this);
                    centerPanel.add(button);
                    image = createImage(new FilteredImageSource(source.getSource(),
                        new CropImageFilter(j*width/3, i*height/4, 
                            (width/3)+1, height/4)));
                    btArray[j].setIcon(new ImageIcon(image));
                }

1 个答案:

答案 0 :(得分:0)

Simpelest解决方案是将JButton存储在List而不是数组中,并使用Collections.shuffle(List<T>)方法。

如果无法将它们存储在List中,您可以查看Fisher-Yates shuffle方法:

public static <T> void shuffle(T[] array) {
    Random random = new Random();

    for (int i = array.length - 1; i >= 1; --i) {
        int j = random.nextInt(i+1);
        T temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}

修改

我最好的猜测是要解决这个问题,你需要点击以获得“你赢了”的消息,你应该在执行动作结束时检查玩家是否赢了,而不是在开始时动作。