I am developing a puzzle game in java with swing. Purpose of the game to find the all the correct pairs on the board without ur lifes reaching 0 , preety simple etc.
- I have a class My_Button which extends JButton.
- My game consists of 6 type of Tiles (square , circle etc)
- All these type of tiles extend My_Button class and implements my_Actions interf.
- my_actions interface has some methods, not crucial for the question.
- I have my_board class which holds arraylist with buttons , initializes buttons, fills the array , checks for validity between pairs , delete pairs etc. It contains all the game functions in a way.
Now my main problem is the actionListener implementation and logic. In my ActionListener in simple words i add the button that got clicked to a temp array and when 2 buttons got added to this array i do certain actions. for example: (pseudo)
ActionListener{
my_board.posible_pair.add(this);
my_board.click_counter++;
my_board.check_game_state();
}
!check_game_state() runs if's and updates all the info of the game like: tries,score,check_valid_pair,delete etc.!
Now my thinking and my question laid out:
First case: If i make every Tile class implement actionListener like the example above ill have to make my_board singleton to be able to have acess to all this variables outside of the my_board class.
Second case: even if i implement actionListener inside the my_board class and add the actionlistener through anonymous classes to my button after i add them to the board it gives error ofc because the scope gets weird.
Third case: full static class and variables, its really bad programming so id like to avoid.
QUESTION:
--How i can reconstruct my project to manage these problem without singletons/static ?
--How i can avoid putting the check_game_state() inside the ActionListener which also is a bad practice?
I checked infinite number of qestions on this forum , google , stackoverflow but nothing really helps on the specifics. I have developed simmilar games before but i always follow the singleton tecqniques and id like to change that.
button1.actionListenerReference.addPair(this)
? And you are trying to decrease this tight coupling without using singletons or statics? If I am understanding correctly I think the architectural approach I would recommend is using a pub/sub event driven system. You will be able to communicate via events between the button and action listener without tight coupling. Let me know if I'm understanding correctly or if I have made some misinterpretations/wrong assumptions. \$\endgroup\$