Skip to content

Instantly share code, notes, and snippets.

@keinix
Created June 9, 2017 23:09
Show Gist options
  • Save keinix/9bdb54d4ee80cbddc65a108d77afb61f to your computer and use it in GitHub Desktop.
Save keinix/9bdb54d4ee80cbddc65a108d77afb61f to your computer and use it in GitHub Desktop.
class Game {
private String answer;
private String hits;
private String misses;
public static final int MAX_MISSES = 7;
public Game(String answer) {
this.answer = answer;
hits = "";
misses = "";
}
public boolean applyGuess(char letter) {
// 2 things: output hit or miss, save hit and misses to string
if (misses.indexOf(letter) != -1 || hits.indexOf(letter) != -1) {
throw new IllegalArgumentException(letter + )
}
boolean isHit = answer.indexOf(letter) != -1;
if (isHit) {
hits += letter;
} else {
misses += letter;
}
return isHit;
}
public int getRemainingTries() {
return MAX_MISSES - misses.length();
}
public String getCurrentProgress() {
// uses char array to see if our current guess is in array if
// not output _
String progress = "";
for (char letter : answer.toCharArray()) {
char display = '_';
if (hits.indexOf(letter) != -1) {
display = letter;
}
progress += display;
}
return progress;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment