0

i can't get the logic of my shuffling method in my code also my image method idk what I need to do for fix this. I have try almost everything but Always I get exceptions.

My isFinished() method is almost working but there is one little problem with it, when I have won, I need click on a button for get the message.

Following is my code:

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

method for shuffling but i got a error that i can't convert my button to 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++;

         }

and for my images i have try this code but didn't work

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));
                }
Sarz
  • 1,970
  • 4
  • 23
  • 43
jan
  • 1
  • 1

1 Answers1

0

Simpelest solution would be to store the JButton in a List instead of an array and use the Collections.shuffle(List<T>) method.

If storing them in a List is not an option, you can look at the Fisher-Yates shuffle method:

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

Edit:

My best guess to fix the problem with the fact that you need to click to get the "You have won" message, you should check at the end of performing an action whether the player has won, not at the start of an action.

Niels Billen
  • 2,189
  • 11
  • 12
  • i gues my positions are not changing but now i get or when i complete the game not a message at all. or i get the message when the tiles are in a incorrect position. miabe there is another solution for get my message i have create the message with if() – jan Jan 08 '15 at 14:53