Consider the following class sketch:
public class Game{
public void switchPlayer(){
}
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.
source
share