-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordle.java
More file actions
303 lines (288 loc) · 10.7 KB
/
Wordle.java
File metadata and controls
303 lines (288 loc) · 10.7 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import java.util.Scanner;
import java.util.Random;
/**
* Project 2: Wordle
*
* This project has you create a text based version of Wordle
* (https://www.nytimes.com/games/wordle/index.html). Wordle is a word guessing
* game in which you have 6 tries to guess a 5-letter word. You are told whether
* each letter of your guess is in the word and in the right position, in the
* word but in the wrong position, or not in the word at all.
*
* Some key differences in our version are:
*
* - Text menu based with no grid. Players have to scroll up to see their
* previous guesses.
*
* - Support for 4, 5, or 6 letter words
*
* - We don't check for whether a guess is a valid word or not. Players can
* guess anything they want (of the correct length).
*
* Fun facts: The original Wordle was developed by Josh Wardle. Wardle's wife
* chose the official word list for the game.
*
* 500.112 Gateway Computing: Java Spring 2022
*
* @author Mark Faust (mfaust4 - 10/5/22)
*/
public class Wordle {
/**
* Defining the only Random variable you may (and must) use. DO NOT CHANGE
* THIS LINE OF CODE.
*/
static Random gen = new Random(0);
/**
* Defines the number of guesses the player starts with for each word. DO NOT
* CHANGE THIS LINE OF CODE.
*/
static final int MAX_GUESSES = 6;
/**
* Defines the number of hints the player starts with for each word. DO NOT
* CHANGE THIS LINE OF CODE.
*/
static final int MAX_HINTS = 2;
/**
* The main method. This is where most of your menu logic and game logic
* (i.e. implementation of the rules of the game ) will end up. Feel free to
* move logic in to smaller subroutines as you see fit.
*
* @param args commandline args
*/
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int guesses = MAX_GUESSES;
int hints = MAX_HINTS;
String word = newWord();
String guess;
String text = prompt(kb);
while (true) {
//checks if entered text is 'E'
if (text.charAt(0) == 'E') {
System.exit(0);
}
//checks if entered text is 'N'
if (text.charAt(0) == 'N') {
word = newWord();
guesses = MAX_GUESSES;
hints = MAX_HINTS;
text = prompt(kb);
continue;
}
//checks if entered text is 'H'.
if (text.charAt(0) == 'H') {
//checks if any hints are left
if (hints > 0) {
giveHint(word);
--hints;
//prints number of hints left
if (hints == 1) {
System.out.println("You have " + hints + " hint remaining.");
}
else {
System.out.println("You have " + hints + " hints remaining.");
}
text = prompt(kb);
continue;
}
else {
System.out.println("Sorry, you're out of hints!");
text = prompt(kb);
continue;
}
}
//checks if entered text is 'G'
if (text.charAt(0) == 'G') {
//checks if any guesses are left
if (guesses > 0) {
System.out.println("Enter your guess:");
guess = kb.nextLine();
//makes sure that guess is valid
if (validateGuess(word.length(), guess)) {
--guesses;
//check to see if the correct word was guessed
if (checkGuess(word, guess)) {
System.out.println("Congrats! You won!");
guesses = 0;
text = prompt(kb);
continue;
}
//prints how many guesses remaining if guess isn't right
else {
if (guesses == 1) {
System.out.print("You have ");
System.out.println(guesses + " guess remaining.");
}
else if (guesses != 0) {
System.out.print("You have ");
System.out.println(guesses + " guesses remaining.");
}
else {
System.out.print("Sorry, you're out of guesses! ");
System.out.print("The word was " + word + ". ");
System.out.print("Use the \"n\"/\"N\" command ");
System.out.println("to play again.");
}
text = prompt(kb);
continue;
}
}
else {
text = prompt(kb);
continue;
}
}
else {
System.out.print("Sorry, you're out of guesses! ");
System.out.println("Use the \"n\"/\"N\" command to play again.");
text = prompt(kb);
continue;
}
}
}
}
/**
* Prints "HINT! The word contains the letter: X" where X is a randomly
* chosen letter in the word parameter.
*
* @param word The word to give a hint for.
*/
static void giveHint(String word) {
int i = gen.nextInt(word.length());
System.out.print("HINT! The word contains the letter: ");
System.out.println(word.charAt(i));
}
/**
* Checks the players guess for validity. We define a valid guess as one that
* is the correct length and contains only lower case letters and upper case
* letters. If either validity condition fails, a message is printed
* indicating which condition(s) failed.
*
* @param length The length of the current word that the player is trynig to
* guess.
* @param guess The guess that the player has entered.
* @return true if the guess is of the correct length and contains only valid
* characters, otherwise false.
*/
static boolean validateGuess(int length, String guess) {
//makes sure guess is one of the accepted values (letter and length)
for (int i = 0; i < guess.length(); ++i) {
if (guess.length() != length &&
(guess.charAt(i) < 65 || guess.charAt(i) > 122)) {
System.out.println("You must enter a guess of length " + length);
System.out.print("Your guess must only contain upper case ");
System.out.println("letters and lower case letters");
return false;
}
}
//makes sure guess is of the right length
if (guess.length() != length) {
System.out.println("You must enter a guess of length " + length);
return false;
}
//makes sure guess only contains letters
for (int i = 0; i < guess.length(); ++i) {
if (guess.charAt(i) < 65 || guess.charAt(i) > 122) {
System.out.print("Your guess must only contain upper case ");
System.out.println("letters and lower case letters");
return false;
}
}
return true;
}
/**
* Checks the player's guess against the current word. Capitalization is
* IGNORED for this comparison. This function also prints a string
* corresponding to the player's guess. A ? indicates a letter that isn't in
* the word at all. A lower case letter indicates that the letter is in the
* word but not in the correct position. An upper case letter indicates a
* correct letter in the correct position. Example:
*
* SPLINE (the correct word)
*
* SPEARS (the player's guess)
*
* SPe??s (the output printed by this function)
*
* Suggestion 1: Convert guess to upper case before doing anything else. This
* can help simplify later logic.
*
* Suggestion 2: Consider using String.indexOf
*
* @param word The current word the player is trying to guess.
* @param guess The guess that a player has entered.
* @return true if the word and guess match IGNORING CASE, otherwise false.
*/
static boolean checkGuess(String word, String guess) {
word = word.toUpperCase();
guess = guess.toUpperCase();
if (word.equals(guess)) {
System.out.println(word);
return true;
}
int i = 0;
//checks each letter of user's guess with each letter of the actual word
if (word.length() > 0 && word.length() > 0) {
for (int k = 0; k < word.length(); ++k) {
for (i = 0; i < word.length(); ++i) {
if (guess.charAt(k) == word.charAt(i)) {
if (k == i) {
System.out.print(guess.charAt(k));
}
else {
System.out.print(guess.toLowerCase().charAt(k));
}
break;
}
}
if (i == word.length()) {
System.out.print("?");
}
}
}
System.out.println();
return false;
}
/**
* Chooses a random word using WordProvider.getWord(int length). This should
* print "New word length: X" where x is the length of the word.
*
* @return newWord the randomly chosen word
*/
static String newWord() {
int random = gen.nextInt(3) + 4;
String newWord = WordProvider.getWord(random);
System.out.println("New word length: " + newWord.length());
return newWord;
}
/**
* Prints menu options.
*/
static void printMenu() {
System.out.println("n/N: New word");
System.out.println("h/H: Get a hint");
System.out.println("g/G: Make a guess");
System.out.println("e/E: Exit");
System.out.println("-------------");
}
/**
* Prompts the user to input text used to select
* one of the four differet menu options.
*
* @param kb The Scanner object that is passed in from the main method
* @return text The String literal that is being checked with each menu print
*/
static String prompt(Scanner kb) {
printMenu();
System.out.print("Please enter a choice: ");
String text = kb.nextLine().toUpperCase();
//checks if user input is one of the valid options and no more than 1 char
if ((text.charAt(0) != 'E' && text.charAt(0) != 'H' &&
text.charAt(0) != 'N' && text.charAt(0) != 'G') ||
text.length() > 1) {
System.out.println("Invalid option! Try again!");
text = prompt(kb);
}
return text;
}
}