-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangmanLogic.java
72 lines (56 loc) · 2.07 KB
/
HangmanLogic.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
public class HangmanLogic {
private String word;
private String guessedLetters;
private int numberOfFaults;
public HangmanLogic(String word) {
this.word = word.toUpperCase();
this.guessedLetters = "";
this.numberOfFaults = 0;
}
public int numberOfFaults() {
return this.numberOfFaults;
}
public String guessedLetters() {
return this.guessedLetters;
}
public int losingFaultAmount() {
return 12;
}
public void guessLetter(String letter) {
if (!this.guessedLetters.contains(letter)) {
if (!this.word.contains(letter)) {
this.numberOfFaults++;
}
this.guessedLetters += letter;
}
// program here the functionality for making a guess
// if the letter has already been guessed, nothing happens
// it the word does not contains the guessed letter, number of faults increase
// the letter is added among the already guessed letters
}
public String hiddenWord() {
// program here the functionality for building the hidden word
// create the hidden word by interating through this.word letter by letter
// if the letter in turn is within the guessed words, put it in to the hidden word
// if the letter is not among guessed, replace it with _ in the hidden word
// return the hidden word at the end
int i = 0;
String hiddenWordString = "";
while (i < this.word.length()) {
char c = this.word.charAt(i);
String wordLetterString = "" + c;
if(this.guessedLetters.contains(wordLetterString)) {
hiddenWordString += c;
} else {
hiddenWordString += "_";
}
i++;
// if (this.word.indexOf(c) >= 0) {
// hiddenWordString += "" + c; //this.guessedLetters.charAt(i);
// } else {
// hiddenWordString += "_";
// }
}
return hiddenWordString;
}
}