-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathYourPlayer.java
More file actions
86 lines (67 loc) · 1.91 KB
/
YourPlayer.java
File metadata and controls
86 lines (67 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.hangman.players;
import com.hangman.Player;
import java.util.*;
public class YourPlayer implements Player {
private static final char NO_GUESS = 0x00;
private final Random rnd = new Random();
private List<Character> IMMUTABLE_CHARS = Arrays.asList('e','a','i','o','u','r','s','t','l','n','h','d');
private List<Character> commonChars = new ArrayList<>(IMMUTABLE_CHARS);
private Stack<Character> guessStack = new Stack<>();
private List<Character> currentClue;
private int guessCount = 0;
@Override
public char GetGuess(List<Character> currentClue) {
this.guessCount++;
this.currentClue = currentClue;
List<Integer> emptySpotList = getEmptySpotPositions();
if (emptySpotList.isEmpty()) {
return NO_GUESS;
}
Character guess = NO_GUESS;
if (!commonChars.isEmpty()) {
for (char c : commonChars) {
if (!currentClue.contains(c)) {
guess = c;
break;
}
}
}
if (guess > NO_GUESS) {
commonChars.remove(guess);
guessStack.push(guess);
return guess;
}
guess = getCharacter();
return guess;
}
public List<Integer> getEmptySpotPositions() {
List<Integer> resList = new ArrayList<>();
for (int i = 0; i < this.currentClue.size(); i++) {
if (this.currentClue.get(i) == '_') {
resList.add(i);
}
}
return resList;
}
int getTotalGuessCount() {
return this.guessCount;
}
Character getLastGuess() {
if (guessStack.isEmpty()) {
return NO_GUESS;
}
return guessStack.peek();
}
Character getCharacter() {
char rndChar = NO_GUESS;
while (rndChar == NO_GUESS || IMMUTABLE_CHARS.contains(rndChar) || guessStack.contains(rndChar)) {
rndChar = getRandomCharacter();
}
guessStack.push(rndChar);
return rndChar;
}
private Character getRandomCharacter() {
int i = rnd.nextInt(26) + 97;
return (char)i;
}
}