Added RestartEvent to clear move list after game restart

This commit is contained in:
Kai S. K. Engelbart 2019-10-30 06:12:31 +01:00
parent aa09674791
commit acf6bf36c1
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
3 changed files with 34 additions and 10 deletions

View File

@ -55,7 +55,7 @@ public class Log implements Iterable<MoveNode> {
return new Iterator<MoveNode>() { return new Iterator<MoveNode>() {
private MoveNode current = root; private MoveNode current = root;
private boolean hasNext = true; private boolean hasNext = !isEmpty();
@Override @Override
public boolean hasNext() { return hasNext; } public boolean hasNext() { return hasNext; }

View File

@ -0,0 +1,20 @@
package dev.kske.chess.event;
import dev.kske.chess.ui.GamePane;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>RestartEvent.java</strong><br>
* Created: <strong>30 Oct 2019</strong><br>
*
* @author Kai S. K. Engelbart
*/
public class RestartEvent implements Event<GamePane> {
private final GamePane gamePane;
public RestartEvent(GamePane source) { gamePane = source; }
@Override
public GamePane getData() { return gamePane; }
}

View File

@ -21,6 +21,7 @@ import dev.kske.chess.board.Piece.Color;
import dev.kske.chess.event.Event; import dev.kske.chess.event.Event;
import dev.kske.chess.event.EventBus; import dev.kske.chess.event.EventBus;
import dev.kske.chess.event.MoveEvent; import dev.kske.chess.event.MoveEvent;
import dev.kske.chess.event.RestartEvent;
import dev.kske.chess.event.Subscribable; import dev.kske.chess.event.Subscribable;
import dev.kske.chess.game.Game; import dev.kske.chess.game.Game;
import dev.kske.chess.game.NaturalPlayer; import dev.kske.chess.game.NaturalPlayer;
@ -58,7 +59,13 @@ public class GamePane extends JComponent {
JPanel toolPanel = new JPanel(); JPanel toolPanel = new JPanel();
btnRestart = new JButton("Restart"); btnRestart = new JButton("Restart");
btnRestart.addActionListener((evt) -> { if (game != null) game.reset(); game.start(); }); btnRestart.addActionListener((evt) -> {
if (game != null) {
game.reset();
game.start();
}
EventBus.getInstance().dispatch(new RestartEvent(this));
});
btnSwapColors = new JButton("Play as black"); btnSwapColors = new JButton("Play as black");
btnSwapColors.addActionListener((evt) -> { btnSwapColors.addActionListener((evt) -> {
@ -147,10 +154,9 @@ public class GamePane extends JComponent {
EventBus.getInstance().register(new Subscribable() { EventBus.getInstance().register(new Subscribable() {
// TODO: Clean on restart
@Override @Override
public void handle(Event<?> event) { public void handle(Event<?> event) {
if (game.getBoard().getLog() == null || game.getBoard().getLog().isEmpty()) return; if (game.getBoard().getLog() == null) return;
DefaultListModel<MoveNode> model = new DefaultListModel<>(); DefaultListModel<MoveNode> model = new DefaultListModel<>();
game.getBoard().getLog().forEach(node -> model.addElement(node)); game.getBoard().getLog().forEach(node -> model.addElement(node));
@ -158,9 +164,7 @@ public class GamePane extends JComponent {
} }
@Override @Override
public Set<Class<?>> supports() { public Set<Class<?>> supports() { return new HashSet<>(Arrays.asList(MoveEvent.class, RestartEvent.class)); }
return new HashSet<>(Arrays.asList(MoveEvent.class));
}
}); });
} }
@ -183,7 +187,7 @@ public class GamePane extends JComponent {
public void setGame(Game game) { public void setGame(Game game) {
if (this.game != null) this.game.stop(); if (this.game != null) this.game.stop();
this.game = game; this.game = game;
btnSwapColors.setEnabled(game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer btnSwapColors
^ game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer); .setEnabled(game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer ^ game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer);
} }
} }