public class Game {
private String mWord;
private String mWrong;
private String mRight;
public Game(String word) {
mWord = word;
mWrong = "";
mRight = "";
}
public void applyGuess(char guess) {
if (mWord.indexOf(guess) >= 0) {
mRight += guess;
} else {
mWrong += guess;
}
}
public String getCurrentProgress() {
String progress = "";
for (int i = 0 ; i<mWord.length() ; i++){
if (mRight.indexOf(mWord.charAt(i)) >= 0) {
progress += mWord.charAt(i);
} else {
progress += '_';
}
}
return progress;
}
}
import java.io.Console;
public class Prompter {
private Game mGame;
public Prompter(Game game){
mGame = game;
}
public char makeGuess() {
Console console = System.console();
String letterAsString = console.readLine("Make a guess!");
return letterAsString.charAt(0);
}
public void displayProgress() {
System.out.println(mGame.getCurrentProgress());
}
}
public class Hangman {
public static void main(String[] args) {
Game game = new Game("monkey");
Prompter prompter = new Prompter(game);
prompter.displayProgress();
while (game.getCurrentProgress().contains("_")) {
game.applyGuess(prompter.makeGuess());
prompter.displayProgress();
}
System.out.println("Congratulations, you won");
}
}