Skip to content

Instantly share code, notes, and snippets.

@Binary-Finery
Created March 12, 2019 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Binary-Finery/a91b668f428b405d27200c7688869d4c to your computer and use it in GitHub Desktop.
Save Binary-Finery/a91b668f428b405d27200c7688869d4c to your computer and use it in GitHub Desktop.
hangman utility class
public class Utils {
private static StringBuilder builder = new StringBuilder();
public static String updateString(String word, String targetWord, char guess) {
reset();
builder.append(word);
for (int i = 0; i < word.length(); i++) {
if (targetWord.charAt(i) == guess){
builder.setCharAt(i, guess);
}
}
return builder.toString();
}
public static String buildInitialWordState(String word) {
reset();
for (int i = 0; i < word.length(); i++){
builder.append("-");
}
return builder.toString();
}
public static boolean letterIsPresent(String word, char c) {
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == c){
return true;
}
}
return false;
}
private static void reset(){
builder.setLength(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment