-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrompter.java
122 lines (102 loc) · 3.92 KB
/
Prompter.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.util.Scanner;
/**
* Created by Mallikarjuna on 9/28/2016.
*/
public class Prompter {
private Scanner mScanner;
private Game mGame;
public Prompter(Game game) {
mScanner = new Scanner(System.in);
mGame = game;
}
private void adminDisplay( ) {
System.out.printf("ADMINISTRATOR CONFIGURATION\n");
System.out.printf("=================================\n\n");
}
private void playerDisplay(String itemName, int maxItemsAllowed) {
System.out.printf("\nPLAYER\n");
System.out.printf("=====================\n\n");
System.out.printf("Game is On :\n");
System.out.printf("You have to guess how many %s in Jar.\n",itemName);
System.out.printf("You guess is inbetween 1 to %d\n",maxItemsAllowed);
}
// Input : Prompt user Item in Jar
private String promptForItemName()
{
String itemName = "";
boolean isValidItemName = false;
while(!isValidItemName) {
System.out.print("Enter the item in Jar:");
itemName = mScanner.nextLine();
if(itemName.matches("[A-Za-z]*")) {
System.out.printf("%s is a valid Item in Jar\n", itemName);
isValidItemName = true;
} else {
System.out.printf("%s is not a valid Item in Jar\n",itemName);
}
}
return itemName;
}
// Input : Prompts user no of Items in Jar
private int promptForMaximumItems(String itemName) {
int maxItemsAllowed;
do {
System.out.printf("The Number of %s below 1 is not allowed in Jar\n",itemName);
System.out.printf("Enter Maximum %s Allowed in Jar:\n",itemName);
while (!mScanner.hasNextInt()) {
String input = mScanner.next();
System.out.printf("%s is not a valid number.\n", input);
System.out.print("Enter Maximum Items Allowed in Jar:");
}
maxItemsAllowed = mScanner.nextInt();
} while (maxItemsAllowed < 1);
return maxItemsAllowed;
}
/*
Input : Receives Jar Input from User
@ itemName : Holds the Item in Jar
@maxItemsAllowed : Holds the No of @itemName in Jar
Pass @ itemName , @maxItemsAllowed to mGame to Instantiate Jar
*/
public void play() {
String itemName;
int maxItemsAllowed;
boolean isSolved = false;
adminDisplay();
itemName = promptForItemName();
maxItemsAllowed = promptForMaximumItems(itemName);
mGame.IntializeJar(itemName,maxItemsAllowed);
playerDisplay(itemName,maxItemsAllowed);
// The Game Runs until User Make Precise Guess
while(!isSolved) {
isSolved = promptForGuess();
System.out.printf("Your Guess is %s\n", mGame.currentProgress());
}
// On Precise Guess Display Success Message
System.out.printf("Congratulations you have guessed that %d %s with %d Tries\n",
mGame.getGuess(),itemName,mGame.countTries());
}
/*
Input : Prompts User to Make Guess
@ guessNumber : Holds the User Guess
Pass @ guessNumber to Match No of Items in Jar
*/
public boolean promptForGuess() {
int guessNumber;
boolean isHit = false;
System.out.print("Guess:");
try {
while (!mScanner.hasNextInt()) {
String input = mScanner.next();
System.out.printf("%s is not a valid number.\n", input);
System.out.print("Guess:");
}
guessNumber = mScanner.nextInt();
isHit = mGame.applyGuess(guessNumber);
}
catch(IllegalArgumentException iae) {
System.out.printf("%s ,Please try Again\n ",iae.getMessage());
}
return isHit;
}
}