2019-07-01 19:50:42 +02:00
|
|
|
package dev.kske.chess.piece;
|
|
|
|
|
2019-07-05 14:14:48 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-07-02 13:49:36 +02:00
|
|
|
import dev.kske.chess.Board;
|
|
|
|
import dev.kske.chess.Move;
|
2019-07-05 14:14:48 +02:00
|
|
|
import dev.kske.chess.Position;
|
2019-07-02 13:49:36 +02:00
|
|
|
|
2019-07-01 19:50:42 +02:00
|
|
|
/**
|
|
|
|
* Project: <strong>Chess</strong><br>
|
|
|
|
* File: <strong>Queen.java</strong><br>
|
|
|
|
* Created: <strong>01.07.2019</strong><br>
|
|
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
|
|
*/
|
|
|
|
public class Queen extends Piece {
|
|
|
|
|
2019-07-02 13:49:36 +02:00
|
|
|
public Queen(Color color, Board board) {
|
|
|
|
super(color, board);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isValidMove(Move move) {
|
|
|
|
return ((move.isHorizontal() || move.isVertical()) || move.isDiagonal()) && isFreePath(move);
|
2019-07-01 19:50:42 +02:00
|
|
|
}
|
|
|
|
|
2019-07-01 21:46:30 +02:00
|
|
|
@Override
|
2019-07-02 13:49:36 +02:00
|
|
|
protected boolean isFreePath(Move move) {
|
|
|
|
if (move.isHorizontal()) {
|
2019-07-02 20:07:47 +02:00
|
|
|
for (int i = move.pos.x + move.xSign; i != move.dest.x; i += move.xSign)
|
|
|
|
if (board.getBoardArr()[i][move.pos.y] != null) return false;
|
2019-07-02 13:49:36 +02:00
|
|
|
} else if (move.isVertical()) {
|
2019-07-02 20:07:47 +02:00
|
|
|
for (int i = move.pos.y + move.ySign; i != move.dest.y; i += move.ySign)
|
|
|
|
if (board.getBoardArr()[move.pos.x][i] != null) return false;
|
2019-07-02 13:49:36 +02:00
|
|
|
} else {
|
2019-07-02 20:07:47 +02:00
|
|
|
for (int i = move.pos.x + move.xSign, j = move.pos.y
|
|
|
|
+ move.ySign; i != move.dest.x; i += move.xSign, j += move.ySign)
|
2019-07-02 13:49:36 +02:00
|
|
|
if (board.getBoardArr()[i][j] != null) return false;
|
|
|
|
}
|
|
|
|
return checkDestination(move);
|
2019-07-01 21:46:30 +02:00
|
|
|
}
|
|
|
|
|
2019-07-05 14:14:48 +02:00
|
|
|
@Override
|
|
|
|
protected List<Move> getPseudolegalMoves(Position pos) {
|
|
|
|
List<Move> moves = new ArrayList<>();
|
|
|
|
|
|
|
|
// Horizontal moves to the right
|
|
|
|
for (int i = pos.x + 1; i < 8; i++) {
|
|
|
|
Move move = new Move(pos, new Position(i, pos.y));
|
|
|
|
if (board.getDest(move) == null || board.getDest(move).getColor() != getColor()) {
|
|
|
|
moves.add(move);
|
|
|
|
if (board.getDest(move) != null) break;
|
|
|
|
} else break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Horizontal moves to the left
|
|
|
|
for (int i = pos.x - 1; i >= 0; i--) {
|
|
|
|
Move move = new Move(pos, new Position(i, pos.y));
|
|
|
|
if (board.getDest(move) == null || board.getDest(move).getColor() != getColor()) {
|
|
|
|
moves.add(move);
|
|
|
|
if (board.getDest(move) != null) break;
|
|
|
|
} else break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vertical moves to the top
|
|
|
|
for (int i = pos.y - 1; i >= 0; i--) {
|
|
|
|
Move move = new Move(pos, new Position(pos.x, i));
|
|
|
|
if (board.getDest(move) == null || board.getDest(move).getColor() != getColor()) {
|
|
|
|
moves.add(move);
|
|
|
|
if (board.getDest(move) != null) break;
|
|
|
|
} else break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vertical moves to the bottom
|
|
|
|
for (int i = pos.y + 1; i < 8; i++) {
|
|
|
|
Move move = new Move(pos, new Position(pos.x, i));
|
|
|
|
if (board.getDest(move) == null || board.getDest(move).getColor() != getColor()) {
|
|
|
|
moves.add(move);
|
|
|
|
if (board.getDest(move) != null) break;
|
|
|
|
} else break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Diagonal moves to the lower right
|
|
|
|
for (int i = pos.x + 1, j = pos.y + 1; i < 8 && j < 8; i++, j++) {
|
|
|
|
Move move = new Move(pos, new Position(i, j));
|
|
|
|
if (board.getDest(move) == null || board.getDest(move).getColor() != getColor()) {
|
|
|
|
moves.add(move);
|
|
|
|
if (board.getDest(move) != null) break;
|
|
|
|
} else break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Diagonal moves to the lower left
|
|
|
|
for (int i = pos.x - 1, j = pos.y + 1; i >= 0 && j < 8; i--, j++) {
|
|
|
|
Move move = new Move(pos, new Position(i, j));
|
|
|
|
if (board.getDest(move) == null || board.getDest(move).getColor() != getColor()) {
|
|
|
|
moves.add(move);
|
|
|
|
if (board.getDest(move) != null) break;
|
|
|
|
} else break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Diagonal moves to the upper right
|
|
|
|
for (int i = pos.x + 1, j = pos.y - 1; i < 8 && j >= 0; i++, j--) {
|
|
|
|
Move move = new Move(pos, new Position(i, j));
|
|
|
|
if (board.getDest(move) == null || board.getDest(move).getColor() != getColor()) {
|
|
|
|
moves.add(move);
|
|
|
|
if (board.getDest(move) != null) break;
|
|
|
|
} else break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Diagonal moves to the upper left
|
|
|
|
for (int i = pos.x - 1, j = pos.y - 1; i >= 0 && j >= 0; i--, j--) {
|
|
|
|
Move move = new Move(pos, new Position(i, j));
|
|
|
|
if (board.getDest(move) == null || board.getDest(move).getColor() != getColor()) {
|
|
|
|
moves.add(move);
|
|
|
|
if (board.getDest(move) != null) break;
|
|
|
|
} else break;
|
|
|
|
}
|
|
|
|
return moves;
|
|
|
|
}
|
|
|
|
|
2019-07-01 19:50:42 +02:00
|
|
|
@Override
|
|
|
|
public Type getType() { return Type.QUEEN; }
|
|
|
|
}
|