Refactored function to get valid moves of a given piece to remove duplicate code