package chess.prashantVaranasi;import chess.spec.*;/** * A knight class which extends piece * @author Prashant Varanasi * */public class Knight extends Piece{ //the amount of moves a knight can make.. private static final int KNIGHT_NUM_MOVES = 8; /** * Basic constructor which just calls the super * @param descriptor String to identify the piece * @param color Color of the piece.. */ public Knight(String descriptor, Player color){ super(descriptor, color); } /** * Checks whether destination can be reached by knight.. */ public boolean isLegalMove(Square destination, MyChessBoard chessBoard){ //get a list of all the moves we're allowed to make Square[] possibleMoves = getKnightMoves(chessBoard.getLocationOf(this), chessBoard); int i = 0; boolean validMove = false; for(i = 0; i < possibleMoves.length; i++){ if(destination.equals(possibleMoves[i])){ validMove = true; } } return validMove; } /** * Function gets the square location when you got in 2 directions * specified by the parameters * @param curSquare The current square to go from * @param direction1 The first direction square to go to * @param direction2 The second direction square to go to * @param chessBoard The board the squares are location on * @return The square the directions lead to, or null if no square */ //gets square at direction1, then direction 2, and returns that square private Square getSquare(Square curSquare, int direction1, int direction2, MyChessBoard chessBoard){ Square endResult = null; Square[] adjacent = chessBoard.getAdjacentSquares(curSquare); endResult = adjacent[direction1]; if(endResult != null){ adjacent = chessBoard.getAdjacentSquares(endResult); endResult = adjacent[direction2]; } return endResult; } /** * Gets a list of all the squares that the knight can move to * Uses a static list of set positions (eg go UP_LEFT, then UP) * @param currentPosition The current sqaure to get knight moves from * @param chessBoard The board that the square is located on * @return Square array with nulls containing all knight moves */ //get a list of squares that the knight can go to private Square[] getKnightMoves(Square currentPosition, MyChessBoard chessBoard){ //there are 8 moves for a knight Square[] moves = new Square[KNIGHT_NUM_MOVES]; //going up moves[0] = this.getSquare(currentPosition, Topology.UP_LEFT, Topology.UP, chessBoard); moves[1] = this.getSquare(currentPosition, Topology.UP_LEFT, Topology.LEFT, chessBoard); moves[2] = this.getSquare(currentPosition, Topology.UP_RIGHT, Topology.UP, chessBoard); moves[3] = this.getSquare(currentPosition, Topology.UP_RIGHT, Topology.RIGHT, chessBoard); //going down moves[4] = this.getSquare(currentPosition, Topology.DOWN_LEFT, Topology.LEFT, chessBoard); moves[5] = this.getSquare(currentPosition, Topology.DOWN_LEFT, Topology.DOWN, chessBoard); moves[6] = this.getSquare(currentPosition, Topology.DOWN_RIGHT, Topology.DOWN, chessBoard); moves[7] = this.getSquare(currentPosition, Topology.DOWN_RIGHT, Topology.RIGHT, chessBoard); return moves; }}