Class design

I have 2 classes for the game that I am doing

gui class and logical class, for playing with notes and crosses. The GUI class has a method that uses an array of JButtons and returns them all with the same anonymous internal class action listener

The problem is that when I press the button, I want the text to change to x or ao, depending on players 1 or 2, but this code should be in the logic class, should it somehow be the method in the logical class and calling it from the internal action listener of the anon class of the make button method. However, the boolean class should not have a reference to gui, since gui refers to a boolean class,

I do not think about a worthy solution to this

thanks

+3
source share
8 answers

Consider the following class sketch:

public class Game{
    public void switchPlayer(){
        // among other things calls all GameListeners
    }
    public void setMarker(int x, int y);

    public Player getCurrentPlayer();
    public Player getPlayerOwningField(int x, int y);

    public void registerGameListener(GameListener l);
}

public interface GameListener(){
    void gameChanged(Game g)
}

public class GUI implements GameListener(){
    private Game game;
}

When you click on a field, game.setMarker (x, y) is called. The game knows who the current player is, so he can mark the field as soon as possible. It also fires the GameChanged event. During application setup, gui must register or register as a GameListener in the game. Therefore, he will also receive information. It uses game getters to update gui. Done.

Of course, there are many possibilities for clarification, for example. in a big game, it would be nice to update the full user interface just because one field has received changes, but it should start you.

+3
source

. , GUI :)

+1

, , . , . , .

0

( ), MVC (Model-View-Controller).

model = game logic, rules (fire events when things change)
  gui = (in this case and many swing apps) is both view and controller. Manipulates the model (control) and the model fires events back to update the widgets (view)
0

, (GUI ) . , , , - MVC (Model-View-Controller). , , . MVC- Java: http://blog.js-development.com/2008/03/logical-separation-with-mvc.html

, , .

0

, , , ( ) - . ( , ,...) , , .

"" . GUI , Observer, , GUI . GUI . , .

0

MVC.

UI (Controller) Logic (Model), View (Class). . , .

View, . , View , "invalidate" UI-, .

, View . , UI .

0

save event flag in database or sth. so requesting this flag in your gui works as asynchronous. developments

-2
source

Source: https://habr.com/ru/post/1704817/


All Articles