Created
June 9, 2017 23:09
-
-
Save keinix/9bdb54d4ee80cbddc65a108d77afb61f to your computer and use it in GitHub Desktop.
This file contains 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
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