diff --git a/app/src/main/java/chess/controller/commands/CastlingCommand.java b/app/src/main/java/chess/controller/commands/CastlingCommand.java index 1f48004..ce96453 100644 --- a/app/src/main/java/chess/controller/commands/CastlingCommand.java +++ b/app/src/main/java/chess/controller/commands/CastlingCommand.java @@ -20,11 +20,11 @@ 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; - final ChessBoard board = game.getBoard(); + + // we must promote the pending pawn before + if (board.pawnShouldBePromoted()) + return CommandResult.NotAllowed; if (bigCastling && !board.canBigCastle(game.getPlayerTurn())) return CommandResult.NotAllowed; diff --git a/app/src/main/java/chess/controller/commands/MoveCommand.java b/app/src/main/java/chess/controller/commands/MoveCommand.java index 1ff40c5..c20a32d 100644 --- a/app/src/main/java/chess/controller/commands/MoveCommand.java +++ b/app/src/main/java/chess/controller/commands/MoveCommand.java @@ -28,7 +28,7 @@ public class MoveCommand extends PlayerCommand { final ChessBoard board = game.getBoard(); // we must promote the pending pawn before - if (game.pawnShouldBePromoted()) + if (board.pawnShouldBePromoted()) return CommandResult.NotAllowed; Piece piece = board.pieceAt(move.getStart()); @@ -50,7 +50,7 @@ public class MoveCommand extends PlayerCommand { return CommandResult.NotAllowed; } - if (game.pawnShouldBePromoted()) + if (board.pawnShouldBePromoted()) return CommandResult.ActionNeeded; board.setLastMove(this.move); @@ -71,7 +71,7 @@ public class MoveCommand extends PlayerCommand { } private void tryPromote(Game game, OutputSystem outputSystem) { - Coordinate pawnPos = game.pawnPromotePosition(); + Coordinate pawnPos = game.getBoard().pawnPromotePosition(); if (pawnPos == null) return; diff --git a/app/src/main/java/chess/controller/commands/PromoteCommand.java b/app/src/main/java/chess/controller/commands/PromoteCommand.java index fcdf853..5bb0326 100644 --- a/app/src/main/java/chess/controller/commands/PromoteCommand.java +++ b/app/src/main/java/chess/controller/commands/PromoteCommand.java @@ -36,7 +36,7 @@ public class PromoteCommand extends PlayerCommand { public CommandResult execute(Game game, OutputSystem outputSystem) { final ChessBoard board = game.getBoard(); - this.pieceCoords = game.pawnPromotePosition(); + this.pieceCoords = board.pawnPromotePosition(); if (this.pieceCoords == null) return CommandResult.NotAllowed; diff --git a/app/src/main/java/chess/model/ChessBoard.java b/app/src/main/java/chess/model/ChessBoard.java index f74e330..3967c44 100644 --- a/app/src/main/java/chess/model/ChessBoard.java +++ b/app/src/main/java/chess/model/ChessBoard.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import chess.model.visitor.KingIdentifier; +import chess.model.visitor.PawnIdentifier; import chess.model.visitor.PiecePathChecker; public class ChessBoard { @@ -220,6 +221,42 @@ public class ChessBoard { return canCastle(color, 0, Direction.Left); } + public boolean pawnShouldBePromoted() { + return pawnPromotePosition() != null; + } + + /** + * + * @return Null if there is no pawn to promote + */ + public Coordinate pawnPromotePosition() { + Coordinate piecePos = pawnPromotePosition(Color.White); + if (piecePos != null) + return piecePos; + return pawnPromotePosition(Color.Black); + } + + /** + * + * @return Null if there is no pawn to promote + */ + private Coordinate pawnPromotePosition(Color color) { + int enemyLineY = color == Color.White ? 0 : 7; + PawnIdentifier identifier = new PawnIdentifier(color); + + for (int x = 0; x < Coordinate.VALUE_MAX; x++) { + Coordinate pieceCoords = new Coordinate(x, enemyLineY); + Piece piece = pieceAt(pieceCoords); + + if (identifier.isPawn(piece)) + return pieceCoords; + + } + + return null; + } + + public Move getLastMove() { return this.lastMove; } diff --git a/app/src/main/java/chess/model/Game.java b/app/src/main/java/chess/model/Game.java index fd19c1b..92dfa39 100644 --- a/app/src/main/java/chess/model/Game.java +++ b/app/src/main/java/chess/model/Game.java @@ -4,7 +4,6 @@ import java.util.Stack; import chess.controller.PlayerCommand; import chess.controller.commands.MoveCommand; -import chess.model.visitor.PawnIdentifier; public class Game { private final ChessBoard board; @@ -51,41 +50,6 @@ public class Game { return GameStatus.OnGoing; } - public boolean pawnShouldBePromoted() { - return pawnPromotePosition() != null; - } - - /** - * - * @return Null if there is no pawn to promote - */ - public Coordinate pawnPromotePosition() { - Coordinate piecePos = pawnPromotePosition(Color.White); - if (piecePos != null) - return piecePos; - return pawnPromotePosition(Color.Black); - } - - /** - * - * @return Null if there is no pawn to promote - */ - private Coordinate pawnPromotePosition(Color color) { - int enemyLineY = color == Color.White ? 0 : 7; - PawnIdentifier identifier = new PawnIdentifier(color); - - for (int x = 0; x < Coordinate.VALUE_MAX; x++) { - Coordinate pieceCoords = new Coordinate(x, enemyLineY); - Piece piece = getBoard().pieceAt(pieceCoords); - - if (identifier.isPawn(piece)) - return pieceCoords; - - } - - return null; - } - public void addAction(PlayerCommand command) { this.movesHistory.add(command); }