0
\$\begingroup\$

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.

  1. I have a class My_Button which extends JButton.
  2. My game consists of 6 type of Tiles (square , circle etc)
  3. All these type of tiles extend My_Button class and implements my_Actions interf.
  4. my_actions interface has some methods, not crucial for the question.
  5. 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.

\$\endgroup\$
  • \$\begingroup\$ Is the ActionListener receiving communication from the buttons via direct reference eg: 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\$ – disc_code22 Jan 9 at 14:40
1
\$\begingroup\$

i would suggest that you do some work on your design - it is a very good idea to rethink your design to make better (less bugs) 🐜 , better to test & maintain and even adding new features.

refactoring into dynamic context

You can move your whole static code into a dynamic context once you start your static main method

public class Game {

    //static context
    public static void main(String[] args){
        Game myGame = new Game();
        game.executeDynamicMethod();
    }

    //dynamic context
    public void executeDynamicMethod(){
    }

}

planning tools

furthermore make a plan what entities should be in your game and what relations they have. Best practice for these plans is usually to use UML diagrams

enter image description here

Use this (or a better suitable) Model to create your classes.

Design hints

Lastly you should try to seperate concerns. Most likely you have a game entity, a so called game model and a GUI to display that game. keep these two things separate as far as you can!

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.