remove "instanceof Pawn"
This commit is contained in:
@@ -12,6 +12,7 @@ import chess.model.pieces.Knight;
|
||||
import chess.model.pieces.Pawn;
|
||||
import chess.model.pieces.Queen;
|
||||
import chess.model.pieces.Rook;
|
||||
import chess.model.visitor.PawnIdentifier;
|
||||
|
||||
public class PromoteCommand extends PlayerCommand {
|
||||
|
||||
@@ -39,7 +40,7 @@ public class PromoteCommand extends PlayerCommand {
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
Piece pawn = board.pieceAt(this.pieceCoords);
|
||||
if (pawn == null || !(pawn instanceof Pawn))
|
||||
if (!new PawnIdentifier(game.getPlayerTurn()).isPawn(pawn))
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
int destY = this.pieceCoords.getY();
|
||||
|
||||
@@ -103,7 +103,7 @@ public class ChessBoard {
|
||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||
Coordinate coordinate = new Coordinate(i, j);
|
||||
Piece piece = pieceAt(coordinate);
|
||||
if (piece != null && kingIdentifier.visit(piece)) {
|
||||
if (kingIdentifier.isKing(piece)) {
|
||||
return coordinate;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package chess.model;
|
||||
|
||||
import chess.model.pieces.Pawn;
|
||||
import chess.model.visitor.PawnIdentifier;
|
||||
|
||||
public class Game {
|
||||
private final ChessBoard board;
|
||||
@@ -66,15 +66,15 @@ public class Game {
|
||||
*/
|
||||
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 (piece == null || !(piece instanceof Pawn) || piece.getColor() != color)
|
||||
continue;
|
||||
|
||||
if (identifier.isPawn(piece))
|
||||
return pieceCoords;
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package chess.model.visitor;
|
||||
|
||||
import chess.model.Color;
|
||||
import chess.model.Piece;
|
||||
import chess.model.PieceVisitor;
|
||||
import chess.model.pieces.*;
|
||||
|
||||
@@ -12,6 +13,12 @@ public class KingIdentifier implements PieceVisitor<Boolean> {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public boolean isKing(Piece piece) {
|
||||
if (piece == null)
|
||||
return false;
|
||||
return visit(piece);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean visitPiece(Bishop bishop) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user