package Pieces; import Main.Board; import java.util.ArrayList; public class Rook extends Piece { public Rook(int x, int y, boolean isWhite, Board board) { super(x, y, isWhite, board); } @Override public boolean isCanMove(int dx, int dy) { if(isOutside(dx, dy)) return false; if(dx ==getX()) return true; if(dy == getY()) return true; return false; } @Override public boolean isValidMove(int dx, int dy) { if(!isCanMove(dx, dy))return false; for(int i = Math.min(dx,getX());i< Math.max(dx,getX());i++){ for(int j =Math.min(dy,getY());i < Math.max(dy,getY());j++){ if(getBoard().getBoard()[i][j]!=null) return false; } } return true; } @Override public ArrayList moves() { ArrayList moves = new ArrayList<>(); int x = getX(); int y = getY(); int tempx=x-1; while(tempx>=0) { if(getBoard().isEmpty(tempx,y)) moves.add(new int[]{tempx,y}); else if((getBoard().getColor(tempx,y)==1 && this.isWhite()) || (getBoard().getColor(tempx,y)==2 && this.isBlack())) break; else { moves.add(new int[]{tempx,y}); break; } tempx--; } tempx=x+1; while(tempx<8) { if(getBoard().isEmpty(tempx,y)) moves.add(new int[]{tempx,y}); else if((getBoard().getColor(tempx,y)==1 && this.isWhite()) || (getBoard().getColor(tempx,y)==2 && this.isBlack())) break; else { moves.add(new int[]{tempx,y}); break; } tempx++; } int tempy=y-1; while(tempy>=0) { if(getBoard().isEmpty(x,tempy)) moves.add(new int[]{x,tempy}); else if((getBoard().getColor(x,tempy)==1 && this.isWhite()) || (getBoard().getColor(x,tempy)==2 && this.isBlack())) break; else { moves.add(new int[]{x,tempy}); break; } tempy--; } tempy=y+1; while(tempy<8) { if(getBoard().isEmpty(x,tempy)) moves.add(new int[]{x,tempy}); else if((getBoard().getColor(x,tempy)==1 && this.isWhite()) || (getBoard().getColor(x,tempy)==2 && this.isBlack())) break; else { moves.add(new int[]{x,tempy}); break; } tempy++; } return moves; } @Override public String toString() { return "R"; } }