add CommandResult.NeedsAction
This commit is contained in:
@@ -5,7 +5,14 @@ import chess.model.Game;
|
||||
public abstract class Command {
|
||||
|
||||
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);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package chess.io;
|
||||
|
||||
import chess.io.Command.CommandResult;
|
||||
import chess.io.commands.MoveCommand;
|
||||
import chess.model.Game;
|
||||
import chess.model.Game.GameStatus;
|
||||
|
||||
@@ -29,21 +28,21 @@ public class CommandExecutor {
|
||||
}
|
||||
|
||||
private void processResult(Command command, CommandResult result) {
|
||||
if (result != CommandResult.Moved)
|
||||
return;
|
||||
|
||||
if (command instanceof MoveCommand move) {
|
||||
boolean needsPromote = this.game.pawnShouldBePromoted();
|
||||
if (needsPromote)
|
||||
this.outputSystem.promotePawn(move.getMove().getFinish());
|
||||
|
||||
if (checkGameStatus())
|
||||
switch (result) {
|
||||
case NotAllowed:
|
||||
case NotMoved:
|
||||
return;
|
||||
|
||||
if(!needsPromote)
|
||||
case ActionNeeded:
|
||||
this.outputSystem.updateDisplay();
|
||||
return;
|
||||
|
||||
case Moved:
|
||||
if (checkGameStatus())
|
||||
return;
|
||||
switchPlayerTurn();
|
||||
} else if (command instanceof PlayerCommand) {
|
||||
switchPlayerTurn();
|
||||
this.outputSystem.updateDisplay();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +62,7 @@ public class CommandExecutor {
|
||||
case Check:
|
||||
this.outputSystem.kingIsInCheck();
|
||||
return false;
|
||||
|
||||
|
||||
case CheckMate:
|
||||
this.outputSystem.kingIsInMat();
|
||||
this.outputSystem.winnerIs(this.game.getPlayerTurn());
|
||||
|
||||
@@ -20,4 +20,6 @@ public interface OutputSystem {
|
||||
void gameStarted();
|
||||
|
||||
void promotePawn(Coordinate pieceCoords);
|
||||
|
||||
void updateDisplay();
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package chess.io.commands;
|
||||
import chess.io.OutputSystem;
|
||||
import chess.io.PlayerCommand;
|
||||
import chess.model.ChessBoard;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Game;
|
||||
import chess.model.Move;
|
||||
import chess.model.Piece;
|
||||
@@ -49,6 +50,9 @@ public class MoveCommand extends PlayerCommand {
|
||||
return CommandResult.NotAllowed;
|
||||
}
|
||||
|
||||
if (shouldPromote(game, outputSystem))
|
||||
return CommandResult.ActionNeeded;
|
||||
|
||||
return CommandResult.Moved;
|
||||
}
|
||||
|
||||
@@ -59,4 +63,14 @@ public class MoveCommand extends PlayerCommand {
|
||||
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) {
|
||||
CommandResult result = this.commandExecutor.executeCommand(command);
|
||||
if (result == CommandResult.Moved) {
|
||||
updateBoard();
|
||||
}
|
||||
return result;
|
||||
return this.commandExecutor.executeCommand(command);
|
||||
}
|
||||
|
||||
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