Compare commits

..

3 Commits

Author SHA1 Message Date
55ef180f57 better gui interaction 2025-04-04 15:06:34 +02:00
5b006034ad undo command def 2025-04-04 14:48:48 +02:00
873ffc05d3 player command 2025-04-04 14:46:58 +02:00
8 changed files with 82 additions and 18 deletions

View File

@@ -19,6 +19,10 @@ public class CommandExecutor {
assert this.outputSystem != null : "No output system specified !";
CommandResult result = command.execute(this.game, this.outputSystem);
// non player commands are not supposed to return move result
assert result != CommandResult.Moved || command instanceof PlayerCommand;
processResult(command, result);
return result;
}
@@ -34,7 +38,7 @@ public class CommandExecutor {
if (!needsPromote)
this.game.switchPlayerTurn();
} else if (command instanceof PromoteCommand) {
} else if (command instanceof PlayerCommand) {
this.game.switchPlayerTurn();
}
}

View File

@@ -0,0 +1,7 @@
package chess.io;
import chess.model.Game;
public abstract class PlayerCommand extends Command{
public abstract void undo(Game game, OutputSystem outputSystem);
}

View File

@@ -1,15 +1,21 @@
package chess.io.commands;
import chess.io.Command;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.io.PlayerCommand;
import chess.model.Game;
public class CastlingCommand extends Command{
public class CastlingCommand extends PlayerCommand{
@Override
public CommandResult execute(Game game, OutputSystem outputSystem) {
return CommandResult.NotAllowed;
}
@Override
public void undo(Game game, OutputSystem outputSystem) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'undo'");
}
}

View File

@@ -1,15 +1,21 @@
package chess.io.commands;
import chess.io.Command;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.io.PlayerCommand;
import chess.model.Game;
public class GrandCastlingCommand extends Command{
public class GrandCastlingCommand extends PlayerCommand {
@Override
public CommandResult execute(Game game, OutputSystem outputSystem) {
return CommandResult.NotAllowed;
}
@Override
public void undo(Game game, OutputSystem outputSystem) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'undo'");
}
}

View File

@@ -1,15 +1,15 @@
package chess.io.commands;
import chess.io.Command;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.io.PlayerCommand;
import chess.model.ChessBoard;
import chess.model.Game;
import chess.model.Move;
import chess.model.Piece;
import chess.model.visitor.PiecePathChecker;
public class MoveCommand extends Command {
public class MoveCommand extends PlayerCommand {
private final Move move;
public MoveCommand(Move move) {
@@ -49,4 +49,9 @@ public class MoveCommand extends Command {
return CommandResult.Moved;
}
@Override
public void undo(Game game, OutputSystem outputSystem) {
game.getBoard().undoLastMove();
}
}

View File

@@ -1,8 +1,8 @@
package chess.io.commands;
import chess.io.Command;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.io.PlayerCommand;
import chess.model.ChessBoard;
import chess.model.Color;
import chess.model.Coordinate;
@@ -14,7 +14,7 @@ import chess.model.pieces.Pawn;
import chess.model.pieces.Queen;
import chess.model.pieces.Rook;
public class PromoteCommand extends Command {
public class PromoteCommand extends PlayerCommand {
public enum PromoteType {
Queen,
@@ -71,4 +71,17 @@ public class PromoteCommand extends Command {
}
}
@Override
public void undo(Game game, OutputSystem outputSystem) {
final ChessBoard board = game.getBoard();
Piece promoted = board.pieceAt(this.pieceCoords);
assert promoted != null;
Color player = promoted.getColor();
board.pieceLeaves(this.pieceCoords);
board.pieceComes(new Pawn(player), this.pieceCoords);
}
}

View File

@@ -0,0 +1,16 @@
package chess.io.commands;
import chess.io.Command;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.model.Game;
public class UndoCommand extends Command{
@Override
public CommandResult execute(Game game, OutputSystem outputSystem) {
//TODO
return CommandResult.Moved;
}
}

View File

@@ -28,16 +28,19 @@ import chess.model.Piece;
public class Window extends JFrame implements OutputSystem {
private final JLabel cells[][];
private final JLabel displayText;
private final CommandExecutor commandExecutor;
private Coordinate lastClick = null;
public Window(CommandExecutor commandExecutor) {
this.cells = new JLabel[8][8];
this.displayText = new JLabel();
this.commandExecutor = commandExecutor;
setSize(800, 800);
setSize(800, 870);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
private CommandResult sendCommand(Command command) {
@@ -53,8 +56,14 @@ public class Window extends JFrame implements OutputSystem {
}
private void buildBoard() {
JPanel content = new JPanel(new GridLayout(8, 8));
JPanel content = new JPanel();
JPanel grid = new JPanel(new GridLayout(8, 8));
content.add(this.displayText);
content.add(grid);
setContentPane(content);
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
JLabel label = new JLabel();
@@ -72,7 +81,7 @@ public class Window extends JFrame implements OutputSystem {
}
});
content.add(label);
grid.add(label);
}
}
updateBoard();
@@ -151,13 +160,14 @@ public class Window extends JFrame implements OutputSystem {
@Override
public void playerTurn(chess.model.Color color) {
System.out.println("C'est au tour de " + color);
this.displayText.setText("C'est au tour de " + color);
}
@Override
public void winnerIs(chess.model.Color color) {
SwingUtilities.invokeLater(() -> {
JOptionPane.showMessageDialog(this, "Victoire de " + color);
this.dispose();
});
}
@@ -197,9 +207,6 @@ public class Window extends JFrame implements OutputSystem {
@Override
public void promotePawn(Coordinate pieceCoords) {
SwingUtilities.invokeLater(() -> {
// String result = JOptionPane.showInputDialog(this, "Choisissez le type !");
// sendCommand(new PromoteCommand(PromoteType.Queen, pieceCoords));
String result = null;
Object[] possibilities = new Object[PromoteType.values().length];