-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
112 lines (100 loc) · 3.06 KB
/
Game.java
File metadata and controls
112 lines (100 loc) · 3.06 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
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
public class Game
{
public static final int MAX_TRIES = 7; // Meximum Tries Available to Guess
private String mAnswer;
private String mHits;
private String mMisses;
public Game(String answer) {
mAnswer = answer;
mHits = "";
mMisses = "";
}
public String getAnswer( )
{
return mAnswer;
}
/*
Validate Method : Input
Validate Input is a letter , digit or Special Character
Process Input : Convert Input to Lower Case
Validate Process Input is already is guessed from user
pass the Process Input to Compute Method: applyGuess(char letter)
*/
private char validateGuess(char letter) {
//Validate the user Input Character
if(!Character.isLetter(letter)) {
throw new IllegalArgumentException(letter+ "is not a letter");
}
letter =Character.toLowerCase(letter);
if ( mHits.indexOf(letter) >= 0 || mMisses.indexOf(letter) >= 0) {
throw new IllegalArgumentException(letter + "is guessed letter");
}
return letter;
}
/*
Error Checking Method : Input
Checks for the Empty String from User Input
Process Input : Obtain Character from String
Pass the Process Input to Compute Method
Compute Method : applyGuess(char letter)
*/
public void applyGuess(String letters) {
//Checks for Empty String
if(letters.length( ) == 0) {
throw new IllegalArgumentException("It is an Empty Guess");
}
applyGuess(letters.charAt(0));
}
/*
Compute Method: Intermediate Output
Input : letter
processing :
Checks the letter is existing in mAnswer
Adds letter mHits or mMisses
Intermediate Output : mHits or mMisses
*/
public void applyGuess(char letter) {
letter = validateGuess(letter);
if ( mAnswer.indexOf(letter) >= 0) {
mHits +=letter;
} else {
mMisses += letter;
}
}
/*
Compute Method : Intermediate Output
@ progress : Holds current status of User Guess
*/
public String currentProgress( ) {
String progress = "";
for (char letter : mAnswer.toCharArray()) {
char display = '-';
if(mHits.indexOf(letter) >= 0) {
display = letter;
}
progress += display;
}
return progress;
}
/*
Helper Methods :
Checks for the Progress of the Application
to further proceed or to Abort the Application
@remainingTries : Holds user remaining tries available to Guess
Compute Method : Final Output
@remainingTries : Holds user Guessed Letter with no of tries remaining
*/
public int remainingTries( ) {
int remainingTries = (MAX_TRIES - mMisses.length());
return remainingTries;
}
/*
Helper Method
Checks for the Progress of the Application
to further proceed or to Abort the Application
*/
public boolean isSolved( )
{
return currentProgress().indexOf('-') == -1;
}
}