uebung04
This commit is contained in:
39
uebung04/chess/Rook.java
Normal file
39
uebung04/chess/Rook.java
Normal file
@@ -0,0 +1,39 @@
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user