-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrompter.java
More file actions
56 lines (51 loc) · 1.77 KB
/
Prompter.java
File metadata and controls
56 lines (51 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Prompter
{
private Game mGame;
private BufferedReader mreader;
public Prompter(Game game) {
mGame = game;
mreader = new BufferedReader(new InputStreamReader(System.in));
}
/*
Application Runnable Method
Game Runs till user has tries left and Game is not solved
*/
public void play ( ) {
while( mGame.remainingTries() > 0 && !mGame.isSolved()) {
displayProgress();
guessForPrompter();
}
if(mGame.isSolved()) {
System.out.printf("Congratulation you have completed %s word with %d tries remaining\n",
mGame.getAnswer(), mGame.remainingTries());
} else {
System.out.printf("%s is the word, Please Try Again, All the Best\n",mGame.getAnswer());
}
}
/*
Input : Receives Input from User
Pass the Input to apply Guess Method
*/
public void guessForPrompter( ) {
boolean isValid = false;
while(!isValid) {
System.out.println("Enter the Guess: ");
try {
String guessAsString = mreader.readLine();
mGame.applyGuess(guessAsString);
isValid = true;
} catch(IllegalArgumentException iae) {
System.out.printf("%s ,Please try Again\n ",iae.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
}
}
//Output : Display the Remaining Tries left to User and Current Progress of Game
public void displayProgress() {
System.out.printf("Try to Solve: %d Tries are remaining %s\n", mGame.remainingTries(),mGame.currentProgress());
}
}