-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathYourPlayer.java
More file actions
74 lines (62 loc) · 2.07 KB
/
YourPlayer.java
File metadata and controls
74 lines (62 loc) · 2.07 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
package com.hangman.players;
import com.hangman.Player;
import java.util.ArrayList;
import java.util.List;
public class YourPlayer implements Player {
private static final char[] LETTERS = {
'e', 't', 'a', 'o', 'i', 'n',
's', 'h', 'r', 'd', 'l', 'c',
'u', 'm', 'y', 'f', 'g', 'y',
'p', 'b', 'v', 'k', 'j', 'x',
'q', 'z'
};
// private static List<Character> COMMON_REPEATS =
// Arrays.asList('e', 't', 'o', 'n', 's', 'r', 'd', 'l', 'c', 'm', 'f', 'g', 'p');
List<Character> exhaustedGuesses = new ArrayList<>();
Integer remainingBlanks;
Character lastGuess;
@Override
public char GetGuess(List<Character> currentClue) {
if (isFirstGuess()) {
remainingBlanks = getRemainingBlanks(currentClue);
return saveAndGuess(LETTERS[0]);
}
return guessNextLetter(currentClue);
}
private char guessNextLetter(List<Character> currentClue) {
if (lastGuessWasCorrect(currentClue)) {
// TODO: don't repeat guess letters that don't repeat in English
// if (COMMON_REPEATS.contains(lastGuess)) {
return saveAndGuess(lastGuess);
// }
}
exhaustedGuesses.add(lastGuess);
return saveAndGuess(LETTERS[exhaustedGuesses.size()]);
}
private boolean lastGuessWasCorrect(List<Character> clue) {
Integer blanks = getRemainingBlanks(clue);
if (blanks < remainingBlanks) {
remainingBlanks = blanks;
return true;
}
return false;
}
private boolean isFirstGuess() {
return exhaustedGuesses.size() == 0 &&
lastGuess == null &&
remainingBlanks == null;
}
private Integer getRemainingBlanks(List<Character> currentClue) {
int blanks = 0;
for (Character c : currentClue) {
if (c.equals('_')) {
++blanks;
}
}
return blanks;
}
private char saveAndGuess(char c) {
lastGuess = c;
return c;
}
}