2019-07-06 18:05:00 +02:00
|
|
|
package dev.kske.chess.board;
|
2019-07-01 19:08:37 +02:00
|
|
|
|
2019-07-05 14:14:48 +02:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-07-01 19:08:37 +02:00
|
|
|
/**
|
|
|
|
* Project: <strong>Chess</strong><br>
|
|
|
|
* File: <strong>Piece.java</strong><br>
|
|
|
|
* Created: <strong>01.07.2019</strong><br>
|
|
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
|
|
*/
|
2019-07-08 06:41:10 +02:00
|
|
|
public abstract class Piece implements Cloneable {
|
2019-07-01 19:08:37 +02:00
|
|
|
|
2019-07-13 11:38:44 +02:00
|
|
|
private final Color color;
|
|
|
|
protected Board board;
|
|
|
|
private int moveCounter;
|
2019-07-02 13:49:36 +02:00
|
|
|
|
|
|
|
public Piece(Color color, Board board) {
|
|
|
|
this.color = color;
|
|
|
|
this.board = board;
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:14:48 +02:00
|
|
|
public List<Move> getMoves(Position pos) {
|
|
|
|
List<Move> moves = getPseudolegalMoves(pos);
|
|
|
|
for (Iterator<Move> iterator = moves.iterator(); iterator.hasNext();) {
|
2019-07-13 11:38:44 +02:00
|
|
|
Move move = iterator.next();
|
2019-07-10 18:54:53 +02:00
|
|
|
board.move(move);
|
2019-07-13 11:38:44 +02:00
|
|
|
if (board.checkCheck(getColor())) iterator.remove();
|
2019-07-10 18:54:53 +02:00
|
|
|
board.revert();
|
2019-07-05 14:14:48 +02:00
|
|
|
}
|
|
|
|
return moves;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract List<Move> getPseudolegalMoves(Position pos);
|
|
|
|
|
2019-07-02 13:49:36 +02:00
|
|
|
public abstract boolean isValidMove(Move move);
|
2019-07-01 19:37:15 +02:00
|
|
|
|
2019-07-22 21:40:25 +02:00
|
|
|
/**
|
|
|
|
* Checks, if the squares between the position and the destination of a move are
|
|
|
|
* free.
|
|
|
|
*
|
|
|
|
* @param move The move to check
|
|
|
|
*/
|
2019-07-02 13:49:36 +02:00
|
|
|
protected boolean isFreePath(Move move) {
|
2019-07-22 21:40:25 +02:00
|
|
|
for (int i = move.pos.x + move.xSign, j = move.pos.y + move.ySign; i != move.dest.x
|
|
|
|
|| j != move.dest.y; i += move.xSign, j += move.ySign)
|
|
|
|
if (board.getBoardArr()[i][j] != null) return false;
|
2019-07-02 13:49:36 +02:00
|
|
|
return checkDestination(move);
|
2019-07-01 19:37:15 +02:00
|
|
|
}
|
|
|
|
|
2019-07-02 13:49:36 +02:00
|
|
|
/**
|
|
|
|
* Checks if the destination of a move is empty or a piece from the opposing
|
|
|
|
* team
|
|
|
|
*
|
|
|
|
* @param move The move to check
|
|
|
|
* @return {@code false} if the move's destination is from the same team
|
|
|
|
*/
|
|
|
|
protected final boolean checkDestination(Move move) {
|
|
|
|
return board.getDest(move) == null || board.getDest(move).getColor() != getColor();
|
|
|
|
}
|
2019-07-01 21:46:30 +02:00
|
|
|
|
2019-07-08 06:41:10 +02:00
|
|
|
@Override
|
|
|
|
public Object clone() {
|
|
|
|
Piece piece = null;
|
|
|
|
try {
|
|
|
|
piece = (Piece) super.clone();
|
|
|
|
} catch (CloneNotSupportedException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
return piece;
|
|
|
|
}
|
|
|
|
|
2019-07-01 19:08:37 +02:00
|
|
|
public abstract Type getType();
|
|
|
|
|
2019-07-01 19:37:15 +02:00
|
|
|
public Color getColor() { return color; }
|
2019-07-02 13:49:36 +02:00
|
|
|
|
2019-07-13 11:38:44 +02:00
|
|
|
public int getMoveCounter() { return moveCounter; }
|
|
|
|
|
|
|
|
public void incMoveCounter() {
|
|
|
|
++moveCounter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decMoveCounter() {
|
|
|
|
--moveCounter;
|
|
|
|
}
|
|
|
|
|
2019-07-01 19:08:37 +02:00
|
|
|
public static enum Type {
|
2019-09-09 19:05:57 +02:00
|
|
|
KING, QUEEN, ROOK, KNIGHT, BISHOP, PAWN;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The first character of this {@link Type} in algebraic notation and lower case
|
|
|
|
*/
|
|
|
|
public char firstChar() {
|
|
|
|
return this == KNIGHT ? 'n' : Character.toLowerCase(this.toString().charAt(0));
|
|
|
|
}
|
2019-07-01 19:08:37 +02:00
|
|
|
}
|
2019-07-01 19:37:15 +02:00
|
|
|
|
|
|
|
public static enum Color {
|
|
|
|
WHITE, BLACK;
|
2019-07-05 14:14:48 +02:00
|
|
|
|
2019-08-05 21:02:54 +02:00
|
|
|
public static Color fromFirstChar(char c) {
|
|
|
|
return Character.toLowerCase(c) == 'w' ? WHITE : BLACK;
|
2019-07-05 14:14:48 +02:00
|
|
|
}
|
2019-07-24 07:32:59 +02:00
|
|
|
|
|
|
|
public char firstChar() {
|
|
|
|
return this == WHITE ? 'w' : 'b';
|
|
|
|
}
|
2019-08-05 21:02:54 +02:00
|
|
|
|
|
|
|
public Color opposite() {
|
|
|
|
return this == WHITE ? BLACK : WHITE;
|
|
|
|
}
|
2019-07-01 19:37:15 +02:00
|
|
|
}
|
2019-07-01 19:08:37 +02:00
|
|
|
}
|