public class Game
extends java.lang.Object
Game objects use the Observer pattern: each Game has an associated list
of GameListener
objects. Whenever the state of the game changes
(for example, if a player makes a move), the Game calls the gameChanged
method on each of its observers.
This gives a very flexible way for different components of the code to react to changes. It is easy to create add code to automatically log moves as they are made, for example. The view and controller classes both observe the model: the view should be updated if the board contains new information, while the controller may wish to prompt the user for input if it has become their turn.
Constructor and Description |
---|
Game()
Create a game with an empty board; X goes first.
|
Game(Player p)
Create a game with an empty board; p goes first.
|
Modifier and Type | Method and Description |
---|---|
void |
addListener(@NonNull GameListener listener)
Add a listener to this game: causes the listener to be notified now and
at any point in the future when this game's state changes.
|
Board |
getBoard() |
@NonNull Player |
nextTurn()
Return the player who should play next.
|
void |
submitMove(@NonNull Player p,
@NonNull Location loc)
Update the game in response to player p playing in location loc.
|
public Game(Player p)
public Game()
public void addListener(@NonNull GameListener listener)
public @NonNull Player nextTurn()
public void submitMove(@NonNull Player p, @NonNull Location loc) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
- if p is not currently allowed to play
in location loc (for example if it is not p's turn, or if loc has
already been played)public Board getBoard()