70 lines
1.7 KiB
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';
|
|
}
|
|
}
|