Compare commits

...

2 Commits

Author SHA1 Message Date
810934aea1 better undo 2025-04-04 20:06:32 +02:00
48a215eae5 remove game signals 2025-04-04 19:42:23 +02:00
8 changed files with 110 additions and 127 deletions

View File

@@ -1,8 +1,8 @@
package chess.io; package chess.io;
import chess.io.commands.MoveCommand; import chess.io.commands.MoveCommand;
import chess.io.commands.PromoteCommand;
import chess.model.Game; import chess.model.Game;
import chess.model.Game.GameStatus;
public class CommandExecutor { public class CommandExecutor {
@@ -32,49 +32,59 @@ public class CommandExecutor {
return; return;
if (command instanceof MoveCommand move) { if (command instanceof MoveCommand move) {
boolean needsPromote = this.game.checkPromotion(move.getMove().getFinish()); boolean needsPromote = this.game.pawnShouldBePromoted();
if (this.game.checkGameStatus()) if (needsPromote)
this.outputSystem.promotePawn(move.getMove().getFinish());
if (checkGameStatus())
return; return;
if (!needsPromote) if(!needsPromote)
this.game.switchPlayerTurn(); switchPlayerTurn();
} else if (command instanceof PlayerCommand) { } else if (command instanceof PlayerCommand) {
this.game.switchPlayerTurn(); switchPlayerTurn();
} }
} }
private void switchPlayerTurn() {
this.game.switchPlayerTurn();
this.outputSystem.playerTurn(this.game.getPlayerTurn());
}
/**
*
* @return True if the game is over
*/
private boolean checkGameStatus() {
GameStatus gameStatus = this.game.checkGameStatus();
switch (gameStatus) {
case Check:
this.outputSystem.kingIsInCheck();
return false;
case CheckMate:
this.outputSystem.kingIsInMat();
this.outputSystem.winnerIs(this.game.getPlayerTurn());
return true;
case OnGoing:
return false;
case Pat:
this.outputSystem.patSituation();
return true;
}
return false;
}
public void setOutputSystem(OutputSystem outputSystem) { public void setOutputSystem(OutputSystem outputSystem) {
unbindListeners();
this.outputSystem = outputSystem; this.outputSystem = outputSystem;
bindListeners();
} }
public void setGame(Game game) { public void setGame(Game game) {
unbindListeners();
this.game = game; this.game = game;
bindListeners();
}
private void unbindListeners() {
if (this.game == null || this.outputSystem == null)
return;
this.game.OnCheck.disconnect(outputSystem::kingIsInCheck);
this.game.OnMat.disconnect(outputSystem::kingIsInMat);
this.game.OnPat.disconnect(outputSystem::patSituation);
this.game.OnPromote.disconnect(outputSystem::promotePawn);
this.game.OnWin.disconnect(outputSystem::winnerIs);
this.game.OnPlayerTurn.disconnect(outputSystem::playerTurn);
}
private void bindListeners() {
if (this.game == null || this.outputSystem == null)
return;
this.game.OnCheck.connect(outputSystem::kingIsInCheck);
this.game.OnMat.connect(outputSystem::kingIsInMat);
this.game.OnPat.connect(outputSystem::patSituation);
this.game.OnPromote.connect(outputSystem::promotePawn);
this.game.OnWin.connect(outputSystem::winnerIs);
this.game.OnPlayerTurn.connect(outputSystem::playerTurn);
} }
} }

View File

