Compare commits
2 Commits
55ef180f57
...
810934aea1
| Author | SHA1 | Date | |
|---|---|---|---|
| 810934aea1 | |||
| 48a215eae5 |
@@ -1,8 +1,8 @@
|
||||
package chess.io;
|
||||
|
||||
import chess.io.commands.MoveCommand;
|
||||
import chess.io.commands.PromoteCommand;
|
||||
import chess.model.Game;
|
||||
import chess.model.Game.GameStatus;
|
||||
|
||||
public class CommandExecutor {
|
||||
|
||||
@@ -32,49 +32,59 @@ public class CommandExecutor {
|
||||
return;
|
||||
|
||||
if (command instanceof MoveCommand move) {
|
||||
boolean needsPromote = this.game.checkPromotion(move.getMove().getFinish());
|
||||
if (this.game.checkGameStatus())
|
||||
boolean needsPromote = this.game.pawnShouldBePromoted();
|
||||
if (needsPromote)
|
||||
this.outputSystem.promotePawn(move.getMove().getFinish());
|
||||
|
||||
if (checkGameStatus())
|
||||
return;
|
||||
|
||||
if(!needsPromote)
|
||||
this.game.switchPlayerTurn();
|
||||
switchPlayerTurn();
|
||||
} 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) {
|
||||
unbindListeners();
|
||||
this.outputSystem = outputSystem;
|
||||
bindListeners();
|
||||
}
|
||||
|
||||
public void setGame(Game game) {
|
||||
unbindListeners();
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,10 +10,13 @@ import chess.model.Piece;
|
||||
import chess.model.visitor.PiecePathChecker;
|
||||
|
||||
public class MoveCommand extends PlayerCommand {
|
||||
|
||||
private final Move move;
|
||||
private Piece deadPiece;
|
||||
|
||||
public MoveCommand(Move move) {
|
||||
this.move = move;
|
||||
this.deadPiece = null;
|
||||
}
|
||||
|
||||
public Move getMove() {
|
||||
@@ -39,6 +42,7 @@ public class MoveCommand extends PlayerCommand {
|
||||
if (!valid)
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
this.deadPiece = board.pieceAt(move.getFinish());
|
||||
board.applyMove(move);
|
||||
|
||||
if (board.isKingInCheck(game.getPlayerTurn())) {
|
||||
@@ -51,7 +55,9 @@ public class MoveCommand extends PlayerCommand {
|
||||
|
||||
@Override
|
||||
public void undo(Game game, OutputSystem outputSystem) {
|
||||
game.getBoard().undoLastMove();
|
||||
final ChessBoard board = game.getBoard();
|
||||
|
||||
board.undoMove(move, deadPiece);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,7 +16,9 @@ import chess.model.pieces.Rook;
|
||||
|
||||
public class NewGameCommand extends Command {
|
||||
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++) {
|
||||
board.pieceComes(new Pawn(Color.Black), new Coordinate(i, 1));
|
||||
@@ -50,6 +52,7 @@ public class NewGameCommand extends Command {
|
||||
game.resetPlayerTurn();
|
||||
|
||||
outputSystem.gameStarted();
|
||||
outputSystem.playerTurn(game.getPlayerTurn());
|
||||
|
||||
return CommandResult.NotMoved;
|
||||
}
|
||||
|
||||
@@ -24,17 +24,21 @@ public class PromoteCommand extends PlayerCommand {
|
||||
}
|
||||
|
||||
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.pieceCoords = pieceCoords;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||
final ChessBoard board = game.getBoard();
|
||||
|
||||
this.pieceCoords = game.pawnPromotePosition();
|
||||
|
||||
if (this.pieceCoords == null)
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
Piece pawn = board.pieceAt(this.pieceCoords);
|
||||
if (pawn == null || !(pawn instanceof Pawn))
|
||||
return CommandResult.NotAllowed;
|
||||
@@ -46,7 +50,6 @@ public class PromoteCommand extends PlayerCommand {
|
||||
if (destY != enemyLine)
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
board.pieceLeaves(this.pieceCoords);
|
||||
board.pieceComes(createPiece(this.promoteType, pawn.getColor()), this.pieceCoords);
|
||||
|
||||
return CommandResult.Moved;
|
||||
@@ -80,7 +83,6 @@ public class PromoteCommand extends PlayerCommand {
|
||||
assert promoted != null;
|
||||
|
||||
Color player = promoted.getColor();
|
||||
board.pieceLeaves(this.pieceCoords);
|
||||
board.pieceComes(new Pawn(player), this.pieceCoords);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package chess.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
import chess.model.visitor.KingIdentifier;
|
||||
import chess.model.visitor.PiecePathChecker;
|
||||
@@ -26,8 +25,8 @@ public class ChessBoard {
|
||||
}
|
||||
|
||||
private final Cell[][] cells;
|
||||
private final Stack<Move> moves;
|
||||
private final Stack<Piece> ejectedPieces;
|
||||
private Move lastMove;
|
||||
private Piece lastEjectedPiece;
|
||||
|
||||
public ChessBoard() {
|
||||
this.cells = new Cell[Coordinate.VALUE_MAX][Coordinate.VALUE_MAX];
|
||||
@@ -36,43 +35,36 @@ public class ChessBoard {
|
||||
this.cells[i][j] = new Cell();
|
||||
}
|
||||
}
|
||||
this.moves = new Stack<>();
|
||||
this.ejectedPieces = new Stack<>();
|
||||
this.lastMove = null;
|
||||
this.lastEjectedPiece = null;
|
||||
}
|
||||
|
||||
public void applyMove(Move move) {
|
||||
assert (move.isValid());
|
||||
assert move.isValid() : "Invalid move !";
|
||||
Piece deadPiece = pieceAt(move.getFinish());
|
||||
if (deadPiece != null) {
|
||||
deadPiece.eject(this.moves.size());
|
||||
this.ejectedPieces.add(deadPiece);
|
||||
this.lastEjectedPiece = deadPiece;
|
||||
} else {
|
||||
this.lastEjectedPiece = null;
|
||||
}
|
||||
Piece movingPiece = pieceAt(move.getStart());
|
||||
pieceComes(movingPiece, move.getFinish());
|
||||
pieceLeaves(move.getStart());
|
||||
movingPiece.move();
|
||||
this.moves.add(move);
|
||||
this.lastMove = move;
|
||||
}
|
||||
|
||||
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());
|
||||
pieceComes(movingPiece, move.getStart());
|
||||
pieceLeaves(move.getFinish());
|
||||
pieceComes(deadPiece, move.getFinish());
|
||||
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) {
|
||||
|
||||
@@ -1,20 +1,14 @@
|
||||
package chess.model;
|
||||
|
||||
import chess.model.pieces.Pawn;
|
||||
import common.Signal0;
|
||||
import common.Signal1;
|
||||
|
||||
public class Game {
|
||||
private final ChessBoard board;
|
||||
private Color playerTurn;
|
||||
|
||||
public final Signal1<Coordinate> OnPromote = new Signal1<>();
|
||||
public final Signal1<Color> OnWin = new Signal1<>();
|
||||
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 enum GameStatus {
|
||||
Check, CheckMate, OnGoing, Pat;
|
||||
}
|
||||
|
||||
public Game(ChessBoard board) {
|
||||
this.board = board;
|
||||
@@ -30,70 +24,60 @@ public class Game {
|
||||
|
||||
public void resetPlayerTurn() {
|
||||
this.playerTurn = Color.White;
|
||||
this.OnPlayerTurn.emit(playerTurn);
|
||||
}
|
||||
|
||||
public void switchPlayerTurn() {
|
||||
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))
|
||||
return false;
|
||||
if (!board.hasAllowedMoves(enemy))
|
||||
return GameStatus.Pat;
|
||||
|
||||
int destY = pieceCoords.getY();
|
||||
return GameStatus.OnGoing;
|
||||
}
|
||||
|
||||
int enemyLine = piece.getColor() == Color.White ? 0 : 7;
|
||||
|
||||
if (destY != enemyLine)
|
||||
return false;
|
||||
|
||||
this.OnPromote.emit(pieceCoords);
|
||||
return true;
|
||||
public boolean pawnShouldBePromoted() {
|
||||
return pawnPromotePosition() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return true if game should end
|
||||
* @return Null if there is no pawn to promote
|
||||
*/
|
||||
public boolean checkGameStatus() {
|
||||
final Color enemy = Color.getEnemy(getPlayerTurn());
|
||||
|
||||
if (this.board.isKingInCheck(enemy)) {
|
||||
if (this.board.hasAllowedMoves(enemy)) {
|
||||
this.OnCheck.emit();
|
||||
} else {
|
||||
this.OnMat.emit();
|
||||
this.OnWin.emit(getPlayerTurn());
|
||||
return true;
|
||||
}
|
||||
} else if (!board.hasAllowedMoves(enemy)) {
|
||||
this.OnPat.emit();
|
||||
return true;
|
||||
public Coordinate pawnPromotePosition() {
|
||||
Coordinate piecePos = pawnPromotePosition(Color.White);
|
||||
if (piecePos != null)
|
||||
return piecePos;
|
||||
return pawnPromotePosition(Color.Black);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean pawnShouldBePromoted() {
|
||||
if (pawnShouldBePromoted(Color.White))
|
||||
return true;
|
||||
return pawnShouldBePromoted(Color.Black);
|
||||
}
|
||||
|
||||
private boolean pawnShouldBePromoted(Color color) {
|
||||
/**
|
||||
*
|
||||
* @return Null if there is no pawn to promote
|
||||
*/
|
||||
private Coordinate pawnPromotePosition(Color color) {
|
||||
int enemyLineY = color == Color.White ? 0 : 7;
|
||||
|
||||
for (int x = 0; x < Coordinate.VALUE_MAX; x++) {
|
||||
if (checkPromotion(new Coordinate(x, enemyLineY)))
|
||||
return true;
|
||||
Coordinate pieceCoords = new Coordinate(x, enemyLineY);
|
||||
Piece piece = getBoard().pieceAt(pieceCoords);
|
||||
|
||||
if (piece == null || !(piece instanceof Pawn) || piece.getColor() != color)
|
||||
continue;
|
||||
|
||||
return pieceCoords;
|
||||
}
|
||||
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,12 +4,10 @@ public abstract class Piece {
|
||||
|
||||
private final Color color;
|
||||
private int moved;
|
||||
private int ejectedMoveNumber;
|
||||
|
||||
public Piece(Color color) {
|
||||
this.color = color;
|
||||
this.moved = 0;
|
||||
this.ejectedMoveNumber = -1;
|
||||
}
|
||||
|
||||
public void move() {
|
||||
@@ -28,18 +26,6 @@ public abstract class Piece {
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
@@ -237,7 +237,7 @@ public class Window extends JFrame implements OutputSystem {
|
||||
}
|
||||
|
||||
if (choosedType != null)
|
||||
sendCommand(new PromoteCommand(choosedType, pieceCoords));
|
||||
sendCommand(new PromoteCommand(choosedType));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user