From acf6bf36c1c20ffa937d7acadfbd447e3f211563 Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 30 Oct 2019 06:12:31 +0100 Subject: [PATCH] Added RestartEvent to clear move list after game restart --- src/dev/kske/chess/board/Log.java | 2 +- src/dev/kske/chess/event/RestartEvent.java | 20 ++++++++++++++++++++ src/dev/kske/chess/ui/GamePane.java | 22 +++++++++++++--------- 3 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 src/dev/kske/chess/event/RestartEvent.java diff --git a/src/dev/kske/chess/board/Log.java b/src/dev/kske/chess/board/Log.java index c2da70b..d7754ff 100644 --- a/src/dev/kske/chess/board/Log.java +++ b/src/dev/kske/chess/board/Log.java @@ -55,7 +55,7 @@ public class Log implements Iterable { return new Iterator() { private MoveNode current = root; - private boolean hasNext = true; + private boolean hasNext = !isEmpty(); @Override public boolean hasNext() { return hasNext; } diff --git a/src/dev/kske/chess/event/RestartEvent.java b/src/dev/kske/chess/event/RestartEvent.java new file mode 100644 index 0000000..bbcf232 --- /dev/null +++ b/src/dev/kske/chess/event/RestartEvent.java @@ -0,0 +1,20 @@ +package dev.kske.chess.event; + +import dev.kske.chess.ui.GamePane; + +/** + * Project: Chess
+ * File: RestartEvent.java
+ * Created: 30 Oct 2019
+ * + * @author Kai S. K. Engelbart + */ +public class RestartEvent implements Event { + + private final GamePane gamePane; + + public RestartEvent(GamePane source) { gamePane = source; } + + @Override + public GamePane getData() { return gamePane; } +} diff --git a/src/dev/kske/chess/ui/GamePane.java b/src/dev/kske/chess/ui/GamePane.java index 8d537d2..5faac02 100644 --- a/src/dev/kske/chess/ui/GamePane.java +++ b/src/dev/kske/chess/ui/GamePane.java @@ -21,6 +21,7 @@ import dev.kske.chess.board.Piece.Color; import dev.kske.chess.event.Event; import dev.kske.chess.event.EventBus; import dev.kske.chess.event.MoveEvent; +import dev.kske.chess.event.RestartEvent; import dev.kske.chess.event.Subscribable; import dev.kske.chess.game.Game; import dev.kske.chess.game.NaturalPlayer; @@ -58,7 +59,13 @@ public class GamePane extends JComponent { JPanel toolPanel = new JPanel(); 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.addActionListener((evt) -> { @@ -130,7 +137,7 @@ public class GamePane extends JComponent { letterLabel.setHorizontalAlignment(JLabel.CENTER); letterPanel.add(letterLabel); } - + JScrollPane scrollPane = new JScrollPane(); GridBagConstraints gbc_scrollPane = new GridBagConstraints(); gbc_scrollPane.fill = GridBagConstraints.BOTH; @@ -147,10 +154,9 @@ public class GamePane extends JComponent { EventBus.getInstance().register(new Subscribable() { - // TODO: Clean on restart @Override public void handle(Event event) { - if (game.getBoard().getLog() == null || game.getBoard().getLog().isEmpty()) return; + if (game.getBoard().getLog() == null) return; DefaultListModel model = new DefaultListModel<>(); game.getBoard().getLog().forEach(node -> model.addElement(node)); @@ -158,9 +164,7 @@ public class GamePane extends JComponent { } @Override - public Set> supports() { - return new HashSet<>(Arrays.asList(MoveEvent.class)); - } + public Set> supports() { return new HashSet<>(Arrays.asList(MoveEvent.class, RestartEvent.class)); } }); } @@ -183,7 +187,7 @@ public class GamePane extends JComponent { public void setGame(Game game) { if (this.game != null) this.game.stop(); this.game = game; - btnSwapColors.setEnabled(game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer - ^ game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer); + btnSwapColors + .setEnabled(game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer ^ game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer); } }