forked from pabulaner/vivi
69 lines
1.9 KiB
Java
69 lines
1.9 KiB
Java
package de.vivi.chess.pieces;
|
|
|
|
import de.vivi.chess.board.Board;
|
|
import de.vivi.chess.board.Field;
|
|
|
|
public class Rook extends Piece {
|
|
|
|
private boolean isValid = false;
|
|
|
|
public Rook(Color color, Type type) {
|
|
super(color, type);
|
|
}
|
|
|
|
public Rook(Color color, Field field) {
|
|
super(color, field);
|
|
}
|
|
|
|
public Rook(Color color, Type type, Field field) {
|
|
super(color, type, field);
|
|
}
|
|
|
|
@Override
|
|
public char getSymbol() {
|
|
if (getColor() == Color.WHITE) {
|
|
if (getType() == Type.ROOK) {
|
|
symbol = '\u265C';
|
|
}
|
|
|
|
} else if (getColor() == Color.BLACK) {
|
|
if (getType() == Type.ROOK) {
|
|
symbol = '\u2656';
|
|
}
|
|
}
|
|
return symbol;
|
|
}
|
|
|
|
@Override
|
|
public boolean isValidMove(Board board, Field from, Field to) {
|
|
if (from.getRow() == to.getRow()) {
|
|
int columnStart = Math.min(from.getColumn(), to.getColumn()) + 1;
|
|
int columnEnd = Math.max(from.getColumn(), to.getColumn());
|
|
for (int column = columnStart; column < columnEnd; column++) {
|
|
if (board.getPiece(Field.from(column, from.getRow())) != null) {
|
|
return false;
|
|
}
|
|
}
|
|
} else if (from.getColumn() == to.getColumn()) {
|
|
int rowStart = Math.min(from.getRow(), to.getRow()) + 1;
|
|
int rowEnd = Math.max(from.getRow(), to.getRow());
|
|
for (int row = rowStart; row < rowEnd; row++) {
|
|
if (board.getPiece(Field.from(from.getColumn(), row)) != null) {
|
|
return false;
|
|
}
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
|
|
Piece destinationPiece = board.getPiece(to);
|
|
if (destinationPiece == null) {
|
|
return true;
|
|
} else if (destinationPiece.getColor() != this.getColor()) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|