Compare commits
3 Commits
810934aea1
...
63a1e261e8
| Author | SHA1 | Date | |
|---|---|---|---|
| 63a1e261e8 | |||
| 7b07423175 | |||
| 416cfadc9b |
@@ -3,5 +3,17 @@ package chess.io;
|
|||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
|
||||||
public abstract class Command {
|
public abstract class Command {
|
||||||
|
|
||||||
|
public enum CommandResult {
|
||||||
|
/** 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,6 +1,6 @@
|
|||||||
package chess.io;
|
package chess.io;
|
||||||
|
|
||||||
import chess.io.commands.MoveCommand;
|
import chess.io.Command.CommandResult;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
import chess.model.Game.GameStatus;
|
import chess.model.Game.GameStatus;
|
||||||
|
|
||||||
@@ -28,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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
package chess.io;
|
|
||||||
|
|
||||||
public enum CommandResult {
|
|
||||||
Moved, NotMoved, NotAllowed;
|
|
||||||
}
|
|
||||||
@@ -20,4 +20,6 @@ public interface OutputSystem {
|
|||||||
void gameStarted();
|
void gameStarted();
|
||||||
|
|
||||||
void promotePawn(Coordinate pieceCoords);
|
void promotePawn(Coordinate pieceCoords);
|
||||||
|
|
||||||
|
void updateDisplay();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
package chess.io.commands;
|
package chess.io.commands;
|
||||||
|
|
||||||
import chess.io.CommandResult;
|
|
||||||
import chess.io.OutputSystem;
|
import chess.io.OutputSystem;
|
||||||
import chess.io.PlayerCommand;
|
import chess.io.PlayerCommand;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
|
||||||
public class CastlingCommand extends PlayerCommand{
|
public class CastlingCommand extends PlayerCommand {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||||
|
// we must promote the pending pawn before
|
||||||
|
if (game.pawnShouldBePromoted())
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
return CommandResult.NotAllowed;
|
return CommandResult.NotAllowed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import chess.io.Command;
|
import chess.io.Command;
|
||||||
import chess.io.CommandResult;
|
|
||||||
import chess.io.OutputSystem;
|
import chess.io.OutputSystem;
|
||||||
import chess.model.ChessBoard;
|
import chess.model.ChessBoard;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package chess.io.commands;
|
package chess.io.commands;
|
||||||
|
|
||||||
import chess.io.Command;
|
import chess.io.Command;
|
||||||
import chess.io.CommandResult;
|
|
||||||
import chess.io.OutputSystem;
|
import chess.io.OutputSystem;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package chess.io.commands;
|
package chess.io.commands;
|
||||||
|
|
||||||
import chess.io.CommandResult;
|
|
||||||
import chess.io.OutputSystem;
|
import chess.io.OutputSystem;
|
||||||
import chess.io.PlayerCommand;
|
import chess.io.PlayerCommand;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
@@ -9,6 +8,10 @@ public class GrandCastlingCommand extends PlayerCommand {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||||
|
// we must promote the pending pawn before
|
||||||
|
if (game.pawnShouldBePromoted())
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
return CommandResult.NotAllowed;
|
return CommandResult.NotAllowed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package chess.io.commands;
|
package chess.io.commands;
|
||||||
|
|
||||||
import chess.io.CommandResult;
|
|
||||||
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;
|
||||||
@@ -50,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package chess.io.commands;
|
package chess.io.commands;
|
||||||
|
|
||||||
import chess.io.Command;
|
import chess.io.Command;
|
||||||
import chess.io.CommandResult;
|
|
||||||
import chess.io.OutputSystem;
|
import chess.io.OutputSystem;
|
||||||
import chess.model.ChessBoard;
|
import chess.model.ChessBoard;
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package chess.io.commands;
|
package chess.io.commands;
|
||||||
|
|
||||||
import chess.io.CommandResult;
|
|
||||||
import chess.io.OutputSystem;
|
import chess.io.OutputSystem;
|
||||||
import chess.io.PlayerCommand;
|
import chess.io.PlayerCommand;
|
||||||
import chess.model.ChessBoard;
|
import chess.model.ChessBoard;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package chess.io.commands;
|
package chess.io.commands;
|
||||||
|
|
||||||
import chess.io.Command;
|
import chess.io.Command;
|
||||||
import chess.io.CommandResult;
|
|
||||||
import chess.io.OutputSystem;
|
import chess.io.OutputSystem;
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package chess.io.commands;
|
package chess.io.commands;
|
||||||
|
|
||||||
import chess.io.Command;
|
import chess.io.Command;
|
||||||
import chess.io.CommandResult;
|
|
||||||
import chess.io.OutputSystem;
|
import chess.io.OutputSystem;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ import javax.swing.JPanel;
|
|||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
import chess.io.Command;
|
import chess.io.Command;
|
||||||
|
import chess.io.Command.CommandResult;
|
||||||
import chess.io.CommandExecutor;
|
import chess.io.CommandExecutor;
|
||||||
import chess.io.CommandResult;
|
|
||||||
import chess.io.OutputSystem;
|
import chess.io.OutputSystem;
|
||||||
import chess.io.commands.GetAllowedMovesCommand;
|
import chess.io.commands.GetAllowedMovesCommand;
|
||||||
import chess.io.commands.GetPieceAtCommand;
|
import chess.io.commands.GetPieceAtCommand;
|
||||||
@@ -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