Gui and actionListener question...

Hello, i figured this should be pretty simple, but i can’t seem to get my head around it… I’m making a somewhat simple card-base game.

So, i have this class Gui() that draws a swing gui and also a method UpdateGui(Player p1, Player p2) inside the Gui class that updates the Gui depending on the players’ cards and board position.

There is a button “Draw” and “Discard” that, i hope, should be self-explanatory… The problem is that my Draw and Discard listeners are in the Gui class, although when they are clicked, they should call, say, p1.DrawCard() or p1.Discard(), but my Gui class has no access to the players’ methods.

So, i’m not sure where i messed up in my design, but how should i have done the thing so that the listeners are independent from the Gui? What the best way of doing something like this?

Model / View / Controller

Controller handles input (the button presses)

Model handles game state (the players’ current hands)

View displays everything.

Have a look at Wikipedia:

In my opinion, strict MVC is not as important for most games and apps as it is for websites and database-driven apps, however it’s a good place to start from.

Your view should contain a reference to the model so it can access the model’s state as it draws. The model represents your players’ hands, so your Gui class should in your case contain references to the players. The controller should be the class that implements ActionListener. It will have references to both the view and the model, so when a button is pressed it will update the model from that press, and then tell the view to refresh from the new model state (or the view can have some sort of state changed listener).

Take a look at the diagram on the Wikipedia page.

Thanks! Exactly what i was looking for :slight_smile: