Created
March 12, 2019 11:00
-
-
Save Binary-Finery/a91b668f428b405d27200c7688869d4c to your computer and use it in GitHub Desktop.
hangman utility class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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