Compare commits

...

3 Commits

Author SHA1 Message Date
63a1e261e8 add move preconditions 2025-04-04 20:57:20 +02:00
7b07423175 add CommandResult.NeedsAction 2025-04-04 20:55:46 +02:00
416cfadc9b nuke CommandResult.java 2025-04-04 20:33:24 +02:00
14 changed files with 59 additions and 36 deletions

View File

@@ -3,5 +3,17 @@ package chess.io;
import chess.model.Game;
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);
}

View File

@@ -1,6 +1,6 @@
package chess.io;
import chess.io.commands.MoveCommand;
import chess.io.Command.CommandResult;
import chess.model.Game;
import chess.model.Game.GameStatus;
@@ -28,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;
}
}

View File

@@ -1,5 +0,0 @@
package chess.io;
public enum CommandResult {
Moved, NotMoved, NotAllowed;
}

View File

@@ -20,4 +20,6 @@ public interface OutputSystem {
void gameStarted();
void promotePawn(Coordinate pieceCoords);
void updateDisplay();
}

View File

@@ -1,14 +1,17 @@
package chess.io.commands;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.io.PlayerCommand;
import chess.model.Game;
public class CastlingCommand extends PlayerCommand{
public class CastlingCommand extends PlayerCommand {
@Override
public CommandResult execute(Game game, OutputSystem outputSystem) {
// we must promote the pending pawn before
if (game.pawnShouldBePromoted())
return CommandResult.NotAllowed;
return CommandResult.NotAllowed;
}

View File

@@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.List;
import chess.io.Command;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.model.ChessBoard;
import chess.model.Coordinate;

View File

@@ -1,7 +1,6 @@
package chess.io.commands;
import chess.io.Command;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.model.Coordinate;
import chess.model.Game;

View File

@@ -1,6 +1,5 @@
package chess.io.commands;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.io.PlayerCommand;
import chess.model.Game;
@@ -9,6 +8,10 @@ public class GrandCastlingCommand extends PlayerCommand {
@Override
public CommandResult execute(Game game, OutputSystem outputSystem) {
// we must promote the pending pawn before
if (game.pawnShouldBePromoted())
return CommandResult.NotAllowed;
return CommandResult.NotAllowed;
}

View File

@@ -1,9 +1,9 @@
package chess.io.commands;
import chess.io.CommandResult;
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;
@@ -50,6 +50,9 @@ public class MoveCommand extends PlayerCommand {
return CommandResult.NotAllowed;
}
if (shouldPromote(game, outputSystem))
return CommandResult.ActionNeeded;
return CommandResult.Moved;
}
@@ -60,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;
}
}

View File

@@ -1,7 +1,6 @@
package chess.io.commands;
import chess.io.Command;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.model.ChessBoard;
import chess.model.Color;

View File

@@ -1,6 +1,5 @@
package chess.io.commands;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.io.PlayerCommand;
import chess.model.ChessBoard;

View File

@@ -1,7 +1,6 @@
package chess.io.commands;
import chess.io.Command;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.model.Color;
import chess.model.Game;

View File

@@ -1,7 +1,6 @@
package chess.io.commands;
import chess.io.Command;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.model.Game;

View File

@@ -13,8 +13,8 @@ import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import chess.io.Command;
import chess.io.Command.CommandResult;
import chess.io.CommandExecutor;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.io.commands.GetAllowedMovesCommand;
import chess.io.commands.GetPieceAtCommand;
@@ -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();
}
}