package dev.kske.chess.board; import java.util.ArrayList; import java.util.List; /** * Project: Chess
* File: Knight.java
* Created: 01.07.2019
* * @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 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 int getValue() { return 35; } @Override public char firstChar() { return 'n'; } }