-
Notifications
You must be signed in to change notification settings - Fork 0
/
theTrial.java
78 lines (66 loc) · 1.69 KB
/
theTrial.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
import java.util.*;
public class theTrial {
private ArrayList<String> lettersguessed=new ArrayList<String>();
private String word;
private String guessword = "";
private int guessesleft = 10;
public theTrial(String w){
word =w;
for(int i =0;i<word.length();i++)
guessword+="*";
}
public boolean checkletter(String a){
for(int i =0;i<lettersguessed.size();i++){
if(lettersguessed.get(i).equals(a)){
guessesleft--;
return false;
}
}
for(int i =0;i<word.length();i++){
if(word.substring(i, i+1).equals(a)){
//lettersguessed.add(a); toggles whether letters displayed correctly guessed show up as letter guessed
updateguess(a);
return true;
}
}
lettersguessed.add(a);
guessesleft--;
return false;
}
/*update the guessed word to reflect the newest guessed letter*/
private void updateguess(String a){
ArrayList<Integer> indexofletters=new ArrayList<Integer>();
int i =0;
while(i<word.length()){
int index =word.indexOf(a, i);
if(index!=-1){
indexofletters.add(index);
i=index+1;
}else
break;
}
for(int ind : indexofletters)
guessword=guessword.substring(0,ind)+ a+guessword.substring(ind+1);
}
public String getWord(){
return word;
}
public String getGuessWord(){
return guessword;
}
public String [] getLettersGuessed(){
String [] a = new String[lettersguessed.size()];
for(int i=0;i<a.length;i++)
a[i]=lettersguessed.get(i);
return a;
}
public int getGuessesLeft(){
return guessesleft;
}
public boolean Won(){
if(guessword.equals(word))
return true;
else
return false;
}
}