-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyBot.java
152 lines (119 loc) · 4.42 KB
/
myBot.java
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
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
public class myBot implements WordlePlayer{
ArrayList<String> givenWords;
int numGuesses;
String getFirstGuess;
String eduGuessWord;
public myBot(){}
public int tieBreak (){
// Creates and returns a random number between 0 and the size of the passed ArrayList
Random rand = new Random();
int upperLimit = givenWords.size();
int randNum = rand.nextInt(upperLimit);
return randNum;
}
@Override
public void beginGame(Wordle game) {
// Grabs ArrayList and sets guesses to 0
givenWords = game.getKnownWords();
numGuesses = 0;
}
@Override
public boolean hasNextGuess() {
// Checks if there is another String in the List
return (givenWords.size() != 0);
}
@Override
public String nextGuess() {
// Returns String if List is 1
// Random String returned if first guess
// Calls eduGuess for second guess, otherwise returns random guess again
if (givenWords.size() == 1) {
return givenWords.get(0);
}else if (numGuesses == 0) {
numGuesses++;
return initGuess();
}else if(numGuesses == 1){
numGuesses++;
return eduGuess();
}else{
numGuesses++;
return givenWords.get(tieBreak());
}
}
public String initGuess() {
// Returns a random String from ArrayList
String firstGuess = givenWords.get(tieBreak());
getFirstGuess = firstGuess;
return firstGuess;
}
public String eduGuess() {
// Returns a String that does not contain any of the characters from the first guess
for (int i = 0; i < givenWords.size(); i++){
for (int j = 0; j < getFirstGuess.length(); j++ ){
if (givenWords.get(i).contains("" + getFirstGuess.charAt(j))){
continue;
}else{
eduGuessWord = givenWords.get(i);
}
}
}
return eduGuessWord;
}
@Override
public void tell(Hint h) {
// Checks if List against characters found in NotInPuzzle, removes if matches
// Checks if character in IncorrectlyPlaced matches the character in the same place, removes if matches
// Continues if there is a dash in the character at index for CorrectlyPlaced
// Checks if character at index matches CorrectlyPlaced, removes if it doesn't match
String hintCP = h.getCorrectlyPlaced();
String hintIP = h.getIncorrectlyPlaced();
String hintNIP = h.getNotInPuzzle();
if (!h.isWin()) {
givenWords.remove(h);
}
for (int i = 0; i < givenWords.size(); i++){
for (int j = 0; j < hintCP.length(); j++) {
if (hintNIP.contains(""+givenWords.get(i).charAt(j))) {
if (!hintCP.contains("" + givenWords.get(i).charAt(j)) && !hintIP.contains("" + givenWords.get(i).charAt(j))) {
givenWords.remove(givenWords.get(i));
i--;
break;
}
} else if (givenWords.get(i).charAt(j) == hintIP.charAt(j)) {
givenWords.remove(givenWords.get(i));
i--;
break;
}else if ((hintCP.charAt(j) == '-')) {
continue;
} else if (givenWords.get(i).charAt(j) != hintCP.charAt(j) ){
givenWords.remove(givenWords.get(i));
i--;
break;
}
}
}
}
// public static void main(String[] args) throws IOException {
//// String[] words = {"colby", "bacon"};
// myWordle game = new myWordle("norvig200.txt", 5, 100000, 0);
// game.initGame();
//
// myBot bot = new myBot();
// bot.beginGame((Wordle) game);
// Hint h = null;
// int guess = 0;
// int maxGuesses = 100;
// while( bot.hasNextGuess() && guess < maxGuesses ) {
// guess++;
// h = game.guess(bot.nextGuess());
// bot.tell(h);
// if (h.isWin()) {
// break;
// }
// }
// System.out.println("Game over: bot " + (h.isWin()?"won":"lost") + " with " + guess + " guesses");
// }
}