package chess; import java.awt.*; import java.util.ArrayList; public abstract class ChessPiece { protected Board b; protected int x, y; protected boolean isInDanger, isWhite; protected int moveCount = 0; public ChessPiece(Board b, boolean isWhite, int x, int y) { this.b = b; this.x = x; this.y = y; this.isWhite = isWhite; } public void setInDanger(boolean isInDanger){ this.isInDanger = isInDanger; } public boolean isWhite(){ return isWhite; } public Move getPosition(){ return new Move(x, y); } public boolean isFirstMove(){ return moveCount < 1; } public boolean isSecondMove(){ return moveCount <= 1; } public void move(Move m){ this.x = m.getX(); this.y = m.getY(); this.moveCount++; } public abstract ArrayList getMoves(); public abstract Image getImage(); public void applySpecialMove(Move moveWithSpecial){} protected ArrayList getCardinalMoves(int length){ ArrayList moves = new ArrayList(); // cardinals for (int i = 1; i <= length; i++){ if (add(moves, new Move(x - i, y))) break; } for (int i = 1; i <= length; i++){ if (add(moves, new Move(x + i, y))) break; } for (int i = 1; i <= length; i++){ if (add(moves, new Move(x, y - i))) break; } for (int i = 1; i <= length; i++){ if (add(moves, new Move(x, y + i))) break; } return moves; } protected ArrayList getDiagonalMoves(int length){ ArrayList moves = new ArrayList(); // diagonals for (int i = 1; i <= length; i++){ if (add(moves, new Move(x - i, y - i))) break; } for (int i = 1; i <= length; i++){ if (add(moves, new Move(x - i, y + i))) break; } for (int i = 1; i <= length; i++){ if (add(moves, new Move(x + i, y - i))) break; } for (int i = 1; i <= length; i++){ if (add(moves, new Move(x + i, y + i))) break; } return moves; } protected ArrayList prune(ArrayList moves){ var prunedMoves = new ArrayList(); // get rid of impossible moves for (Move m : moves){ if (m == null) continue; if (m.getX() < 0 || m.getX() >= b.size()) continue; if (m.getY() < 0 || m.getY() >= b.size()) continue; prunedMoves.add(m); } return prunedMoves; } /** * Checks to make sure that the proposed move is valid. * @param moves array to add to * @param move move to add to the array * @return true if we need to stop here. (Break the loop) */ private boolean add(ArrayList moves, Move move){ var m = b.get(move); if (m != null && m.isWhite != isWhite) { moves.add(b.checkIfMoveValid(move, isWhite)); return true; } else { if (m != null) return true; moves.add(b.checkIfMoveValid(move, isWhite)); } return false; } }