40 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			40 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
package chess;
 | 
						|
 | 
						|
import static java.lang.Integer.signum;
 | 
						|
import static java.lang.Math.abs;
 | 
						|
 | 
						|
public class Rook extends Piece{
 | 
						|
    public Rook(Color color, Board board, int row, int col) {
 | 
						|
        super(color, board, row, col);
 | 
						|
      }
 | 
						|
    
 | 
						|
      @Override
 | 
						|
      public char charRep() {
 | 
						|
        return getColor() == Color.white ? 'r' : 'R';
 | 
						|
      }
 | 
						|
    
 | 
						|
      @Override
 | 
						|
      public String toString() {
 | 
						|
        return "" + getColor() + " rook at (" + getRow() + ", " + getCol() + ")";
 | 
						|
      }
 | 
						|
    
 | 
						|
      @Override
 | 
						|
      public boolean canCapture(Piece other) {
 | 
						|
        if (getBoard() != other.getBoard() || getColor() == other.getColor())
 | 
						|
          return false;
 | 
						|
        if (other.getRow() != getRow() &&
 | 
						|
            other.getCol() != getCol())
 | 
						|
          return false;
 | 
						|
        final int dr = signum(other.getRow() - getRow());
 | 
						|
        final int dc = signum(other.getCol() - getCol());
 | 
						|
        int r = getRow() + dr;
 | 
						|
        int c = getCol() + dc;
 | 
						|
        while (r != other.getRow() || c != other.getCol()) {
 | 
						|
          if (getBoard().pieceAt(r, c) != null) return false;
 | 
						|
          r += dr;
 | 
						|
          c += dc;
 | 
						|
        }
 | 
						|
        return true;
 | 
						|
      }
 | 
						|
}
 |