add castling commands
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package chess.controller.commands;
|
||||
|
||||
import chess.controller.Command;
|
||||
import chess.controller.event.GameListener;
|
||||
import chess.model.Game;
|
||||
|
||||
public class GetAllowedCastlingsCommand extends Command{
|
||||
|
||||
public enum CastlingResult {
|
||||
None, Small, Big, Both;
|
||||
}
|
||||
|
||||
private CastlingResult castlingResult;
|
||||
|
||||
public GetAllowedCastlingsCommand() {}
|
||||
|
||||
@Override
|
||||
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||
boolean canSmallCastle = game.getBoard().canSmallCastle(game.getPlayerTurn());
|
||||
boolean canBigCastle = game.getBoard().canBigCastle(game.getPlayerTurn());
|
||||
|
||||
int result = 0;
|
||||
if (canSmallCastle)
|
||||
result += 1;
|
||||
if (canBigCastle)
|
||||
result += 2;
|
||||
|
||||
this.castlingResult = CastlingResult.values()[result];
|
||||
|
||||
return CommandResult.NotMoved;
|
||||
}
|
||||
|
||||
public CastlingResult getCastlingResult() {
|
||||
return castlingResult;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -230,7 +230,10 @@ public class ChessBoard {
|
||||
undoLastMove();
|
||||
}
|
||||
|
||||
return true;
|
||||
Coordinate rookObstacleCoords = Coordinate.fromIndex(rookCoords.toIndex() - kingDirection.getIndexOffset());
|
||||
Piece obstacle = pieceAt(rookObstacleCoords);
|
||||
|
||||
return obstacle == null;
|
||||
}
|
||||
|
||||
public boolean canSmallCastle(Color color) {
|
||||
|
||||
@@ -5,6 +5,8 @@ import java.awt.Graphics;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@@ -19,12 +21,14 @@ import chess.controller.Command;
|
||||
import chess.controller.Command.CommandResult;
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.commands.CastlingCommand;
|
||||
import chess.controller.commands.GetAllowedCastlingsCommand;
|
||||
import chess.controller.commands.GetAllowedMovesPieceCommand;
|
||||
import chess.controller.commands.GetPieceAtCommand;
|
||||
import chess.controller.commands.MoveCommand;
|
||||
import chess.controller.commands.PromoteCommand;
|
||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||
import chess.controller.commands.UndoCommand;
|
||||
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
|
||||
import chess.controller.event.GameListener;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Move;
|
||||
@@ -44,7 +48,7 @@ public class Window extends JFrame implements GameListener {
|
||||
|
||||
private final boolean showPopups;
|
||||
|
||||
public Window(CommandExecutor commandExecutor, boolean showPopups) {
|
||||
public Window(final CommandExecutor commandExecutor, boolean showPopups) {
|
||||
this.cells = new JLabel[8][8];
|
||||
this.displayText = new JLabel();
|
||||
this.commandExecutor = commandExecutor;
|
||||
@@ -53,6 +57,12 @@ public class Window extends JFrame implements GameListener {
|
||||
setVisible(true);
|
||||
setLocationRelativeTo(null);
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosed(WindowEvent e) {
|
||||
commandExecutor.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private CommandResult sendCommand(Command command) {
|
||||
@@ -193,9 +203,19 @@ public class Window extends JFrame implements GameListener {
|
||||
this.lastClick = null;
|
||||
}
|
||||
|
||||
private void updateButtons() {
|
||||
GetAllowedCastlingsCommand cmd = new GetAllowedCastlingsCommand();
|
||||
sendCommand(cmd);
|
||||
this.castlingButton.setEnabled(
|
||||
cmd.getCastlingResult() == CastlingResult.Small || cmd.getCastlingResult() == CastlingResult.Both);
|
||||
this.bigCastlingButton.setEnabled(
|
||||
cmd.getCastlingResult() == CastlingResult.Big || cmd.getCastlingResult() == CastlingResult.Both);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerTurn(chess.model.Color color) {
|
||||
this.displayText.setText("Current turn: " + color);
|
||||
updateButtons();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user