Skip to content

Instantly share code, notes, and snippets.

@tarynsauer
Created January 19, 2014 22:45
Show Gist options
  • Save tarynsauer/8512016 to your computer and use it in GitHub Desktop.
Save tarynsauer/8512016 to your computer and use it in GitHub Desktop.
public boolean validMove(String move) {
if (tryParse(move)) { // Checks if string can be parsed
int cellIndex = (new Integer(move) - 1);
int totalCellsCount = rows * rows;
return (cellIndex >= 0) && (cellIndex < totalCellsCount) && isOpen(cellIndex); // Checks if parsed value is valid based on game board
} else {
return false;
}
}
private static boolean tryParse(String input) {
if (input.length() > 0) { // Checks for empty string
try {
new Integer(input); // Checks if valid number
return true;
} catch (NumberFormatException e) {
return false;
}
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment