diff --git a/src/dev/kske/chess/board/Board.java b/src/dev/kske/chess/board/Board.java
index fbbf2bd..327e7e3 100644
--- a/src/dev/kske/chess/board/Board.java
+++ b/src/dev/kske/chess/board/Board.java
@@ -255,9 +255,9 @@ public class Board {
}
}
- public GameState getGameEventType(Color color) {
- return checkCheck(color) ? checkCheckmate(color) ? GameState.CHECKMATE : GameState.CHECK
- : getMoves(color).isEmpty() || log.getLast().halfmoveClock >= 50 ? GameState.STALEMATE : GameState.NORMAL;
+ public BoardState getGameEventType(Color color) {
+ return checkCheck(color) ? checkCheckmate(color) ? BoardState.CHECKMATE : BoardState.CHECK
+ : getMoves(color).isEmpty() || log.getLast().halfmoveClock >= 50 ? BoardState.STALEMATE : BoardState.NORMAL;
}
/**
diff --git a/src/dev/kske/chess/board/GameState.java b/src/dev/kske/chess/board/BoardState.java
similarity index 72%
rename from src/dev/kske/chess/board/GameState.java
rename to src/dev/kske/chess/board/BoardState.java
index 635a235..b8db707 100644
--- a/src/dev/kske/chess/board/GameState.java
+++ b/src/dev/kske/chess/board/BoardState.java
@@ -2,12 +2,12 @@ package dev.kske.chess.board;
/**
* Project: Chess
- * File: GameState.java
+ * File: BoardState.java
* Created: 07.07.2019
*
* @since Chess v0.1-alpha
* @author Kai S. K. Engelbart
*/
-public enum GameState {
+public enum BoardState {
CHECK, CHECKMATE, STALEMATE, NORMAL;
}
diff --git a/src/dev/kske/chess/event/GameStartEvent.java b/src/dev/kske/chess/event/GameStartEvent.java
new file mode 100644
index 0000000..4097550
--- /dev/null
+++ b/src/dev/kske/chess/event/GameStartEvent.java
@@ -0,0 +1,21 @@
+package dev.kske.chess.event;
+
+import dev.kske.chess.game.Game;
+
+/**
+ * Project: Chess
+ * File: GameStartEvent.java
+ * Created: 30 Oct 2019
+ *
+ * @since Chess v0.5-alpha
+ * @author Kai S. K. Engelbart
+ */
+public class GameStartEvent implements Event {
+
+ private final Game game;
+
+ public GameStartEvent(Game source) { game = source; }
+
+ @Override
+ public Game getData() { return game; }
+}
diff --git a/src/dev/kske/chess/event/MoveEvent.java b/src/dev/kske/chess/event/MoveEvent.java
index 92eafe6..a60a785 100644
--- a/src/dev/kske/chess/event/MoveEvent.java
+++ b/src/dev/kske/chess/event/MoveEvent.java
@@ -1,5 +1,6 @@
package dev.kske.chess.event;
+import dev.kske.chess.board.BoardState;
import dev.kske.chess.board.Move;
/**
@@ -13,11 +14,15 @@ import dev.kske.chess.board.Move;
public class MoveEvent implements Event {
private final Move move;
+ private final BoardState boardState;
- public MoveEvent(Move move) {
+ public MoveEvent(Move move, BoardState boardState) {
this.move = move;
+ this.boardState = boardState;
}
@Override
public Move getData() { return move; }
+
+ public BoardState getBoardState() { return boardState; }
}
diff --git a/src/dev/kske/chess/event/RestartEvent.java b/src/dev/kske/chess/event/RestartEvent.java
deleted file mode 100644
index bbcf232..0000000
--- a/src/dev/kske/chess/event/RestartEvent.java
+++ /dev/null
@@ -1,20 +0,0 @@
-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/game/Game.java b/src/dev/kske/chess/game/Game.java
index edaf6d8..2005cd0 100644
--- a/src/dev/kske/chess/game/Game.java
+++ b/src/dev/kske/chess/game/Game.java
@@ -6,10 +6,11 @@ import java.util.Map;
import javax.swing.JOptionPane;
import dev.kske.chess.board.Board;
-import dev.kske.chess.board.GameState;
+import dev.kske.chess.board.BoardState;
import dev.kske.chess.board.Move;
import dev.kske.chess.board.Piece.Color;
import dev.kske.chess.event.EventBus;
+import dev.kske.chess.event.GameStartEvent;
import dev.kske.chess.event.MoveEvent;
import dev.kske.chess.game.ai.AIPlayer;
import dev.kske.chess.io.EngineUtil;
@@ -82,12 +83,12 @@ public class Game {
// Run garbage collection
System.gc();
- EventBus.getInstance().dispatch(new MoveEvent(move));
- GameState eventType = board.getGameEventType(board.getDest(move).getColor().opposite());
- switch (eventType) {
+ BoardState boardState = board.getGameEventType(board.getDest(move).getColor().opposite());
+ EventBus.getInstance().dispatch(new MoveEvent(move, boardState));
+ switch (boardState) {
case CHECKMATE:
case STALEMATE:
- String result = String.format("%s in %s!%n", player.color.opposite(), eventType);
+ String result = String.format("%s in %s!%n", player.color.opposite(), boardState);
System.out.print(result);
JOptionPane.showMessageDialog(boardComponent, result);
break;
@@ -100,6 +101,7 @@ public class Game {
}
public void start() {
+ EventBus.getInstance().dispatch(new GameStartEvent(this));
players.get(board.getLog().getActiveColor()).requestMove();
}
diff --git a/src/dev/kske/chess/ui/GamePane.java b/src/dev/kske/chess/ui/GamePane.java
index 5faac02..7f891d2 100644
--- a/src/dev/kske/chess/ui/GamePane.java
+++ b/src/dev/kske/chess/ui/GamePane.java
@@ -16,12 +16,13 @@ import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
+import dev.kske.chess.board.BoardState;
import dev.kske.chess.board.MoveNode;
import dev.kske.chess.board.Piece.Color;
import dev.kske.chess.event.Event;
import dev.kske.chess.event.EventBus;
+import dev.kske.chess.event.GameStartEvent;
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;
@@ -64,7 +65,6 @@ public class GamePane extends JComponent {
game.reset();
game.start();
}
- EventBus.getInstance().dispatch(new RestartEvent(this));
});
btnSwapColors = new JButton("Play as black");
@@ -152,10 +152,18 @@ public class GamePane extends JComponent {
pgnList.setCellRenderer(new MoveNodeRenderer());
scrollPane.setViewportView(pgnList);
+ // Listen to moves and game (re-)starts and update the move list or disable the
+ // color switching buttons if necessary
EventBus.getInstance().register(new Subscribable() {
@Override
public void handle(Event> event) {
+ if (event instanceof MoveEvent && (((MoveEvent) event).getBoardState() == BoardState.CHECKMATE
+ || ((MoveEvent) event).getBoardState() == BoardState.STALEMATE))
+ btnSwapColors.setEnabled(false);
+ else if (event instanceof GameStartEvent) btnSwapColors.setEnabled(
+ game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer ^ game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer);
+
if (game.getBoard().getLog() == null) return;
DefaultListModel model = new DefaultListModel<>();
@@ -164,7 +172,7 @@ public class GamePane extends JComponent {
}
@Override
- public Set> supports() { return new HashSet<>(Arrays.asList(MoveEvent.class, RestartEvent.class)); }
+ public Set> supports() { return new HashSet<>(Arrays.asList(MoveEvent.class, GameStartEvent.class)); }
});
}
@@ -187,7 +195,5 @@ 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);
}
}