-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordle.java
139 lines (130 loc) · 5.11 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
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Wordle {
// Various game constants
private final int TRIES = 6;
private final int WORD_LENGTH = 5;
private final int TILE_SIZE = 64;
private final int TILE_SPACING = 8;
private final int SHIFT = 32;
private final String[] WORDS;
private final String ANSWER;
// Variables that get updated throughout the game
private char[][] guesses;
private Color[][] tileColors;
private int currentGuessIndex;
private int currentLetterIndex;
private boolean correctWordGuessed;
public Wordle() {
WORDS = new String[14855];
loadWords();
ANSWER = WORDS[(int) (Math.random() * WORDS.length)];
guesses = new char[TRIES][WORD_LENGTH];
tileColors = new Color[TRIES][WORD_LENGTH];
currentGuessIndex = 0;
currentLetterIndex = 0;
correctWordGuessed = false;
}
private void loadWords() {
try {
Scanner scanner = new Scanner(new File("Words.txt"));
int index = 0;
while (scanner.hasNextLine()) {
WORDS[index] = scanner.nextLine();
index++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void update() {
if (StdDraw.hasNextKeyTyped()) {
char keyTyped = StdDraw.nextKeyTyped();
if (Character.isAlphabetic(keyTyped) && currentLetterIndex < WORD_LENGTH) {
guesses[currentGuessIndex][currentLetterIndex] = keyTyped;
currentLetterIndex++;
} else if (keyTyped == KeyEvent.VK_ENTER && currentLetterIndex == WORD_LENGTH) {
String guess = new String(guesses[currentGuessIndex]);
if (binarySearch(guess)) {
setTileColors(guess);
currentGuessIndex++;
currentLetterIndex = 0;
if (guess.equals(ANSWER)) {
correctWordGuessed = true;
}
}
} else if (keyTyped == KeyEvent.VK_BACK_SPACE && currentLetterIndex > 0) {
guesses[currentGuessIndex][currentLetterIndex - 1] = Character.MIN_VALUE;
currentLetterIndex--;
}
}
}
private boolean binarySearch(String value) {
int low = 0;
int high = WORDS.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (WORDS[mid].equals(value)) {
return true;
} else if (WORDS[mid].compareTo(value) < 0) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return false;
}
private void setTileColors(String guess) {
String charsLeft = ANSWER;
List<Integer> indexesLeft = new ArrayList<>();
// Green and gray tiles
for (int i = 0; i < WORD_LENGTH; i++) {
char guessChar = guess.charAt(i);
if (guessChar == ANSWER.charAt(i)) {
tileColors[currentGuessIndex][i] = StdDraw.GREEN;
int index = charsLeft.indexOf(guessChar);
charsLeft = charsLeft.substring(0, index) + charsLeft.substring(index + 1);
} else if (!ANSWER.contains(guessChar + "")) {
tileColors[currentGuessIndex][i] = StdDraw.GRAY;
} else {
indexesLeft.add(i);
}
}
// Yellow tiles
for (Integer index : indexesLeft) {
char guessChar = guess.charAt(index);
if (charsLeft.contains(guessChar + "")) {
tileColors[currentGuessIndex][index] = StdDraw.YELLOW;
int i = charsLeft.indexOf(guessChar);
charsLeft = charsLeft.substring(0, i) + charsLeft.substring(i + 1);
} else {
tileColors[currentGuessIndex][index] = StdDraw.GRAY;
}
}
}
public void draw() {
for (int row = 0; row < TRIES; row++) {
for (int col = 0; col < WORD_LENGTH; col++) {
int x = col * (TILE_SIZE + TILE_SPACING) + TILE_SIZE / 2 + SHIFT;
int y = row * (TILE_SIZE + TILE_SPACING) + TILE_SIZE / 2 + SHIFT;
StdDraw.setPenColor(StdDraw.GRAY);
StdDraw.rectangle(x, y, TILE_SIZE / 2, TILE_SIZE / 2);
Color tileColor = tileColors[row][col];
if (tileColor != null) {
StdDraw.setPenColor(tileColor);
StdDraw.filledRectangle(x, y, TILE_SIZE / 2, TILE_SIZE / 2);
}
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.text(x, y, Character.toUpperCase(guesses[row][col]) + "");
}
}
}
public boolean isGameOver() {
return correctWordGuessed || currentGuessIndex == TRIES;
}
}