-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordle1.java
252 lines (215 loc) · 7.21 KB
/
Wordle1.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package org.gameProject.wordle1;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class Wordle1 {
private int numGuesses;
private String wordleWord;
private boolean gameOver;
private boolean wonGame;
/**
* Constructor sets up game state.
*/
public Wordle1() {
reset();
}
public int getNumGuesses() {
return numGuesses;
}
public String getWordleWord() {
return wordleWord;
}
public boolean getWonGame() {
return wonGame;
}
public boolean getGameOver() {
return gameOver;
}
public void setWordleWord(String s) {
wordleWord = s;
}
public void setNumGuesses(int n) {
numGuesses = n;
}
public void setWonGame(boolean b) {
wonGame = b;
}
public void setGameOver(boolean b) {
gameOver = b;
}
Path p = Paths.get("src/main/java/org/gameProject/wordle1/WordleWords.txt"); // txt file with word
// bank
// creates the ArrayList of valid words
// Collections
public List<String> getArrayOfWords() {
List<String> words = new ArrayList<>();
try {
words = Files.readAllLines(p);
} catch (IOException e) {
System.out.println("Invalid File Name");
}
return words;
}
private List<String> words = getArrayOfWords();
// determines if the guess (s) is in the word bank.
private boolean isValidWord(String s) {
if (words.contains(s.trim().toLowerCase())) {
return true;
}
return false;
}
// returns a random word from the word bank
private String getChosenWord() {
List<String> words = new ArrayList<>();
try {
words = Files.readAllLines(p);
} catch (IOException e) {
System.out.println("Invalid File Name");
}
Random r = new Random();
int pos = r.nextInt(words.size());
String chosen = words.get(pos).trim().toUpperCase();
return chosen;
}
// checks whether the guess (s) is valid
// valid if the length is 5 (excluding surrounding white spaces) and if it
// exists in the word bank
public boolean isValidInput(String s) {
if ((s.length() == 5) && (isValidWord(s))) {
return true;
}
return false;
}
// green: 1, yellow: 0, gray: -1
public List<Integer> getColors(String userWord, String wordleWord) {
String userInput = userWord.trim().toUpperCase();
String[] wordleWordArray = wordleWord.split("");
String[] userWordArray = userInput.split("");
List<String> chosenWordList = Arrays.asList(wordleWordArray);
List<String> userWordList = Arrays.asList(userWordArray);
List<Integer> colorings = new ArrayList<>();
for (int i = 0; i < 5; i++) {
if (chosenWordList.contains(userWordList.get(i))) {
if (chosenWordList.get(i).equals(userWordList.get(i))) {
colorings.add(1);
} else {
colorings.add(0);
}
} else {
colorings.add(-1);
}
}
return colorings;
}
/**
* playTurn allows players to play a turn. Returns true if the word can be
* successfully added to the board and false if the word is invalid. A word
* is invalid if its length is not 5 or the word is not in the given dictionary.
*
* @param s the user's guess
* @return whether the turn was successful
*/
// true if word can be entered, false otherwise
public boolean playTurn(String s) {
if (gameOver || !isValidInput(s.trim())) {
return false;
}
numGuesses++;
if (numGuesses == 6 || isCorrectWord(s.trim())) {
if (isCorrectWord(s)) {
wonGame = true;
}
gameOver = true;
}
return true;
}
public boolean isCorrectWord(String s) {
return s.trim().toUpperCase().equals(wordleWord);
}
/**
* checkWinner checks whether the game has reached a win condition.
* checkWinner checks whether the guess is structurally equal to the actual word
*
* @param s the user's guess
* @return true if the guess is the actual word, false otherwise
*/
public boolean checkWinner(String s) {
if (isCorrectWord(s)) {
gameOver = true;
wonGame = true;
return true;
}
return false;
}
/**
* printGameState prints the current game state
* for debugging.
*/
public void printGameState(String s) {
boolean result = playTurn(s);
if (result) {
List<Integer> colorings = getColors(s, wordleWord);
if (checkWinner(s)) {
System.out.println("You found the correct word: " + s);
System.out.println("Number of guesses taken: " + numGuesses);
System.out.println(colorings);
System.out.println();
} else {
System.out.println("You guessed: " + s);
System.out.println("Aw rip, better luck next guess.");
System.out.println("Number of guesses left: " + (6 - numGuesses));
System.out.println(colorings);
System.out.println();
}
} else {
if (!isValidInput(s)) {
System.out.println("You guessed: " + s);
System.out.println("Invalid word: Please input an actual 5-letter word");
System.out.println("Number of guesses left: " + (6 - numGuesses));
System.out.println();
} else {
if (wonGame) {
System.out.println("You already won the game silly bozo");
} else {
System.out.println(
"Sadly, you did not find the correct word. The word was: " + wordleWord
);
}
}
}
}
/**
* reset (re-)sets the game state to start a new game.
*/
public void reset() {
numGuesses = 0;
gameOver = false;
wordleWord = getChosenWord().toUpperCase();
wonGame = false;
}
/**
* This main method illustrates how the model is completely independent of
* the view and controller. We can play the game from start to finish
* without ever creating a Java Swing object.
*
* This is modularity in action, and modularity is the bedrock of the
* Model-View-Controller design framework.
*
* Run this file to see the output of this method in your console.
*/
public static void main(String[] args) {
Wordle1 w = new Wordle1();
w.setWordleWord("SPENT");
w.printGameState("ARISE");
w.printGameState("COUNT");
w.printGameState("CRANE");
w.printGameState("URMOM");
w.printGameState("SPLIT");
w.printGameState("sPeNt");
}
}