-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathYourPlayer.java
More file actions
56 lines (41 loc) · 1.16 KB
/
YourPlayer.java
File metadata and controls
56 lines (41 loc) · 1.16 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.hangman.players;
import com.hangman.Player;
import java.util.*;
public class YourPlayer implements Player {
private final Random random = new Random();
private final Set<Character> usedLetters = new HashSet<>();
private final List<Character> letters ;
public YourPlayer() {
List<Character> tempLetters = new ArrayList<>();
for (char i = 'a'; i <= 'z'; i++) {
tempLetters.add((char) i);
}
letters = Collections.unmodifiableList(tempLetters);
}
void addLetter(Character c) {
usedLetters.add(c);
}
@Override
public char GetGuess(List<Character> currentClue) {
if (currentClue == null || currentClue.isEmpty()) {
return ' ';
}
if (currentClue.stream().noneMatch((c) -> c == '_')) {
return ' ';
}
if (usedLetters.size() == 26) {
usedLetters.clear();
}
Character newCharacter;
do {
newCharacter = getRandomLetter();
}
while (usedLetters.contains(newCharacter));
usedLetters.add(newCharacter);
return newCharacter;
}
private char getRandomLetter() {
int randomIndex = random.nextInt(letters.size());
return letters.get(randomIndex);
}
}