-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHangmanWord.java
83 lines (81 loc) · 1.98 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* 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(letters.length -1).length(); i++){
if(letters[i] == ' ')
presentLetters[i] = true;
if(presentLetters[i]==true)
filling+=letters[i]+" ";
else
filling+="_ ";
}
return filling;
}
/**
* returns default hint (no hint)
* @return hint
*/
public String getHint() {
return "No hint for this one! Keep trying.";
}
/**
* returns default win message
* @return win
*/
public String getWonMessage() {
return "YOU WON!";
}
/**
* returns default lose message
* @return lose
*/
public String getLostMessage() {
return "YOU LOST!";
}
}