/** * Knight.js - Knight piece implementation * Handles L-shaped movement pattern */ import { Piece } from './Piece.js'; export class Knight extends Piece { constructor(color, position) { super(color, position); this.type = 'knight'; } /** * Get valid moves for knight * Knight moves in L-shape: 2 squares in one direction, 1 square perpendicular * @param {Board} board - Game board * @returns {Position[]} Array of valid positions */ getValidMoves(board) { const moves = []; // All 8 possible L-shaped moves const moveOffsets = [ [-2, -1], [-2, 1], // Up 2, left/right 1 [-1, -2], [-1, 2], // Up 1, left/right 2 [1, -2], [1, 2], // Down 1, left/right 2 [2, -1], [2, 1] // Down 2, left/right 1 ]; for (const [dRow, dCol] of moveOffsets) { const targetRow = this.position.row + dRow; const targetCol = this.position.col + dCol; if (!this.isInBounds(targetRow, targetCol)) { continue; } const targetPiece = board.getPiece(targetRow, targetCol); // Can move to empty square or capture opponent piece if (!targetPiece || targetPiece.color !== this.color) { moves.push({ row: targetRow, col: targetCol }); } } return moves; } }