-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathYourPlayer.java
More file actions
71 lines (57 loc) · 1.91 KB
/
YourPlayer.java
File metadata and controls
71 lines (57 loc) · 1.91 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.hangman.players;
import com.hangman.Player;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
public class YourPlayer implements Player {
private final List<Character> charsNotGuessedYet = new ArrayList<>();
private final List<Character> charsAlreadyGuessed = new ArrayList<>();
private final List<String> wordsList = new ArrayList<>();
public YourPlayer(String[] wordsList) {
// initialize non-guessed characters
for (char c = 'a'; c <= 'z'; ++c) {
this.charsNotGuessedYet.add(c);
}
// copy the word list
this.wordsList.addAll(Arrays.asList(wordsList));
}
@Override
public char GetGuess(List<Character> currentClue) {
Character guess = null;
StringBuilder pattern = new StringBuilder(currentClue.size());
int nextYetUnknownCharacterIndex = 0;
int i = 0;
for (Character c : currentClue) {
if (c.equals('_')) {
nextYetUnknownCharacterIndex = i;
pattern.append(".");
} else {
// known character, act like if it was already guessed
this.charsNotGuessedYet.remove(c);
this.charsAlreadyGuessed.add(c);
pattern.append(c);
}
++i;
}
Pattern wordPattern = Pattern.compile(pattern.toString());
for (String word : this.wordsList) {
if ((word.length() == currentClue.size()) && wordPattern.matcher(word).matches()) {
guess = word.charAt(nextYetUnknownCharacterIndex);
if (!this.charsAlreadyGuessed.contains(guess)) {
break;
}
}
}
if (guess == null) {
// no word matching, just get the next not-yet-guessed character
guess = this.charsNotGuessedYet.isEmpty() ?
'a' :
this.charsNotGuessedYet.get(0);
}
// store char as already guessed
this.charsNotGuessedYet.remove(guess);
this.charsAlreadyGuessed.add(guess);
return guess;
}
}