0

So, im working on my slide puzzel game, which have 16 JButtons in a gridlayout. 15 of them have a number 1-15 and the last one is empty. I have now written the code for the gui and everything except the public void actionPerformed(ActionEvent e). This is how it looks:(the gui)

https://i.stack.imgur.com/rbTt6.jpg

enter image description here

(Sorry for not posting picture here, I cannot do that since I dont have enough reputation)

For example, when i click the "4" button, it should change like it did in the picture to the right. To change this, I know how to, but to check horizontally and vertically if the "empty button" is next to it I have no idea how to. Ive tried to google it, but I havnt found anything to help me. How should I do this? I am not asking for you guys to write code for me, no, I am asking how should I tackle this problem?

THank you

mKorbel
  • 109,525
  • 20
  • 134
  • 319
UserFuser
  • 110
  • 12

1 Answers1

0

Steps to follow:

  • Find the index of empty cell
  • Find the index of current clicked cell
  • Get left, right, top and bottom index of current clicked cell
  • Put a logic to validate a logical move

     if (emptyIndex == left || emptyIndex == right || emptyIndex == top
                    || emptyIndex == bottom) {
                JButton emptyBtn = btns.get(emptyIndex);
                emptyBtn.setText(btn.getText());
                btn.setText("");
     }
    
  • Swap the text of empty and current clicked cell if its a valid move
  • That' all

Here is a code for you.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Puzzle {
    private static List<JButton> btns = new ArrayList<JButton>();

    static class MyButtonActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            JButton btn = (JButton) e.getSource();

            int emptyIndex = -1;
            int currentInex = -1;
            for (int i = 0; i < btns.size(); i++) {
                if (btns.get(i).getText().equals("")) {
                    emptyIndex = i;
                } else if (btns.get(i).getText().equals(btn.getText())) {
                    currentInex = i;
                }
            }

            int left = currentInex - 1;
            int right = currentInex + 1;
            int top = currentInex - 4;
            int bottom = currentInex + 4;

            if (emptyIndex == left || emptyIndex == right || emptyIndex == top
                    || emptyIndex == bottom) {
                JButton emptyBtn = btns.get(emptyIndex);
                emptyBtn.setText(btn.getText());
                btn.setText("");
            }
        }
    }

    public static void main(String[] a) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 4));

        for (int i = 0; i < 15; i++) {
            JButton btn = new JButton(String.valueOf(i));
            btn.addActionListener(new MyButtonActionListener());
            btns.add(btn);
        }
        JButton empty = new JButton("");
        empty.addActionListener(new MyButtonActionListener());
        btns.add(empty);

        Collections.shuffle(btns);
        for (JButton btn : btns) {
            panel.add(btn);
        }

        frame.setTitle("The 15 game");
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
Braj
  • 46,415
  • 5
  • 60
  • 76