juste better

This commit is contained in:
2025-04-02 10:54:14 +02:00
parent 1b9ff5bdd1
commit 97cafb903a
12 changed files with 212 additions and 131 deletions

View File

@@ -4,12 +4,12 @@ import chess.model.Game;
public class CommandExecutor {
private final Game game;
private final OutputSystem outputSystem;
private Game game;
private OutputSystem outputSystem;
public CommandExecutor(Game game, OutputSystem outputSystem) {
this.game = game;
this.outputSystem = outputSystem;
public CommandExecutor() {
this.game = null;
this.outputSystem = null;
}
public CommandResult executeCommand(Command command) {
@@ -22,16 +22,14 @@ public class CommandExecutor {
private void alternatePlayers() {
this.game.switchPlayerTurn();
this.outputSystem.playerTurn(this.game.getPlayerTurn());
}
public void setOutputSystem(OutputSystem outputSystem) {
this.outputSystem = outputSystem;
}
public Game getGame() {
return game;
public void setGame(Game game) {
this.game = game;
}
public OutputSystem getOutputSystem() {
return outputSystem;
}
}

View File

@@ -1,8 +1,20 @@
package chess.io;
public abstract class OutputSystem implements OutputSystemInterface {
import chess.model.Color;
public OutputSystem() {
}
public interface OutputSystem {
void playerTurn(Color color);
void winnerIs(Color color);
void kingIsInCheck();
void kingIsInMat();
void patSituation();
void hasSurrendered(Color color);
void gameStarted();
}

View File

@@ -1,18 +0,0 @@
package chess.io;
import chess.model.Color;
public interface OutputSystemInterface {
void playerTurn(Color color);
void winnerIs(Color color);
void kingIsInCheck();
void kingIsInMat();
void patSituation();
void hasSurrendered(Color color);
}

View File

@@ -0,0 +1,42 @@
package chess.io.commands;
import java.util.ArrayList;
import java.util.List;
import chess.io.Command;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.model.ChessBoard;
import chess.model.Coordinate;
import chess.model.Game;
import chess.model.Piece;
public class GetAllowedMovesCommand extends Command {
private final Coordinate start;
private List<Coordinate> destinations;
public GetAllowedMovesCommand(Coordinate start) {
this.start = start;
this.destinations = new ArrayList<>();
}
@Override
public CommandResult execute(Game game, OutputSystem outputSystem) {
final ChessBoard board = game.getBoard();
Piece piece = board.pieceAt(start);
if (piece == null)
return CommandResult.NotAllowed;
if (piece.getColor() != game.getPlayerTurn())
return CommandResult.NotAllowed;
this.destinations = board.getAllowedMoves(start);
return CommandResult.NotMoved;
}
public List<Coordinate> getDestinations() {
return destinations;
}
}

View File

@@ -0,0 +1,34 @@
package chess.io.commands;
import chess.io.Command;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.model.Coordinate;
import chess.model.Game;
import chess.model.Piece;
public class GetPieceAtCommand extends Command{
private final Coordinate pieceCoords;
private Piece piece;
public GetPieceAtCommand(Coordinate pieceCoords) {
this.pieceCoords = pieceCoords;
this.piece = null;
}
@Override
public CommandResult execute(Game game, OutputSystem outputSystem) {
if (!pieceCoords.isValid())
return CommandResult.NotAllowed;
this.piece = game.getBoard().pieceAt(pieceCoords);
return CommandResult.NotMoved;
}
public Piece getPiece() {
return piece;
}
}

View File

@@ -49,6 +49,8 @@ public class NewGameCommand extends Command {
game.resetPlayerTurn();
outputSystem.gameStarted();
return CommandResult.NotMoved;
}
}