-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
70 lines (59 loc) · 1.85 KB
/
Game.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
/**
* Created by Mallikarjuna on 9/28/2016.
*/
public class Game {
private Jar mJar;
private int mGuess;
private static int mTotalTries = 0;
public int getGuess( ) {
return mGuess;
}
// Instantiate Jar Object
public void IntializeJar(String itemName, int maxItemsAllowed) {
mJar = new Jar(itemName,maxItemsAllowed);
// Fill Jar with Random number of Items of maxItemsAllowed
mJar.fill();
}
/*
Compute Method : Intermediate Output
@ progress : Holds guess is High , Low , Precise
*/
public String currentProgress( ) {
String progress = " ";
if ( mJar.getRandomNumber() != mGuess ) {
if( mJar.getRandomNumber() < mGuess ) {
progress = "High";
return progress;
} else {
progress = "Low";
return progress;
}
}
progress = "Precise";
return progress;
}
// Validate Input : Input can't be more than maxItemsAllowed or less than 1
private int preventGuess(int guessNumber) {
if ( guessNumber > mJar.getMaxItemsAllowed() || guessNumber < 1 ) {
throw new IllegalArgumentException("Your guess must between 1 and "+mJar.getMaxItemsAllowed());
}
return guessNumber;
}
/*
Compute method : Checks Random No of Items in Jar is equal to guess by user
@mGuess : Holds guess made by user
mTotalTries : Holds no of Tries made by user
*/
public boolean applyGuess(int guessNumber) {
mGuess = preventGuess(guessNumber);
mTotalTries++;
if ( mJar.getRandomNumber() == mGuess ) {
return true;
}
return false;
}
public int countTries()
{
return mTotalTries;
}
}