getValidMoves method
Returns a list of valid moves for the selected piece, to render in the ChessBoardWidget.
Implementation
List<Position> getValidMoves(Position from) {
ChessPiece? piece = getPiece(from);
if (piece == null || piece.color != turn) return [];
List<Position> validMoves = [];
// Existing valid move logic: iterate over board squares and check if a move is valid.
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
Position target = Position(row: row, col: col);
if (MoveValidator.isValidMove(this, from, target)) {
// (Optional: simulate the move to ensure the king does not end up in check.)
validMoves.add(target);
}
}
}
// If the selected piece is the king, add castling moves.
if (piece.type == PieceType.king) {
// King‑side castling: king should move to column 6.
if (MoveValidator.canCastleKingSide(this, piece.color)) {
validMoves.add(Position(row: from.row, col: 6));
}
// Queen‑side castling: king should move to column 2.
if (MoveValidator.canCastleQueenSide(this, piece.color)) {
validMoves.add(Position(row: from.row, col: 2));
}
}
return validMoves;
}