Implemented LogFrame updating

+ Export to SAN in move
+ Updating LogFrame after a move
- Turned EventBus into a singleton
This commit is contained in:
Kai S. K. Engelbart 2019-08-04 14:40:25 +02:00
parent 65e75053d7
commit 1923dbf27a
4 changed files with 24 additions and 6 deletions

View File

@ -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; }

View File

@ -18,7 +18,14 @@ public final class EventBus {
private Map<Class<?>, List<Invocation>> invocations;
public EventBus() {
private static EventBus instance;
public static EventBus getInstance() {
if (instance == null) instance = new EventBus();
return instance;
}
private EventBus() {
invocations = new ConcurrentHashMap<>();
}

View File

@ -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:

View File

@ -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: <strong>Chess</strong><br>
@ -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<LoggedMove> 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" }));
}