Super IA (#5)
Reviewed-on: #5 Co-authored-by: Persson-dev <sim16.prib@gmail.com> Co-committed-by: Persson-dev <sim16.prib@gmail.com>
This commit was merged in pull request #5.
This commit is contained in:
@@ -2,6 +2,7 @@ package chess.controller;
|
||||
|
||||
import chess.controller.Command.CommandResult;
|
||||
import chess.controller.commands.UndoCommand;
|
||||
import chess.controller.event.AsyncGameDispatcher;
|
||||
import chess.controller.event.GameDispatcher;
|
||||
import chess.controller.event.GameListener;
|
||||
import chess.model.Game;
|
||||
@@ -17,9 +18,9 @@ public class CommandExecutor {
|
||||
}
|
||||
|
||||
public CommandExecutor(Game game) {
|
||||
this(game, new GameDispatcher());
|
||||
this(game, new AsyncGameDispatcher());
|
||||
}
|
||||
|
||||
|
||||
public CommandExecutor(Game game, GameDispatcher dispatcher) {
|
||||
this.game = game;
|
||||
this.dispatcher = dispatcher;
|
||||
@@ -52,19 +53,21 @@ public class CommandExecutor {
|
||||
return;
|
||||
|
||||
case Moved:
|
||||
boolean notifyPlayerTurn = true;
|
||||
this.dispatcher.onBoardUpdate();
|
||||
if (checkGameStatus()) {
|
||||
if (!(command instanceof UndoCommand) && checkGameStatus()) {
|
||||
this.dispatcher.onGameEnd();
|
||||
return;
|
||||
notifyPlayerTurn = false;
|
||||
}
|
||||
switchPlayerTurn(command instanceof UndoCommand);
|
||||
switchPlayerTurn(command instanceof UndoCommand, notifyPlayerTurn);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void switchPlayerTurn(boolean undone) {
|
||||
private void switchPlayerTurn(boolean undone, boolean notifyPlayerTurn) {
|
||||
this.game.switchPlayerTurn();
|
||||
this.dispatcher.onPlayerTurn(this.game.getPlayerTurn(), undone);
|
||||
if (notifyPlayerTurn)
|
||||
this.dispatcher.onPlayerTurn(this.game.getPlayerTurn(), undone);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,6 +110,6 @@ public class CommandExecutor {
|
||||
}
|
||||
|
||||
public void close() {
|
||||
this.dispatcher.stopService();
|
||||
this.dispatcher.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,10 @@ public class CastlingCommand extends PlayerCommand {
|
||||
board.applyMove(this.kingMove);
|
||||
board.applyMove(this.rookMove);
|
||||
|
||||
board.setLastMove(this.kingMove);
|
||||
|
||||
outputSystem.onCastling(this.bigCastling);
|
||||
|
||||
return CommandResult.Moved;
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,6 @@ public class MoveCommand extends PlayerCommand {
|
||||
return result;
|
||||
|
||||
case Moved:
|
||||
outputSystem.onMove(this.move);
|
||||
game.saveTraitPiecesPos();
|
||||
return result;
|
||||
|
||||
@@ -53,7 +52,7 @@ public class MoveCommand extends PlayerCommand {
|
||||
final ChessBoard board = game.getBoard();
|
||||
|
||||
// we must promote the pending pawn before
|
||||
if (board.pawnShouldBePromoted())
|
||||
if (game.pawnShouldBePromoted())
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
Piece piece = board.pieceAt(move.getStart());
|
||||
@@ -76,11 +75,14 @@ public class MoveCommand extends PlayerCommand {
|
||||
}
|
||||
|
||||
if (tryPromote(game, outputSystem)) {
|
||||
outputSystem.onMove(this.move);
|
||||
return CommandResult.ActionNeeded;
|
||||
}
|
||||
|
||||
board.setLastMove(this.move);
|
||||
|
||||
outputSystem.onMove(this.move);
|
||||
|
||||
return CommandResult.Moved;
|
||||
}
|
||||
|
||||
|
||||
@@ -55,6 +55,8 @@ public class PromoteCommand extends PlayerCommand {
|
||||
this.oldPawn = pawn;
|
||||
board.pieceComes(createPiece(this.promoteType, pawn.getColor()), this.pieceCoords);
|
||||
|
||||
outputSystem.onPawnPromoted(this.promoteType);
|
||||
|
||||
return CommandResult.Moved;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
package chess.controller.event;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||
import chess.model.Color;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Move;
|
||||
|
||||
public class AsyncGameDispatcher extends GameDispatcher {
|
||||
|
||||
private final List<GameListener> listeners;
|
||||
private final ExecutorService executor;
|
||||
|
||||
public AsyncGameDispatcher() {
|
||||
this.listeners = new ArrayList<>();
|
||||
this.executor = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(GameListener listener) {
|
||||
this.listeners.add(listener);
|
||||
}
|
||||
|
||||
private void asyncForEachCall(Consumer<GameListener> func) {
|
||||
this.executor.execute(() -> this.listeners.forEach(func));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerTurn(Color color, boolean undone) {
|
||||
asyncForEachCall((l) -> l.onPlayerTurn(color, undone));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWin(Color color) {
|
||||
asyncForEachCall((l) -> l.onWin(color));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onKingInCheck() {
|
||||
asyncForEachCall((l) -> l.onKingInCheck());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onKingInMat() {
|
||||
asyncForEachCall((l) -> l.onKingInMat());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPatSituation() {
|
||||
asyncForEachCall((l) -> l.onPatSituation());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurrender(Color color) {
|
||||
asyncForEachCall((l) -> l.onSurrender(color));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGameStart() {
|
||||
asyncForEachCall((l) -> l.onGameStart());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPromotePawn(Coordinate pieceCoords) {
|
||||
asyncForEachCall((l) -> l.onPromotePawn(pieceCoords));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBoardUpdate() {
|
||||
asyncForEachCall((l) -> l.onBoardUpdate());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGameEnd() {
|
||||
asyncForEachCall((l) -> l.onGameEnd());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMove(Move move) {
|
||||
asyncForEachCall((l) -> l.onMove(move));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMoveNotAllowed(Move move) {
|
||||
asyncForEachCall((l) -> l.onMoveNotAllowed(move));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDraw() {
|
||||
asyncForEachCall((l) -> l.onDraw());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCastling(boolean bigCastling) {
|
||||
asyncForEachCall((l) -> l.onCastling(bigCastling));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPawnPromoted(PromoteType promotion) {
|
||||
asyncForEachCall((l) -> l.onPawnPromoted(promotion));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
this.executor.shutdown();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package chess.controller.event;
|
||||
|
||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||
import chess.model.Color;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Move;
|
||||
@@ -57,5 +58,21 @@ public class EmptyGameDispatcher extends GameDispatcher {
|
||||
@Override
|
||||
public void onWin(Color winner) {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCastling(boolean bigCastling) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPawnPromoted(PromoteType promotion) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(GameListener listener) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package chess.controller.event;
|
||||
|
||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||
import chess.model.Color;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Move;
|
||||
@@ -45,4 +46,10 @@ public abstract class GameAdaptator implements GameListener {
|
||||
@Override
|
||||
public void onDraw() {}
|
||||
|
||||
@Override
|
||||
public void onCastling(boolean bigCastling) {}
|
||||
|
||||
@Override
|
||||
public void onPawnPromoted(PromoteType promotion) {}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,100 +1,9 @@
|
||||
package chess.controller.event;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.function.Consumer;
|
||||
public abstract class GameDispatcher extends GameAdaptator {
|
||||
|
||||
import chess.model.Color;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Move;
|
||||
|
||||
public class GameDispatcher implements GameListener {
|
||||
|
||||
private final List<GameListener> listeners;
|
||||
private final ExecutorService executor;
|
||||
|
||||
public GameDispatcher() {
|
||||
this.listeners = new ArrayList<>();
|
||||
this.executor = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
|
||||
public void addListener(GameListener listener) {
|
||||
this.listeners.add(listener);
|
||||
}
|
||||
|
||||
private void asyncForEachCall(Consumer<GameListener> func) {
|
||||
this.executor.execute(() -> this.listeners.forEach(func));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerTurn(Color color, boolean undone) {
|
||||
asyncForEachCall((l) -> l.onPlayerTurn(color, undone));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWin(Color color) {
|
||||
asyncForEachCall((l) -> l.onWin(color));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onKingInCheck() {
|
||||
asyncForEachCall((l) -> l.onKingInCheck());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onKingInMat() {
|
||||
asyncForEachCall((l) -> l.onKingInMat());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPatSituation() {
|
||||
asyncForEachCall((l) -> l.onPatSituation());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurrender(Color color) {
|
||||
asyncForEachCall((l) -> l.onSurrender(color));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGameStart() {
|
||||
asyncForEachCall((l) -> l.onGameStart());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPromotePawn(Coordinate pieceCoords) {
|
||||
asyncForEachCall((l) -> l.onPromotePawn(pieceCoords));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBoardUpdate() {
|
||||
asyncForEachCall((l) -> l.onBoardUpdate());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGameEnd() {
|
||||
asyncForEachCall((l) -> l.onGameEnd());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMove(Move move) {
|
||||
asyncForEachCall((l) -> l.onMove(move));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMoveNotAllowed(Move move) {
|
||||
asyncForEachCall((l) -> l.onMoveNotAllowed(move));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDraw() {
|
||||
asyncForEachCall((l) -> l.onDraw());
|
||||
}
|
||||
|
||||
public void stopService() {
|
||||
this.executor.shutdown();
|
||||
}
|
||||
public abstract void addListener(GameListener listener);
|
||||
|
||||
public abstract void close();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package chess.controller.event;
|
||||
|
||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||
import chess.model.Color;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Move;
|
||||
@@ -15,7 +16,7 @@ public interface GameListener {
|
||||
* Invoked when a draw occurs (same position is repeated three times)
|
||||
*/
|
||||
void onDraw();
|
||||
|
||||
|
||||
/**
|
||||
* Invoked when the game has ended (by a win or a draw)
|
||||
*/
|
||||
@@ -25,7 +26,7 @@ public interface GameListener {
|
||||
* Invoked when the game has started
|
||||
*/
|
||||
void onGameStart();
|
||||
|
||||
|
||||
/**
|
||||
* Invoked when a king is in check
|
||||
*/
|
||||
@@ -35,47 +36,68 @@ public interface GameListener {
|
||||
* Invoked when a checkmate occurs
|
||||
*/
|
||||
void onKingInMat();
|
||||
|
||||
|
||||
/**
|
||||
* Invoked when a valid move on the board occurs
|
||||
*
|
||||
* @param move the move to be processed
|
||||
*/
|
||||
void onMove(Move move);
|
||||
|
||||
/**
|
||||
* Invoked when a sent move is not allowed
|
||||
*
|
||||
* @param move the move to be processed
|
||||
*/
|
||||
void onMoveNotAllowed(Move move);
|
||||
|
||||
|
||||
/**
|
||||
* Invoked when a pat situation occurs
|
||||
*/
|
||||
void onPatSituation();
|
||||
|
||||
|
||||
/**
|
||||
* Invoked when it's the player turn
|
||||
* @param color the color of the player who should play
|
||||
*
|
||||
* @param color the color of the player who should play
|
||||
* @param undone true if it's a result of an undo command
|
||||
*/
|
||||
void onPlayerTurn(Color color, boolean undone);
|
||||
|
||||
|
||||
/**
|
||||
* Invoked when a pawn should be promoted
|
||||
*
|
||||
* @param pieceCoords the coordinates of the pawn
|
||||
*/
|
||||
void onPromotePawn(Coordinate pieceCoords);
|
||||
|
||||
|
||||
/**
|
||||
* Invoked when a players surrenders
|
||||
*
|
||||
* @param coward the player who gave up
|
||||
*/
|
||||
void onSurrender(Color coward);
|
||||
|
||||
|
||||
/**
|
||||
* Invoked when a player wins (by checkmate or if the other one surrenders)
|
||||
*
|
||||
* @param winner
|
||||
*/
|
||||
void onWin(Color winner);
|
||||
|
||||
/**
|
||||
* Invoked when a castling is done
|
||||
*
|
||||
* @param bigCastling if it's queen side castling
|
||||
*/
|
||||
void onCastling(boolean bigCastling);
|
||||
|
||||
/**
|
||||
* Invoked when a pawn is promoted
|
||||
*
|
||||
* @param promotion the type of promotion
|
||||
* @param player the player who promoted the pawns
|
||||
*/
|
||||
void onPawnPromoted(PromoteType promotion);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user