lots of things

This commit is contained in:
2025-04-01 23:11:41 +02:00
parent 2c6b64fa7d
commit 1b9ff5bdd1
15 changed files with 428 additions and 79 deletions

View File

@@ -0,0 +1,63 @@
package chess.io.commands;
import chess.io.Command;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.model.ChessBoard;
import chess.model.Color;
import chess.model.Game;
import chess.model.Move;
import chess.model.Piece;
import chess.model.visitor.PiecePathChecker;
public class MoveCommand extends Command {
private final Move move;
public MoveCommand(Move move) {
this.move = move;
}
@Override
public CommandResult execute(Game game, OutputSystem outputSystem) {
final ChessBoard board = game.getBoard();
Piece piece = board.pieceAt(move.getStart());
if (piece == null)
return CommandResult.NotAllowed;
if (piece.getColor() != game.getPlayerTurn())
return CommandResult.NotAllowed;
boolean valid = new PiecePathChecker(board, move).isValid();
if (!valid)
return CommandResult.NotAllowed;
board.applyMove(move);
if (board.isKingInCheck(game.getPlayerTurn())) {
board.undoLastMove();
return CommandResult.NotAllowed;
}
checkGameStatus(game, outputSystem);
return CommandResult.Moved;
}
private void checkGameStatus(Game game, OutputSystem outputSystem) {
final ChessBoard board = game.getBoard();
final Color enemy = Color.getEnemy(game.getPlayerTurn());
if (board.isKingInCheck(enemy)) {
if (board.hasAllowedMoves(enemy)) {
outputSystem.kingIsInCheck();
} else {
outputSystem.kingIsInMat();
outputSystem.winnerIs(game.getPlayerTurn());
}
} else if(!board.hasAllowedMoves(enemy)) {
outputSystem.patSituation();
}
}
}