forked from alveerak/hangman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangmanWord.java
59 lines (58 loc) · 1.52 KB
/
HangmanWord.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
/**
* class extends the abstract class Word and provides Hangman game specifications
*/
public class HangmanWord extends Word {
/**
* HangmanWord constructor adds letters of a given string to the letters array
* @param a - String of which's characters are added to letter array
*/
public HangmanWord(String a){
letters = new char [a.length()];
presentLetters = new boolean [a.length()];
for (int i = 0; i<a.length(); i++){
letters[i]=a.charAt(i);
}
}
/**
* method that checks if presentLetters array contains any false values
*/
public boolean isComplete(){
for (int i = 0; i<letters.length; i++){
if (presentLetters[i]==false)
return false;
}
return true;
}
/**
* method that sets value of presentLetters to true if letters contains param a
* @param a - letter character to be checked
*/
public void addLetter (char a){
for (int i = 0; i<letters.length; i++){
if (letters[i]==a){
presentLetters[i]=true;
}
}
}
/**
* method checks if a character at a certain index has been guessed already
* @param index - index of character to be checked
*/
public boolean isLetter(int index){
return presentLetters[index];
}
/**
* method adds the guessed letter to String filling if it is in the correct word
* else it adds a dash
*/
public String makeLabel(){
String filling = "";
for (int i = 0; i<getWord().length(); i++){
if(presentLetters[i]==true)
filling+=letters[i]+" ";
else
filling+="_ ";
}
return filling;
}
}