From e7af9f40c2782b82fd6b48c339e0f47e0dca8451 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 4 Aug 2019 14:40:25 +0200 Subject: [PATCH] Implemented LogFrame updating + Export to SAN in move + Updating LogFrame after a move - Turned EventBus into a singleton --- src/dev/kske/chess/board/Move.java | 4 ++++ src/dev/kske/chess/event/EventBus.java | 9 ++++++++- src/dev/kske/chess/game/Game.java | 2 ++ src/dev/kske/chess/ui/LogFrame.java | 15 ++++++++++----- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/dev/kske/chess/board/Move.java b/src/dev/kske/chess/board/Move.java index 06d7ba9..a781361 100644 --- a/src/dev/kske/chess/board/Move.java +++ b/src/dev/kske/chess/board/Move.java @@ -35,6 +35,10 @@ public class Move { Position.fromSAN(move.substring(2))); } + public String toSAN() { + return pos.toSAN() + dest.toSAN(); + } + public boolean isHorizontal() { return yDist == 0; } public boolean isVertical() { return xDist == 0; } diff --git a/src/dev/kske/chess/event/EventBus.java b/src/dev/kske/chess/event/EventBus.java index f9351aa..15a7d15 100644 --- a/src/dev/kske/chess/event/EventBus.java +++ b/src/dev/kske/chess/event/EventBus.java @@ -18,7 +18,14 @@ public final class EventBus { private Map, List> invocations; - public EventBus() { + private static EventBus instance; + + public static EventBus getInstance() { + if (instance == null) instance = new EventBus(); + return instance; + } + + private EventBus() { invocations = new ConcurrentHashMap<>(); } diff --git a/src/dev/kske/chess/game/Game.java b/src/dev/kske/chess/game/Game.java index e7552a1..dc30852 100644 --- a/src/dev/kske/chess/game/Game.java +++ b/src/dev/kske/chess/game/Game.java @@ -9,6 +9,7 @@ import dev.kske.chess.board.Board; import dev.kske.chess.board.GameState; import dev.kske.chess.board.Move; import dev.kske.chess.board.Piece.Color; +import dev.kske.chess.event.EventBus; import dev.kske.chess.game.ai.AIPlayer; import dev.kske.chess.ui.BoardComponent; import dev.kske.chess.ui.BoardPane; @@ -65,6 +66,7 @@ public class Game { System.out.printf("%s: %s%n", player.color, move); System.out.println("FEN: " + board.toFEN()); + EventBus.getInstance().post(move); GameState eventType = board.getGameEventType(board.getDest(move).getColor().opposite()); switch (eventType) { case CHECKMATE: diff --git a/src/dev/kske/chess/ui/LogFrame.java b/src/dev/kske/chess/ui/LogFrame.java index c903764..6d32bb0 100644 --- a/src/dev/kske/chess/ui/LogFrame.java +++ b/src/dev/kske/chess/ui/LogFrame.java @@ -12,6 +12,9 @@ import javax.swing.table.DefaultTableModel; import dev.kske.chess.board.Log; import dev.kske.chess.board.Log.LoggedMove; +import dev.kske.chess.board.Move; +import dev.kske.chess.event.EventBus; +import dev.kske.chess.event.Subscribe; /** * Project: Chess
@@ -45,16 +48,18 @@ public class LogFrame extends JFrame { mtable = new JTable(); mtable.setEnabled(false); mcontentPane.add(new JScrollPane(mtable), BorderLayout.CENTER); - update(); + + EventBus.getInstance().register(this); + EventBus.getInstance().post(new Move(0, 0, 0, 0)); } - // TODO: call this method after a move - public void update() { + @Subscribe + public void updateData(Move move) { final List moves = log.getLoggedMoves(); String[][] data = new String[moves.size() / 2 + moves.size() % 2][2]; for (int i = 0; i < data.length; i++) { - data[i][0] = moves.get(i * 2).move.toString(); - if (i < data.length - 1) data[i][1] = moves.get(i * 2 + 1).move.toString(); + data[i][0] = moves.get(i * 2).move.toSAN(); + if (i * 2 + 1 < moves.size()) data[i][1] = moves.get(i * 2 + 1).move.toSAN(); } mtable.setModel(new DefaultTableModel(data, new String[] { "White", "Black" })); }