-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathYourPlayer.java
More file actions
57 lines (40 loc) · 1.27 KB
/
YourPlayer.java
File metadata and controls
57 lines (40 loc) · 1.27 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
package com.hangman.players;
import com.hangman.Player;
import com.hangman.WordList;
import java.util.*;
public class YourPlayer implements Player {
private Map<Character, Integer> characters = new HashMap<Character, Integer>();
private Set<Character> guessedCharacters = new HashSet<Character>();
{
buildCharactersMap();
}
@Override
public char GetGuess(List<Character> currentClue) {
guessedCharacters.addAll(currentClue);
Character c = getMostFrequentCharacter();
guessedCharacters.add(c);
System.out.println(c);
return c;
}
private void buildCharactersMap() {
for (String s: WordList.words)
for (Character c: s.toCharArray()) {
if (characters.get(c) == null)
characters.put(c, 1);
else
characters.put(c, characters.get(c) + 1);
}
}
public char getMostFrequentCharacter() {
Map.Entry mostFrequentCharacter = null;
for (Map.Entry e: characters.entrySet()) {
if (mostFrequentCharacter == null)
mostFrequentCharacter = e;
if (guessedCharacters.contains(e.getKey()))
continue;
if ((Integer)mostFrequentCharacter.getValue() < (Integer)e.getValue())
mostFrequentCharacter = e;
}
return (Character)mostFrequentCharacter.getKey();
}
}