-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathYourPlayer.java
More file actions
165 lines (133 loc) · 4.83 KB
/
YourPlayer.java
File metadata and controls
165 lines (133 loc) · 4.83 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.hangman.players;
import com.hangman.Player;
import com.hangman.WordList;
import java.util.*;
public class YourPlayer implements Player {
private boolean hasOneRightGuess;
List<Character> attemptedCharacters;
char[] frequentUsedChars = "etaoinshrdlcumwfgypbvkjxqz".toCharArray();
int index;
private char lastCharGuessed;
private String lastClue;
private boolean lastAttemptResult;
public static final boolean RIGHT_GUESS = true;
public static final boolean WRONG_GUESS = false;
private String[] words;
public YourPlayer () {
attemptedCharacters = new ArrayList<Character>();
index = 0;
hasOneRightGuess = false;
words = WordList.words.clone();
for (int i=0; i < words.length; i++) {
words[i] = words[i].toLowerCase();
}
}
public static List<String> getWordMatchingLength(String[] words, int length) {
List<String> matchingStrings = new ArrayList<String>();
for(String word : words) {
if ( word.length() == length ) {
matchingStrings.add(word);
}
}
return matchingStrings;
}
@Override
public char GetGuess(List<Character> clue) {
StringBuilder sb = new StringBuilder(clue.size());
for (Character c : clue)
sb.append(c);
String clueAsString = sb.toString();
if (clueAsString.equals(lastClue) || lastClue == null) {
lastAttemptResult = WRONG_GUESS;
}
else {
lastAttemptResult = RIGHT_GUESS;
hasOneRightGuess = true;
}
List<String> wordList = YourPlayer.getWordMatchingLength(words,clue.size());
words = new String[wordList.size()];
words = wordList.toArray(words);
wordList = YourPlayer.getWordMatchingPattern(words,clue);
words = new String[wordList.size()];
words = wordList.toArray(words);
if(getLastAttemptResult() == WRONG_GUESS) {
wordList = YourPlayer.getWordsWithoutChars(words, getLastCharGuessed());
words = new String[wordList.size()];
words = wordList.toArray(words);
}
if(words.length > 0 ) {
lastCharGuessed = getMostFrequentChar(words, attemptedCharacters);
}
else{
while(attemptedCharacters.contains(frequentUsedChars[index])){
index++;
}
lastCharGuessed = frequentUsedChars[index++];
}
attemptedCharacters.add(lastCharGuessed);
lastClue = clueAsString;
return lastCharGuessed;
}
public static List<String> getWordMatchingPattern(String[] words, List<Character> pattern) {
List<String> matchingStrings = new ArrayList<String>(Arrays.asList(words));
for(int index=0; index < pattern.size(); index++) {
if(pattern.get(index) == '_') {
continue;
}
for(String word : words) {
if(word.charAt(index) != pattern.get(index)) {
matchingStrings.remove(word);
continue;
}
}
}
return matchingStrings;
}
public static List<String> getWordsWithoutChars(String[] words, char chars) {
List<String> matchingStrings = new ArrayList<String>(Arrays.asList(words));
for(String word: words) {
if ( word.indexOf(chars) != -1) {
matchingStrings.remove(word);
continue;
}
}
return matchingStrings;
}
public char getLastCharGuessed() {
return lastCharGuessed;
}
public boolean getLastAttemptResult() {
return lastAttemptResult;
}
public boolean hasOneRightGuess() {
return hasOneRightGuess;
}
public static char getMostFrequentChar(String[] words, List<Character> attemptedCharacters) {
Map<Character,Integer> counter = new HashMap<Character, Integer>();
for(String word : words) {
for(char character: word.toCharArray()) {
try {
counter.get(character);
if ( counter.get(character) == null ) {
counter.put(character,0);
}
}
catch (Exception e){
counter.put(character, 0);
}
counter.put(character, counter.get(character) + 1);
}
}
char highest = 'a';
int highest_count = 0;
for(Character character: counter.keySet()) {
if(counter.get(character) > highest_count ) {
if(attemptedCharacters.indexOf(character) == -1 ) {
highest = character;
highest_count = counter.get(character);
}
}
}
return highest;
}
}