forked from pabulaner/vivi
45 lines
1.2 KiB
Java
45 lines
1.2 KiB
Java
|
|
package de.vivi.chess.pieces;
|
||
|
|
|
||
|
|
import de.vivi.chess.board.Board;
|
||
|
|
import de.vivi.chess.board.Field;
|
||
|
|
|
||
|
|
public class King extends Piece {
|
||
|
|
public King(Color color, Type type) {
|
||
|
|
super(color, type);
|
||
|
|
}
|
||
|
|
|
||
|
|
public King(Color color, Type type, Field field) {
|
||
|
|
super(color, type, field);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public char getSymbol() {
|
||
|
|
if (getColor() == Color.WHITE) {
|
||
|
|
if (getType() == Type.KING) {
|
||
|
|
symbol = '\u265A';
|
||
|
|
}
|
||
|
|
|
||
|
|
} else if (getColor() == Color.BLACK) {
|
||
|
|
if (getType() == Type.KING) {
|
||
|
|
symbol = '\u2654';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return symbol;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public boolean isValidMove(Board board, Field from, Field to) {
|
||
|
|
int rowDiff = Math.abs(from.getRow() - to.getRow());
|
||
|
|
int colDiff = Math.abs(from.getColumn() - to.getColumn());
|
||
|
|
|
||
|
|
boolean isOneSquareMove = rowDiff <= 1 && colDiff <= 1 && !(rowDiff == 0 && colDiff == 0);
|
||
|
|
|
||
|
|
if (!isOneSquareMove) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
Piece destinationPiece = board.getPiece(Field.from(to.getColumn(), to.getRow()));
|
||
|
|
return destinationPiece == null || destinationPiece.getColor() != this.getColor();
|
||
|
|
}
|
||
|
|
}
|