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/Knight.java

70 lines
1.7 KiB
Java

package dev.kske.chess.board;
import java.util.ArrayList;
import java.util.List;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>Knight.java</strong><br>
* Created: <strong>01.07.2019</strong><br>
*
* @since Chess v0.1-alpha
* @author Kai S. K. Engelbart
*/
public class Knight extends Piece {
/**
* Creates knight {@link Piece}.
*
* @param color the color of this knight
* @param board the board on which this knight will be placed
*/
public Knight(Color color, Board board) {
super(color, board);
}
@Override
public boolean isValidMove(Move move) {
return Math.abs(move.getxDist() - move.getyDist()) == 1
&& (move.getxDist() == 1 && move.getyDist() == 2
|| move.getxDist() == 2 && move.getyDist() == 1)
&& checkDestination(move);
}
private void checkAndInsertMove(
List<Move> moves, Position pos, int offsetX, int offsetY
) {
if (
pos.x + offsetX >= 0 && pos.x + offsetX < 8 && pos.y + offsetY >= 0
&& pos.y + offsetY < 8
) {
Move move
= new Move(pos, new Position(pos.x + offsetX, pos.y + offsetY));
if (checkDestination(move))
moves.add(move);
}
}
@Override
protected List<Move> getPseudolegalMoves(Position pos) {
List<Move> moves = new ArrayList<>();
checkAndInsertMove(moves, pos, -2, 1);
checkAndInsertMove(moves, pos, -1, 2);
checkAndInsertMove(moves, pos, 1, 2);
checkAndInsertMove(moves, pos, 2, 1);
checkAndInsertMove(moves, pos, -2, -1);
checkAndInsertMove(moves, pos, -1, -2);
checkAndInsertMove(moves, pos, 1, -2);
checkAndInsertMove(moves, pos, 2, -1);
return moves;
}
@Override
public int getValue() { return 35; }
@Override
public char firstChar() {
return 'n';
}
}