-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHiddenWord.java
More file actions
42 lines (33 loc) · 1.23 KB
/
Copy pathHiddenWord.java
File metadata and controls
42 lines (33 loc) · 1.23 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
public class HiddenWord
{
private String hiddenWord;
public HiddenWord(String word){
hiddenWord = word;
}
public String getHint(String guess){
String hint = "";
for (int i = 0; i < guess.length(); i++){
String gChar = guess.substring(i, i + 1);
String hChar = hiddenWord.substring(i, i + 1);
if (gChar.equals(hChar))
hint += gChar;
else if (hiddenWord.indexOf(gChar) >= 0)
hint += "+";
else
hint += "*";
}
return hint;
}
public static void main(String[] args)
{
HiddenWord puzzle = new HiddenWord("HARPS");
System.out.println(puzzle.getHint("AAAAA")); //should display +A+++
System.out.println(puzzle.getHint("HELLO")); //should display H****
System.out.println(puzzle.getHint("HEART")); //should display H*++*
System.out.println(puzzle.getHint("HARMS")); //should display HAR*S
System.out.println(puzzle.getHint("HARPS")); //should display HARPS
HiddenWord puzzle2 = new HiddenWord("JACKS");
System.out.println(puzzle2.getHint("FARSE")); //should display *A*+*
System.out.println(puzzle2.getHint("SACKS")); //should display +ACKS
}
}