lots of things
This commit is contained in:
63
app/src/main/java/chess/io/commands/MoveCommand.java
Normal file
63
app/src/main/java/chess/io/commands/MoveCommand.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user