Skip to content

Instantly share code, notes, and snippets.

@ezragol
Last active January 18, 2023 16:06
Show Gist options
  • Save ezragol/b122d58f3b17074df0118e74ef67c66d to your computer and use it in GitHub Desktop.
Save ezragol/b122d58f3b17074df0118e74ef67c66d to your computer and use it in GitHub Desktop.
guessword codecheck v1
/*
* GuessWord.java
*
* @author EZRA GOLDNER <24goldnere@brooklinek12.org>
*/
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