This repository has been archived on 2021-02-18. You can view files and clone it, but cannot push or open issues or pull requests.
chess/src/main/java/dev/kske/chess/board/Queen.java

146 lines
3.6 KiB
Java

package dev.kske.chess.board;
import java.util.ArrayList;
import java.util.List;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>Queen.java</strong><br>
* Created: <strong>01.07.2019</strong><br>
*
* @since Chess v0.1-alpha
* @author Kai S. K. Engelbart
*/
public class Queen extends Piece {
/**
* Creates queen {@link Piece}.
*
* @param color the color of this queen
* @param board the board on which this queen will be placed
*/
public Queen(Color color, Board board) {
super(color, board);
}
@Override
public boolean isValidMove(Move move) {
return (move.isHorizontal() || move.isVertical() || move.isDiagonal())
&& isFreePath(move);
}
@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;
}
@Override
public int getValue() { return 90; }
}