promote not that uggly
This commit is contained in:
@@ -3,8 +3,8 @@ package chess.io.commands;
|
||||
import chess.io.Command;
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
import chess.io.commands.PromoteCommand.PromoteType;
|
||||
import chess.model.ChessBoard;
|
||||
import chess.model.Color;
|
||||
import chess.model.Game;
|
||||
import chess.model.Move;
|
||||
import chess.model.Piece;
|
||||
@@ -17,6 +17,10 @@ public class MoveCommand extends Command {
|
||||
this.move = move;
|
||||
}
|
||||
|
||||
public Move getMove() {
|
||||
return move;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||
final ChessBoard board = game.getBoard();
|
||||
@@ -39,25 +43,7 @@ public class MoveCommand extends Command {
|
||||
return CommandResult.NotAllowed;
|
||||
}
|
||||
|
||||
checkGameStatus(game, outputSystem);
|
||||
|
||||
return CommandResult.Moved;
|
||||
}
|
||||
|
||||
private void checkGameStatus(Game game, OutputSystem outputSystem) {
|
||||
final ChessBoard board = game.getBoard();
|
||||
|
||||
final Color enemy = Color.getEnemy(game.getPlayerTurn());
|
||||
|
||||
if (board.isKingInCheck(enemy)) {
|
||||
if (board.hasAllowedMoves(enemy)) {
|
||||
outputSystem.kingIsInCheck();
|
||||
} else {
|
||||
outputSystem.kingIsInMat();
|
||||
outputSystem.winnerIs(game.getPlayerTurn());
|
||||
}
|
||||
} else if(!board.hasAllowedMoves(enemy)) {
|
||||
outputSystem.patSituation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
74
app/src/main/java/chess/io/commands/PromoteCommand.java
Normal file
74
app/src/main/java/chess/io/commands/PromoteCommand.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package chess.io.commands;
|
||||
|
||||
import chess.io.Command;
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
import chess.model.ChessBoard;
|
||||
import chess.model.Color;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Game;
|
||||
import chess.model.Piece;
|
||||
import chess.model.pieces.Bishop;
|
||||
import chess.model.pieces.Knight;
|
||||
import chess.model.pieces.Pawn;
|
||||
import chess.model.pieces.Queen;
|
||||
import chess.model.pieces.Rook;
|
||||
|
||||
public class PromoteCommand extends Command {
|
||||
|
||||
public enum PromoteType {
|
||||
Queen,
|
||||
Rook,
|
||||
Bishop,
|
||||
Knight
|
||||
}
|
||||
|
||||
private final PromoteType promoteType;
|
||||
private final Coordinate pieceCoords;
|
||||
|
||||
public PromoteCommand(PromoteType promoteType, Coordinate pieceCoords) {
|
||||
this.promoteType = promoteType;
|
||||
this.pieceCoords = pieceCoords;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||
final ChessBoard board = game.getBoard();
|
||||
|
||||
Piece pawn = board.pieceAt(this.pieceCoords);
|
||||
if (pawn == null || !(pawn instanceof Pawn))
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
int destY = this.pieceCoords.getY();
|
||||
|
||||
int enemyLine = pawn.getColor() == Color.White ? 0 : 7;
|
||||
|
||||
if (destY != enemyLine)
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
board.pieceLeaves(this.pieceCoords);
|
||||
board.pieceComes(createPiece(this.promoteType, pawn.getColor()), this.pieceCoords);
|
||||
|
||||
return CommandResult.NotMoved;
|
||||
}
|
||||
|
||||
private Piece createPiece(PromoteType promoteType, Color color) {
|
||||
switch (promoteType) {
|
||||
case Queen:
|
||||
return new Queen(color);
|
||||
|
||||
case Bishop:
|
||||
return new Bishop(color);
|
||||
|
||||
case Knight:
|
||||
return new Knight(color);
|
||||
|
||||
case Rook:
|
||||
return new Rook(color);
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user