-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordle.java
More file actions
110 lines (102 loc) · 4 KB
/
Wordle.java
File metadata and controls
110 lines (102 loc) · 4 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
import java.util.*;
import java.util.stream.*;
import java.io.*;
public class Wordle {
String puzzle;
Set<String> dictionary;
// Set<Character> puzzleChars;
Map<Character, Integer> puzzleCharCounts;
final String GREENSQUARE = "🟩", YELLOWSQUARE = "🟨", GRAYSQUARE = "⬜";
final int WORDLENGTH = 5, NUMGUESSES = 6;
final String WINSTRING = "🟩🟩🟩🟩🟩";
public Wordle(String puzzle) {
this.puzzle = puzzle;
// puzzleChars = new TreeSet<>();
// for (char c : puzzle.toCharArray()) puzzleChars.add(c);
puzzleCharCounts = new TreeMap<>();
for (char c : puzzle.toCharArray()) {
puzzleCharCounts.compute(c, (k, v) -> v == null ? 1 : v+1);
}
dictionary = loadDictionary();
}
public static Set<String> loadDictionary() {
String dictfile = "/workspaces/Wordle/valid-wordle-words.txt";
Set<String> dictionary = new TreeSet<>();
try {
BufferedReader br = new BufferedReader(new FileReader(dictfile));
String line;
while ((line = br.readLine()) != null) {
dictionary.add(line.toUpperCase());
}
br.close();
} catch (IOException e) {
System.out.println("Error reading dictionary file: " + e);
}
return dictionary;
}
public String checkGuess(String guess) {
if (guess.length() != WORDLENGTH) {
System.out.println("Too short");
return null;
}
if (!dictionary.contains(guess)) {
System.out.println("Word not in dictionary");
return null;
}
Map<Character, Integer> localPuzzleCharCounts = new TreeMap<>(puzzleCharCounts); // deep copy
// NOT Map<Character, Integer> localPuzzleCharCounts = puzzleCharCounts; // shallow copy
String [] result = new String[WORDLENGTH];
for (int i = 0; i < puzzle.length(); i++) {
if (guess.charAt(i) == puzzle.charAt(i)) {
result[i] = GREENSQUARE;
localPuzzleCharCounts.compute(guess.charAt(i), (k, v) -> v == 1 ? null : v - 1);
/*
* We could write this as:
* char c = guess.charAt(i);
* Integer charCount = localPuzzleCharCounts.get(c);
* charCount--;
* if (charCount == 0) localPuzzleCharCounts.remove(c);
* else (localPuzzleCharCounts.put(c, charCount));
*/
}
}
for (int i = 0; i < puzzle.length(); i++) {
char c = guess.charAt(i);
if (result[i] != null) continue; // if result[i] currently holds a green square, don't change it
if (localPuzzleCharCounts.containsKey(c)) {
result[i] = YELLOWSQUARE;
localPuzzleCharCounts.compute(c, (k, v) -> v == 1 ? null : v-1);
} else {
result[i] = GRAYSQUARE;
}
}
String resultString = Arrays.stream(result).collect(Collectors.joining(""));
return resultString;
}
public void play() {
Scanner sc = new Scanner(System.in);
int guesses = 0;
while (guesses < NUMGUESSES) {
System.out.println("Enter your guess. You have " + (NUMGUESSES - guesses) + " guesses left.");
String guess = sc.next();
String guessResult = checkGuess(guess);
if (guessResult != null) {
guesses++;
System.out.println(guessResult);
if (guessResult.equals(WINSTRING)) {
System.out.println("You guessed the word in " + guesses + " guesses!");
sc.close();
return;
}
}
}
System.out.println("You lose.");
System.out.println("The word was " + puzzle);
sc.close();
return;
}
public static void main(String[] args) {
Wordle w = new Wordle(args[0]);
w.play();
}
}