@@ -10,10 +10,13 @@ import chess.model.Piece;
import chess.model.visitor.PiecePathChecker; import chess.model.visitor.PiecePathChecker;
public class MoveCommand extends PlayerCommand { public class MoveCommand extends PlayerCommand {
private final Move move; private final Move move;
private Piece deadPiece;
public MoveCommand(Move move) { public MoveCommand(Move move) {
this.move = move; this.move = move;
this.deadPiece = null;
} }
public Move getMove() { public Move getMove() {
@@ -39,6 +42,7 @@ public class MoveCommand extends PlayerCommand {
if (!valid) if (!valid)
return CommandResult.NotAllowed; return CommandResult.NotAllowed;
this.deadPiece = board.pieceAt(move.getFinish());
board.applyMove(move); board.applyMove(move);
if (board.isKingInCheck(game.getPlayerTurn())) { if (board.isKingInCheck(game.getPlayerTurn())) {
@@ -51,7 +55,9 @@ public class MoveCommand extends PlayerCommand {
@Override @Override
public void undo(Game game, OutputSystem outputSystem) { public void undo(Game game, OutputSystem outputSystem) {
game.getBoard().undoLastMove(); final ChessBoard board = game.getBoard();
board.undoMove(move, deadPiece);
} }
} }

View File

@@ -16,7 +16,9 @@ import chess.model.pieces.Rook;
public class NewGameCommand extends Command { public class NewGameCommand extends Command {
public CommandResult execute(Game game, OutputSystem outputSystem) { public CommandResult execute(Game game, OutputSystem outputSystem) {
ChessBoard board = game.getBoard(); final ChessBoard board = game.getBoard();
board.clearBoard();
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
board.pieceComes(new Pawn(Color.Black), new Coordinate(i, 1)); board.pieceComes(new Pawn(Color.Black), new Coordinate(i, 1));
@@ -50,6 +52,7 @@ public class NewGameCommand extends Command {
game.resetPlayerTurn(); game.resetPlayerTurn();
outputSystem.gameStarted(); outputSystem.gameStarted();
outputSystem.playerTurn(game.getPlayerTurn());
return CommandResult.NotMoved; return CommandResult.NotMoved;
} }

View File

@@ -24,17 +24,21 @@ public class PromoteCommand extends PlayerCommand {
} }
private final PromoteType promoteType; private final PromoteType promoteType;
private final Coordinate pieceCoords; private Coordinate pieceCoords;
public PromoteCommand(PromoteType promoteType, Coordinate pieceCoords) { public PromoteCommand(PromoteType promoteType) {
this.promoteType = promoteType; this.promoteType = promoteType;
this.pieceCoords = pieceCoords;
} }
@Override @Override
public CommandResult execute(Game game, OutputSystem outputSystem) { public CommandResult execute(Game game, OutputSystem outputSystem) {
final ChessBoard board = game.getBoard(); final ChessBoard board = game.getBoard();
this.pieceCoords = game.pawnPromotePosition();
if (this.pieceCoords == null)
return CommandResult.NotAllowed;
Piece pawn = board.pieceAt(this.pieceCoords); Piece pawn = board.pieceAt(this.pieceCoords);
if (pawn == null || !(pawn instanceof Pawn)) if (pawn == null || !(pawn instanceof Pawn))
return CommandResult.NotAllowed; return CommandResult.NotAllowed;
@@ -46,7 +50,6 @@ public class PromoteCommand extends PlayerCommand {
if (destY != enemyLine) if (destY != enemyLine)
return CommandResult.NotAllowed; return CommandResult.NotAllowed;
board.pieceLeaves(this.pieceCoords);
board.pieceComes(createPiece(this.promoteType, pawn.getColor()), this.pieceCoords); board.pieceComes(createPiece(this.promoteType, pawn.getColor()), this.pieceCoords);
return CommandResult.Moved; return CommandResult.Moved;
@@ -80,7 +83,6 @@ public class PromoteCommand extends PlayerCommand {
assert promoted != null; assert promoted != null;
Color player = promoted.getColor(); Color player = promoted.getColor();
board.pieceLeaves(this.pieceCoords);
board.pieceComes(new Pawn(player), this.pieceCoords); board.pieceComes(new Pawn(player), this.pieceCoords);
} }

View File

@@ -2,7 +2,6 @@ package chess.model;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Stack;
import chess.model.visitor.KingIdentifier; import chess.model.visitor.KingIdentifier;
import chess.model.visitor.PiecePathChecker; import chess.model.visitor.PiecePathChecker;
@@ -26,8 +25,8 @@ public class ChessBoard {
} }
private final Cell[][] cells; private final Cell[][] cells;
private final Stack<Move> moves; private Move lastMove;
private final Stack<Piece> ejectedPieces; private Piece lastEjectedPiece;
public ChessBoard() { public ChessBoard() {
this.cells = new Cell[Coordinate.VALUE_MAX][Coordinate.VALUE_MAX]; this.cells = new Cell[Coordinate.VALUE_MAX][Coordinate.VALUE_MAX];
@@ -36,43 +35,36 @@ public class ChessBoard {
this.cells[i][j] = new Cell(); this.cells[i][j] = new Cell();
} }
} }
this.moves = new Stack<>(); this.lastMove = null;
this.ejectedPieces = new Stack<>(); this.lastEjectedPiece = null;
} }
public void applyMove(Move move) { public void applyMove(Move move) {
assert (move.isValid()); assert move.isValid() : "Invalid move !";
Piece deadPiece = pieceAt(move.getFinish()); Piece deadPiece = pieceAt(move.getFinish());
if (deadPiece != null) { if (deadPiece != null) {
deadPiece.eject(this.moves.size()); this.lastEjectedPiece = deadPiece;
this.ejectedPieces.add(deadPiece); } else {
this.lastEjectedPiece = null;
} }
Piece movingPiece = pieceAt(move.getStart()); Piece movingPiece = pieceAt(move.getStart());
pieceComes(movingPiece, move.getFinish()); pieceComes(movingPiece, move.getFinish());
pieceLeaves(move.getStart()); pieceLeaves(move.getStart());
movingPiece.move(); movingPiece.move();
this.moves.add(move); this.lastMove = move;
} }
public void undoLastMove() { public void undoLastMove() {
assert !this.moves.empty() : "Can't undo at the beginning!"; assert this.lastMove != null: "Can't undo at the beginning!";
Move move = this.moves.pop(); undoMove(this.lastMove, this.lastEjectedPiece);
}
public void undoMove(Move move, Piece deadPiece) {
Piece movingPiece = pieceAt(move.getFinish()); Piece movingPiece = pieceAt(move.getFinish());
pieceComes(movingPiece, move.getStart()); pieceComes(movingPiece, move.getStart());
pieceLeaves(move.getFinish()); pieceComes(deadPiece, move.getFinish());
movingPiece.unMove(); movingPiece.unMove();
if (this.ejectedPieces.empty())
return;
Piece piece = this.ejectedPieces.lastElement();
if (piece.getEjectedMoveNumber() != this.moves.size())
return;
pieceComes(piece, move.getFinish());
piece.eject(-1);
} }
public boolean isCellEmpty(Coordinate coordinate) { public boolean isCellEmpty(Coordinate coordinate) {

View File

@@ -1,20 +1,14 @@
package chess.model; package chess.model;
import chess.model.pieces.Pawn; import chess.model.pieces.Pawn;
import common.Signal0;
import common.Signal1;
public class Game { public class Game {
private final ChessBoard board; private final ChessBoard board;
private Color playerTurn; private Color playerTurn;
public final Signal1<Coordinate> OnPromote = new Signal1<>(); public enum GameStatus {
public final Signal1<Color> OnWin = new Signal1<>(); Check, CheckMate, OnGoing, Pat;
public final Signal1<Color> OnPlayerTurn = new Signal1<>(); }
public final Signal0 OnCheck = new Signal0();
public final Signal0 OnMat = new Signal0();
public final Signal0 OnPat = new Signal0();
public Game(ChessBoard board) { public Game(ChessBoard board) {
this.board = board; this.board = board;
@@ -30,70 +24,60 @@ public class Game {
public void resetPlayerTurn() { public void resetPlayerTurn() {
this.playerTurn = Color.White; this.playerTurn = Color.White;
this.OnPlayerTurn.emit(playerTurn);
} }
public void switchPlayerTurn() { public void switchPlayerTurn() {
playerTurn = Color.getEnemy(playerTurn); playerTurn = Color.getEnemy(playerTurn);
this.OnPlayerTurn.emit(playerTurn);
} }
public boolean checkPromotion(Coordinate pieceCoords) { public GameStatus checkGameStatus() {
final Color enemy = Color.getEnemy(getPlayerTurn());
Piece piece = getBoard().pieceAt(pieceCoords); if (this.board.isKingInCheck(enemy))
if (this.board.hasAllowedMoves(enemy))
return GameStatus.Check;
else
return GameStatus.CheckMate;
if (piece == null || !(piece instanceof Pawn)) if (!board.hasAllowedMoves(enemy))
return false; return GameStatus.Pat;
int destY = pieceCoords.getY(); return GameStatus.OnGoing;
}
int enemyLine = piece.getColor() == Color.White ? 0 : 7; public boolean pawnShouldBePromoted() {
return pawnPromotePosition() != null;
if (destY != enemyLine)
return false;
this.OnPromote.emit(pieceCoords);
return true;
} }
/** /**
* *
* @return true if game should end * @return Null if there is no pawn to promote
*/ */
public boolean checkGameStatus() { public Coordinate pawnPromotePosition() {
final Color enemy = Color.getEnemy(getPlayerTurn()); Coordinate piecePos = pawnPromotePosition(Color.White);
if (piecePos != null)
if (this.board.isKingInCheck(enemy)) { return piecePos;
if (this.board.hasAllowedMoves(enemy)) { return pawnPromotePosition(Color.Black);
this.OnCheck.emit();
} else {
this.OnMat.emit();
this.OnWin.emit(getPlayerTurn());
return true;
}
} else if (!board.hasAllowedMoves(enemy)) {
this.OnPat.emit();
return true;
}
return false;
} }
public boolean pawnShouldBePromoted() { /**
if (pawnShouldBePromoted(Color.White)) *
return true; * @return Null if there is no pawn to promote
return pawnShouldBePromoted(Color.Black); */
} private Coordinate pawnPromotePosition(Color color) {
private boolean pawnShouldBePromoted(Color color) {
int enemyLineY = color == Color.White ? 0 : 7; int enemyLineY = color == Color.White ? 0 : 7;
for (int x = 0; x < Coordinate.VALUE_MAX; x++) { for (int x = 0; x < Coordinate.VALUE_MAX; x++) {
if (checkPromotion(new Coordinate(x, enemyLineY))) Coordinate pieceCoords = new Coordinate(x, enemyLineY);
return true; Piece piece = getBoard().pieceAt(pieceCoords);
if (piece == null || !(piece instanceof Pawn) || piece.getColor() != color)
continue;
return pieceCoords;
} }
return false; return null;
} }
} }

View File

@@ -4,12 +4,10 @@ public abstract class Piece {
private final Color color; private final Color color;
private int moved; private int moved;
private int ejectedMoveNumber;
public Piece(Color color) { public Piece(Color color) {
this.color = color; this.color = color;
this.moved = 0; this.moved = 0;
this.ejectedMoveNumber = -1;
} }
public void move() { public void move() {
@@ -28,18 +26,6 @@ public abstract class Piece {
this.moved--; this.moved--;
} }
public boolean isEjected() {
return this.ejectedMoveNumber != -1;
}
public int getEjectedMoveNumber() {
return ejectedMoveNumber;
}
public void eject(int moveNumber) {
this.ejectedMoveNumber = moveNumber;
}
public abstract <T> T accept(PieceVisitor<T> visitor); public abstract <T> T accept(PieceVisitor<T> visitor);
} }

View File

@@ -237,7 +237,7 @@ public class Window extends JFrame implements OutputSystem {
} }
if (choosedType != null) if (choosedType != null)
sendCommand(new PromoteCommand(choosedType, pieceCoords)); sendCommand(new PromoteCommand(choosedType));
}); });
} }