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/dev/kske/chess/board/Knight.java

48 lines
1.4 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>
* Author: <strong>Kai S. K. Engelbart</strong>
*/
public class Knight extends Piece {
public Knight(Color color, Board board) {
super(color, board);
}
@Override
public boolean isValidMove(Move move) {
return Math.abs(move.xDist - move.yDist) == 1
&& (move.xDist == 1 && move.yDist == 2 || move.xDist == 2 && move.yDist == 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 Type getType() { return Type.KNIGHT; }
}