-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordle.java
171 lines (122 loc) · 4.67 KB
/
Wordle.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
/**
* CS 152 Lab 5 - Wordle
*
* Implement the methods needed to play a Wordle-like game.
*
* Student name: Roman Balayar
*/
import java.util.Random;
import java.util.Scanner;
/** Contains methods for a wordle clone */
public class Wordle {
/** Represents a letter in the guess in the correct position. */
public static final char CORRECT = 'X';
/** Represents a letter in the guess that is present, but in wrong place. */
public static final char PRESENT = 'o';
/** Represents a letter in the guess does not occur in the word at all. */
public static final char MISSING = '.';
/** How many guesses do we get to solve the puzzle? */
public static final int NUMBER_OF_GUESSES = 6;
/**
* Picks a random word from the dictionary.
* @param dictionary An array of words.
* @return Randomly chosen word from dictionary.
*/
public static String getRandomWord(String[] dictionary) {
// TODO: Implement this method
if( dictionary == null || dictionary.length == 0) {
System.out.println("Dictionary is empty or null.");
return null;
}
Random ran = new Random();
int radomIndex = ran.nextInt(dictionary.length);
return dictionary[radomIndex];
}
/**
* Is the guess a recognized word?
* @param dictionary Array of known words.
* @param guess The guess word.
* @return True if guess is in dictionary, false if not.
*/
public static boolean isValidWord(String[] dictionary, String guess) {
// TODO: Implement this method
if (dictionary != null || dictionary.length == 0 && guess != null) {
for (String word : dictionary) {
if (word.equals(guess)) {
return true;
}
}
}
return false;
}
/**
* How close is the guess to the secret word?
* @param word The secret word
* @param guess Guessed word
* @return Array of characters corresponding to the letters of guess,
* where the char at a given index is:
* CORRECT if the guess letter appears at that position in word
* PRESENT if the guess letter is present in word elsewhere
* MISSING if the guess letter does not occur in word
*/
public static char[] getGuessResult(String word, String guess) {
// TODO: Implement this method
if (word == null || guess == null) {
return new char[guess.length()];
}
char[] result = new char[word.length()];
for (int i = 0; i < word.length(); i++) {
char wordChar = word.charAt(i);
char guessChar = guess.charAt(i);
if (wordChar == guessChar) {
result[i] = CORRECT;
} else if (word.contains(String.valueOf(guessChar))) {
result[i] = PRESENT;
} else {
result[i] = MISSING;
}
}
return result;
}
/**
* Is this a winning result?
* @param guessResult Array as returned by getGuessResult
* @return True if all places are CORRECT, false if not
*/
public static boolean isWinningResult(char[] guessResult) {
// TODO: Implement this method
for (char result : guessResult) {
if (result != CORRECT) {
return false;
}
}
return true;
}
/**
* Plays a console based Wordle game
* @param args Ignored
*/
public static void main(String[] args) {
System.out.println("Let's play Wordle!");
System.out.println();
String[] words = WordleDictionary.FIVE_LETTER_WORDS;
Scanner in = new Scanner(System.in);
String secret = getRandomWord(words);
int guesses = NUMBER_OF_GUESSES;
boolean winning;
do {
System.out.println("Guesses remaining: " + guesses);
System.out.println("What is your guess?");
String guess = in.nextLine().trim().toLowerCase();
while(!isValidWord(words, guess)) {
System.out.println("Not a recognized word! Try again");
guess = in.nextLine().trim().toLowerCase();
}
char[] guessResult = getGuessResult(secret, guess);
System.out.println(new String(guessResult));
winning = isWinningResult(guessResult);
guesses--;
} while(guesses > 0 && !winning);
System.out.println("The word was " + secret);
}
}