Compare commits
2 Commits
9af06e36f8
...
2ec7be27ca
| Author | SHA1 | Date | |
|---|---|---|---|
| 2ec7be27ca | |||
| 8c2c6946d7 |
@@ -24,6 +24,10 @@ public class CommandExecutor {
|
|||||||
assert result != CommandResult.Moved || command instanceof PlayerCommand;
|
assert result != CommandResult.Moved || command instanceof PlayerCommand;
|
||||||
|
|
||||||
processResult(command, result);
|
processResult(command, result);
|
||||||
|
|
||||||
|
if (command instanceof PlayerCommand playerCommand)
|
||||||
|
this.game.addAction(playerCommand);
|
||||||
|
|
||||||
command.postExec(game, outputSystem);
|
command.postExec(game, outputSystem);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,5 +3,5 @@ package chess.controller;
|
|||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
|
||||||
public abstract class PlayerCommand extends Command{
|
public abstract class PlayerCommand extends Command{
|
||||||
public abstract void undo(Game game, OutputSystem outputSystem);
|
public abstract CommandResult undo(Game game, OutputSystem outputSystem);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public class CastlingCommand extends PlayerCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void undo(Game game, OutputSystem outputSystem) {
|
public CommandResult undo(Game game, OutputSystem outputSystem) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'undo'");
|
throw new UnsupportedOperationException("Unimplemented method 'undo'");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public class GrandCastlingCommand extends PlayerCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void undo(Game game, OutputSystem outputSystem) {
|
public CommandResult undo(Game game, OutputSystem outputSystem) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'undo'");
|
throw new UnsupportedOperationException("Unimplemented method 'undo'");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,10 +57,11 @@ public class MoveCommand extends PlayerCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void undo(Game game, OutputSystem outputSystem) {
|
public CommandResult undo(Game game, OutputSystem outputSystem) {
|
||||||
final ChessBoard board = game.getBoard();
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
board.undoMove(move, deadPiece);
|
board.undoMove(move, deadPiece);
|
||||||
|
return CommandResult.Moved;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import chess.model.pieces.Knight;
|
|||||||
import chess.model.pieces.Pawn;
|
import chess.model.pieces.Pawn;
|
||||||
import chess.model.pieces.Queen;
|
import chess.model.pieces.Queen;
|
||||||
import chess.model.pieces.Rook;
|
import chess.model.pieces.Rook;
|
||||||
|
import chess.model.visitor.PawnIdentifier;
|
||||||
|
|
||||||
public class PromoteCommand extends PlayerCommand {
|
public class PromoteCommand extends PlayerCommand {
|
||||||
|
|
||||||
@@ -39,7 +40,7 @@ public class PromoteCommand extends PlayerCommand {
|
|||||||
return CommandResult.NotAllowed;
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
Piece pawn = board.pieceAt(this.pieceCoords);
|
Piece pawn = board.pieceAt(this.pieceCoords);
|
||||||
if (pawn == null || !(pawn instanceof Pawn))
|
if (!new PawnIdentifier(game.getPlayerTurn()).isPawn(pawn))
|
||||||
return CommandResult.NotAllowed;
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
int destY = this.pieceCoords.getY();
|
int destY = this.pieceCoords.getY();
|
||||||
@@ -74,7 +75,7 @@ public class PromoteCommand extends PlayerCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void undo(Game game, OutputSystem outputSystem) {
|
public CommandResult undo(Game game, OutputSystem outputSystem) {
|
||||||
final ChessBoard board = game.getBoard();
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
Piece promoted = board.pieceAt(this.pieceCoords);
|
Piece promoted = board.pieceAt(this.pieceCoords);
|
||||||
@@ -83,6 +84,8 @@ public class PromoteCommand extends PlayerCommand {
|
|||||||
|
|
||||||
Color player = promoted.getColor();
|
Color player = promoted.getColor();
|
||||||
board.pieceComes(new Pawn(player), this.pieceCoords);
|
board.pieceComes(new Pawn(player), this.pieceCoords);
|
||||||
|
|
||||||
|
return CommandResult.ActionNeeded;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,14 +2,18 @@ package chess.controller.commands;
|
|||||||
|
|
||||||
import chess.controller.Command;
|
import chess.controller.Command;
|
||||||
import chess.controller.OutputSystem;
|
import chess.controller.OutputSystem;
|
||||||
|
import chess.controller.PlayerCommand;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
|
||||||
public class UndoCommand extends Command{
|
public class UndoCommand extends Command{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||||
//TODO
|
PlayerCommand lastAction = game.getLastAction();
|
||||||
return CommandResult.Moved;
|
if (lastAction == null)
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
return lastAction.undo(game, outputSystem);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ public class ChessBoard {
|
|||||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
Coordinate coordinate = new Coordinate(i, j);
|
Coordinate coordinate = new Coordinate(i, j);
|
||||||
Piece piece = pieceAt(coordinate);
|
Piece piece = pieceAt(coordinate);
|
||||||
if (piece != null && kingIdentifier.visit(piece)) {
|
if (kingIdentifier.isKing(piece)) {
|
||||||
return coordinate;
|
return coordinate;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,15 @@
|
|||||||
package chess.model;
|
package chess.model;
|
||||||
|
|
||||||
import chess.model.pieces.Pawn;
|
import java.util.EmptyStackException;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
import chess.controller.PlayerCommand;
|
||||||
|
import chess.model.visitor.PawnIdentifier;
|
||||||
|
|
||||||
public class Game {
|
public class Game {
|
||||||
private final ChessBoard board;
|
private final ChessBoard board;
|
||||||
private Color playerTurn;
|
private Color playerTurn;
|
||||||
|
private final Stack<PlayerCommand> movesHistory;
|
||||||
|
|
||||||
public enum GameStatus {
|
public enum GameStatus {
|
||||||
Check, CheckMate, OnGoing, Pat;
|
Check, CheckMate, OnGoing, Pat;
|
||||||
@@ -12,6 +17,7 @@ public class Game {
|
|||||||
|
|
||||||
public Game(ChessBoard board) {
|
public Game(ChessBoard board) {
|
||||||
this.board = board;
|
this.board = board;
|
||||||
|
this.movesHistory = new Stack<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChessBoard getBoard() {
|
public ChessBoard getBoard() {
|
||||||
@@ -66,18 +72,31 @@ public class Game {
|
|||||||
*/
|
*/
|
||||||
private Coordinate pawnPromotePosition(Color color) {
|
private Coordinate pawnPromotePosition(Color color) {
|
||||||
int enemyLineY = color == Color.White ? 0 : 7;
|
int enemyLineY = color == Color.White ? 0 : 7;
|
||||||
|
PawnIdentifier identifier = new PawnIdentifier(color);
|
||||||
|
|
||||||
for (int x = 0; x < Coordinate.VALUE_MAX; x++) {
|
for (int x = 0; x < Coordinate.VALUE_MAX; x++) {
|
||||||
Coordinate pieceCoords = new Coordinate(x, enemyLineY);
|
Coordinate pieceCoords = new Coordinate(x, enemyLineY);
|
||||||
Piece piece = getBoard().pieceAt(pieceCoords);
|
Piece piece = getBoard().pieceAt(pieceCoords);
|
||||||
|
|
||||||
if (piece == null || !(piece instanceof Pawn) || piece.getColor() != color)
|
if (identifier.isPawn(piece))
|
||||||
continue;
|
|
||||||
|
|
||||||
return pieceCoords;
|
return pieceCoords;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addAction(PlayerCommand command) {
|
||||||
|
this.movesHistory.add(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PlayerCommand getLastAction() {
|
||||||
|
try {
|
||||||
|
PlayerCommand last = this.movesHistory.pop();
|
||||||
|
return last;
|
||||||
|
} catch (EmptyStackException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package chess.model.visitor;
|
package chess.model.visitor;
|
||||||
|
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
|
import chess.model.Piece;
|
||||||
import chess.model.PieceVisitor;
|
import chess.model.PieceVisitor;
|
||||||
import chess.model.pieces.*;
|
import chess.model.pieces.*;
|
||||||
|
|
||||||
@@ -12,6 +13,12 @@ public class KingIdentifier implements PieceVisitor<Boolean> {
|
|||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isKing(Piece piece) {
|
||||||
|
if (piece == null)
|
||||||
|
return false;
|
||||||
|
return visit(piece);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean visitPiece(Bishop bishop) {
|
public Boolean visitPiece(Bishop bishop) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
52
app/src/main/java/chess/model/visitor/PawnIdentifier.java
Normal file
52
app/src/main/java/chess/model/visitor/PawnIdentifier.java
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package chess.model.visitor;
|
||||||
|
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Piece;
|
||||||
|
import chess.model.PieceVisitor;
|
||||||
|
import chess.model.pieces.*;
|
||||||
|
|
||||||
|
public class PawnIdentifier implements PieceVisitor<Boolean> {
|
||||||
|
|
||||||
|
private final Color color;
|
||||||
|
|
||||||
|
public PawnIdentifier(Color color) {
|
||||||
|
this.color = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPawn(Piece piece) {
|
||||||
|
if (piece == null)
|
||||||
|
return false;
|
||||||
|
return visit(piece);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Bishop bishop) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(King king) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Knight knight) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Pawn pawn) {
|
||||||
|
return pawn.getColor() == color;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Queen queen) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Rook rook) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import java.awt.event.MouseAdapter;
|
|||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.swing.JButton;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
@@ -13,31 +14,38 @@ import javax.swing.JPanel;
|
|||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
import chess.controller.Command;
|
import chess.controller.Command;
|
||||||
|
import chess.controller.Command.CommandResult;
|
||||||
import chess.controller.CommandExecutor;
|
import chess.controller.CommandExecutor;
|
||||||
import chess.controller.OutputSystem;
|
import chess.controller.OutputSystem;
|
||||||
import chess.controller.Command.CommandResult;
|
import chess.controller.commands.CastlingCommand;
|
||||||
import chess.controller.commands.GetAllowedMovesCommand;
|
import chess.controller.commands.GetAllowedMovesCommand;
|
||||||
import chess.controller.commands.GetPieceAtCommand;
|
import chess.controller.commands.GetPieceAtCommand;
|
||||||
|
import chess.controller.commands.GrandCastlingCommand;
|
||||||
import chess.controller.commands.MoveCommand;
|
import chess.controller.commands.MoveCommand;
|
||||||
import chess.controller.commands.PromoteCommand;
|
import chess.controller.commands.PromoteCommand;
|
||||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||||
|
import chess.controller.commands.UndoCommand;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Move;
|
import chess.model.Move;
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
|
|
||||||
public class Window extends JFrame implements OutputSystem {
|
public class Window extends JFrame implements OutputSystem {
|
||||||
|
|
||||||
private final JLabel cells[][];
|
|
||||||
private final JLabel displayText;
|
|
||||||
private final CommandExecutor commandExecutor;
|
private final CommandExecutor commandExecutor;
|
||||||
|
|
||||||
private Coordinate lastClick = null;
|
private Coordinate lastClick = null;
|
||||||
|
|
||||||
|
private final JLabel cells[][];
|
||||||
|
private final JLabel displayText;
|
||||||
|
private final JButton castlingButton = new JButton("Roque");
|
||||||
|
private final JButton bigCastlingButton = new JButton("Grand Roque");
|
||||||
|
private final JButton undoButton = new JButton("Annuler le coup précédent");
|
||||||
|
|
||||||
public Window(CommandExecutor commandExecutor) {
|
public Window(CommandExecutor commandExecutor) {
|
||||||
this.cells = new JLabel[8][8];
|
this.cells = new JLabel[8][8];
|
||||||
this.displayText = new JLabel();
|
this.displayText = new JLabel();
|
||||||
this.commandExecutor = commandExecutor;
|
this.commandExecutor = commandExecutor;
|
||||||
setSize(800, 870);
|
setSize(800, 910);
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
setLocationRelativeTo(null);
|
setLocationRelativeTo(null);
|
||||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||||
@@ -51,12 +59,34 @@ public class Window extends JFrame implements OutputSystem {
|
|||||||
return ((x + y) % 2 == 1) ? Color.BLACK : Color.WHITE;
|
return ((x + y) % 2 == 1) ? Color.BLACK : Color.WHITE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void buildButtons(JPanel bottom) {
|
||||||
|
castlingButton.addActionListener((event) -> {
|
||||||
|
sendCommand(new CastlingCommand());
|
||||||
|
});
|
||||||
|
|
||||||
|
bigCastlingButton.addActionListener((event) -> {
|
||||||
|
sendCommand(new GrandCastlingCommand());
|
||||||
|
});
|
||||||
|
|
||||||
|
undoButton.addActionListener((event) -> {
|
||||||
|
sendCommand(new UndoCommand());
|
||||||
|
});
|
||||||
|
|
||||||
|
bottom.add(castlingButton);
|
||||||
|
bottom.add(bigCastlingButton);
|
||||||
|
bottom.add(undoButton);
|
||||||
|
}
|
||||||
|
|
||||||
private void buildBoard() {
|
private void buildBoard() {
|
||||||
JPanel content = new JPanel();
|
JPanel content = new JPanel();
|
||||||
JPanel grid = new JPanel(new GridLayout(8, 8));
|
JPanel grid = new JPanel(new GridLayout(8, 8));
|
||||||
|
JPanel bottom = new JPanel();
|
||||||
|
|
||||||
|
buildButtons(bottom);
|
||||||
|
|
||||||
content.add(this.displayText);
|
content.add(this.displayText);
|
||||||
content.add(grid);
|
content.add(grid);
|
||||||
|
content.add(bottom);
|
||||||
|
|
||||||
setContentPane(content);
|
setContentPane(content);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user