Fixed color switching button

* Renamed RestartEvent to GameStartEvent
* Moved event trigger to Game
* Renamed GameState to BoardState
* Added BoardState instance to MoveEvent
* Disabling the color switching button when a checkmate or stalemate is
reached

Fixes #1
This commit is contained in:
Kai S. K. Engelbart 2019-11-06 19:52:52 +01:00
parent 6bbef7deee
commit 44f6a4b9b8
7 changed files with 50 additions and 36 deletions

View File

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

View File

@ -2,12 +2,12 @@ package dev.kske.chess.board;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>GameState.java</strong><br>
* File: <strong>BoardState.java</strong><br>
* Created: <strong>07.07.2019</strong><br>
*
* @since Chess v0.1-alpha
* @author Kai S. K. Engelbart
*/
public enum GameState {
public enum BoardState {
CHECK, CHECKMATE, STALEMATE, NORMAL;
}

View File

@ -0,0 +1,21 @@
package dev.kske.chess.event;
import dev.kske.chess.game.Game;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>GameStartEvent.java</strong><br>
* Created: <strong>30 Oct 2019</strong><br>
*
* @since Chess v0.5-alpha
* @author Kai S. K. Engelbart
*/
public class GameStartEvent implements Event<Game> {
private final Game game;
public GameStartEvent(Game source) { game = source; }
@Override
public Game getData() { return game; }
}

View File

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

View File

@ -1,20 +0,0 @@
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

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

View File

@ -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<MoveNode> model = new DefaultListModel<>();
@ -164,7 +172,7 @@ public class GamePane extends JComponent {
}
@Override
public Set<Class<?>> supports() { return new HashSet<>(Arrays.asList(MoveEvent.class, RestartEvent.class)); }
public Set<Class<?>> 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);
}
}