package dev.kske.chess.board; import java.util.ArrayList; import java.util.List; /** * Project: Chess
* File: Knight.java
* Created: 01.07.2019
* Author: Kai S. K. Engelbart */ 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 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 getPseudolegalMoves(Position pos) { List 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; } }