Last active
January 18, 2023 16:06
-
-
Save ezragol/b122d58f3b17074df0118e74ef67c66d to your computer and use it in GitHub Desktop.
guessword codecheck v1
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
/* | |
* GuessWord.java | |
* | |
* @author EZRA GOLDNER <[email protected]> | |
*/ | |
import java.lang.reflect.*; | |
public class GuessWord { | |
// There may be instance variables, constructors, and methods that are not shown. | |
private String word; | |
public GuessWord(String word) { this.word = word; } | |
public int length() { return this.word.length(); } | |
/** | |
* Return a hint for hidden word based on guess, such that: | |
* If the letter in the guess is... | corresponding character in the hint is... | |
* also in same position in hidden word | matching letter | |
* also in hidden word, but different position | "+" | |
* not in hidden word | "*" | |
* Precondition: guess.length() is the same as the length of the hidden word. | |
* | |
* @param guess guess for hidden word | |
* @return hint for hidden word | |
*/ | |
public String getHint(String guess) { | |
String hint = ""; | |
for (int i = 0; i < length(); i++) { | |
char letter = guess.charAt(i); | |
if (letter == word.charAt(i)) { | |
hint += letter; | |
} | |
else if (word.contains(String.valueOf(letter))) { | |
hint += "+"; | |
} | |
else { | |
hint += "*"; | |
} | |
} | |
return hint; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment