0

So I have been creating a board game(Reversi). My game has a 2d array of enum (WHITE BLACK NONE) created as a board, and read the information from the 2d array and translate it into JButtons GUI. Now, my problem is, how to connect those buttons to the 2d array? How to determine which button is clicked, and which enum in the 2d array should change. I've been thinking about the solution and I got some approaches that might be able to work. But I want to know which approaches is the fastest and most efficient.

  1. Create another 2d array containing JButtons. In the actionListener, loop through each JButton in the JButtons 2d array. Change the value of enum board relate to the row/column of JButtons.

  2. When creating a JButton, use setName method, for example myButton.setName("2;4") which refer to the board array(row2 column4), and decrypt it in the actionListener.

  3. Create new class that extends JButton, this class will have an additional attribute which is the state of the button (WHITE, BLACK, NONE), and create 2d array of this class instead of the enum.

Please tell me the pros and cons of each solution, and please tell me the best (not of this 3) solution that most of developers used for doing such things.

  • You'll need to [profile](http://stackoverflow.com/q/2064427/230513) to be sure, but [this approach](http://stackoverflow.com/a/7706684/230513) is _O(1)_. – trashgod Nov 01 '15 at 12:30
  • @Arc676 this question is a poor fit for Programmers - it would be quickly voted down and closed over there, see [What is the problem with “Pros and Cons”?](http://meta.programmers.stackexchange.com/q/6758/31260) Recommended reading: **[What goes on Programmers.SE? A guide for Stack Overflow](http://meta.programmers.stackexchange.com/q/7182/31260)** – gnat Nov 01 '15 at 12:55
  • I use actionCommand instead of name in #2. The cost of decoding the content is not high. and as @trashgod mentioned, it is O(1) – Soley Nov 01 '15 at 13:30

0 Answers0