简体   繁体   中英

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:

在此处输入图片说明

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!

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.

I would recommend ditching the idea of only adding actionListeners to the buttons that surround the empty space: keep actionListeners added to all buttons and only process a click if it is valid.

Also, in my experience, it is very user-hostile to only allow a click on one of the tiles that surround the empty space. In the example picture, you are planning to only allow a click on 7, 1, and 11. You should allow a click on 5, 7, 1, 13, 14 and 11. So, for example a click on 14 should move 1, 13 and 14 to the left.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM