Search code examples
javaswingactionlistener

15 puzzle game in java


I am creating a 15 puzzle game in java. I have already created a game panel and written method that finds the position of the empty place (not JButton) on a field and a method that controls whether it is a winning combitanion or not (JButtons on a correct places). So, I got something like this:

enter image description here

I have a trouble with writing an ActionListener. Is it possible to write a method that determines that actionListener should be added to the JButtons that surround the empty space.

I really need some tips or/maybe already written methods/solutions.

Thanks!


Solution

  • I think your best bet would be to keep the action listener active on all the buttons. When clicked, it simply does nothing if it determines itself to not be adjacent to the empty box.