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