added uebung04 task
This commit is contained in:
28
uebung04/chess/Knight.java
Normal file
28
uebung04/chess/Knight.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package chess;
|
||||
|
||||
import static java.lang.Math.abs;
|
||||
|
||||
public class Knight extends Piece {
|
||||
public Knight(Color color, Board board, int row, int col) {
|
||||
super(color, board, row, col);
|
||||
}
|
||||
|
||||
@Override
|
||||
public char charRep() {
|
||||
return getColor() == Color.white ? 'n' : 'N';
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "" + getColor() + " knight at (" + getRow() + ", " + getCol() + ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canCapture(Piece other) {
|
||||
if (getBoard() != other.getBoard() || getColor() == other.getColor())
|
||||
return false;
|
||||
final int dr = abs(getRow() - other.getRow());
|
||||
final int dc = abs(getCol() - other.getCol());
|
||||
return dr == 2 && dc == 1 || dr == 1 && dc == 2;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user