add CommandResult.NeedsAction
This commit is contained in:
@@ -5,7 +5,14 @@ import chess.model.Game;
|
|||||||
public abstract class Command {
|
public abstract class Command {
|
||||||
|
|
||||||
public enum CommandResult {
|
public enum CommandResult {
|
||||||
Moved, NotMoved, NotAllowed;
|
/** The command was successfull. Should update display and switch player turn. */
|
||||||
|
Moved,
|
||||||
|
/** The command was successfull. Should not update anything */
|
||||||
|
NotMoved,
|
||||||
|
/** The command was successfull. Should only update display */
|
||||||
|
ActionNeeded,
|
||||||
|
/** The command was not successfull */
|
||||||
|
NotAllowed;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract CommandResult execute(Game game, OutputSystem outputSystem);
|
public abstract CommandResult execute(Game game, OutputSystem outputSystem);
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package chess.io;
|
package chess.io;
|
||||||
|
|
||||||
import chess.io.Command.CommandResult;
|
import chess.io.Command.CommandResult;
|
||||||
import chess.io.commands.MoveCommand;
|
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
import chess.model.Game.GameStatus;
|
import chess.model.Game.GameStatus;
|
||||||
|
|
||||||
@@ -29,21 +28,21 @@ public class CommandExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void processResult(Command command, CommandResult result) {
|
private void processResult(Command command, CommandResult result) {
|
||||||
if (result != CommandResult.Moved)
|
switch (result) {
|
||||||
return;
|
case NotAllowed:
|
||||||
|
case NotMoved:
|
||||||
if (command instanceof MoveCommand move) {
|
|
||||||
boolean needsPromote = this.game.pawnShouldBePromoted();
|
|
||||||
if (needsPromote)
|
|
||||||
this.outputSystem.promotePawn(move.getMove().getFinish());
|
|
||||||
|
|
||||||
if (checkGameStatus())
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if(!needsPromote)
|
case ActionNeeded:
|
||||||
|
this.outputSystem.updateDisplay();
|
||||||
|
return;
|
||||||
|
|
||||||
|
case Moved:
|
||||||
|
if (checkGameStatus())
|
||||||
|
return;
|
||||||
switchPlayerTurn();
|
switchPlayerTurn();
|
||||||
} else if (command instanceof PlayerCommand) {
|
this.outputSystem.updateDisplay();
|
||||||
switchPlayerTurn();
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +62,7 @@ public class CommandExecutor {
|
|||||||
case Check:
|
case Check:
|
||||||
this.outputSystem.kingIsInCheck();
|
this.outputSystem.kingIsInCheck();
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
case CheckMate:
|
case CheckMate:
|
||||||
this.outputSystem.kingIsInMat();
|
this.outputSystem.kingIsInMat();
|
||||||
this.outputSystem.winnerIs(this.game.getPlayerTurn());
|
this.outputSystem.winnerIs(this.game.getPlayerTurn());
|
||||||
|
|||||||
@@ -20,4 +20,6 @@ public interface OutputSystem {
|
|||||||
void gameStarted();
|
void gameStarted();
|
||||||
|
|
||||||
void promotePawn(Coordinate pieceCoords);
|
void promotePawn(Coordinate pieceCoords);
|
||||||
|
|
||||||
|
void updateDisplay();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package chess.io.commands;
|
|||||||
import chess.io.OutputSystem;
|
import chess.io.OutputSystem;
|
||||||
import chess.io.PlayerCommand;
|
import chess.io.PlayerCommand;
|
||||||
import chess.model.ChessBoard;
|
import chess.model.ChessBoard;
|
||||||
|
import chess.model.Coordinate;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
import chess.model.Move;
|
import chess.model.Move;
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
@@ -49,6 +50,9 @@ public class MoveCommand extends PlayerCommand {
|
|||||||
return CommandResult.NotAllowed;
|
return CommandResult.NotAllowed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (shouldPromote(game, outputSystem))
|
||||||
|
return CommandResult.ActionNeeded;
|
||||||
|
|
||||||
return CommandResult.Moved;
|
return CommandResult.Moved;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,4 +63,14 @@ public class MoveCommand extends PlayerCommand {
|
|||||||
board.undoMove(move, deadPiece);
|
board.undoMove(move, deadPiece);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean shouldPromote(Game game, OutputSystem outputSystem) {
|
||||||
|
Coordinate pawnPos = game.pawnPromotePosition();
|
||||||
|
|
||||||
|
if (pawnPos == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
outputSystem.promotePawn(pawnPos);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,11 +44,7 @@ public class Window extends JFrame implements OutputSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private CommandResult sendCommand(Command command) {
|
private CommandResult sendCommand(Command command) {
|
||||||
CommandResult result = this.commandExecutor.executeCommand(command);
|
return this.commandExecutor.executeCommand(command);
|
||||||
if (result == CommandResult.Moved) {
|
|
||||||
updateBoard();
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Color getCellColor(int x, int y) {
|
private Color getCellColor(int x, int y) {
|
||||||
@@ -241,4 +237,9 @@ public class Window extends JFrame implements OutputSystem {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateDisplay() {
|
||||||
|
updateBoard();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user