add castling + fix promote undo
This commit is contained in:
@@ -2,23 +2,62 @@ package chess.controller.commands;
|
||||
|
||||
import chess.controller.OutputSystem;
|
||||
import chess.controller.PlayerCommand;
|
||||
import chess.model.ChessBoard;
|
||||
import chess.model.Color;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Game;
|
||||
import chess.model.Move;
|
||||
|
||||
public class CastlingCommand extends PlayerCommand {
|
||||
|
||||
private Move kingMove;
|
||||
private Move rookMove;
|
||||
private final boolean bigCastling;
|
||||
|
||||
public CastlingCommand(boolean bigCastling) {
|
||||
this.bigCastling = bigCastling;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||
// we must promote the pending pawn before
|
||||
if (game.pawnShouldBePromoted())
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
return CommandResult.NotAllowed;
|
||||
final ChessBoard board = game.getBoard();
|
||||
|
||||
if (bigCastling && !board.canBigCastle(game.getPlayerTurn()))
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
if (!bigCastling && !board.canSmallCastle(game.getPlayerTurn()))
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
int rookBeginX = bigCastling ? 0 : 7;
|
||||
int rookEndX = bigCastling ? 3 : 5;
|
||||
|
||||
int kingBeginX = 4;
|
||||
int kingEndX = bigCastling ? 2 : 6;
|
||||
|
||||
int colorLine = game.getPlayerTurn() == Color.White ? 7 : 0;
|
||||
|
||||
Coordinate kingCoords = new Coordinate(kingBeginX, colorLine);
|
||||
Coordinate rookCoords = new Coordinate(rookBeginX, colorLine);
|
||||
|
||||
this.kingMove = new Move(kingCoords, new Coordinate(kingEndX, colorLine));
|
||||
this.rookMove = new Move(rookCoords, new Coordinate(rookEndX, colorLine));
|
||||
|
||||
board.applyMove(this.kingMove);
|
||||
board.applyMove(this.rookMove);
|
||||
|
||||
return CommandResult.Moved;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CommandResult undoImpl(Game game, OutputSystem outputSystem) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'undo'");
|
||||
game.getBoard().undoMove(this.kingMove, null);
|
||||
game.getBoard().undoMove(this.rookMove, null);
|
||||
|
||||
return CommandResult.Moved;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user