-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
211 lines (197 loc) · 7.65 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>WORDLE CRACKER</title>
<style>
body {
background-color: #212121;
text-align: center;
}
.tile {
width: 58px;
height: 58px;
border: solid;
border-width: 2px;
border-color: #565758;
}
.letter {
width: 94%;
height: 96%;
background: transparent;
border: none;
color: #d7dadc;
font-size: 2rem;
line-height: 2rem;
font-weight: bold;
text-transform: uppercase;
text-align: center;
vertical-align: middle;
}
.button {
width: 100%;
background: transparent;
border-width: thin;
border-radius: 5px;
cursor: pointer;
}
label {
color: #d7dadc;
display: block;
margin-bottom: 10px;
font-size: large;
}
textarea {
background: #333333;
color: #d7dadc;
padding: 10px;
max-width: 100%;
line-height: 1.5;
border-radius: 5px;
border: 1px solid #565758;
}
</style>
<script src="allWords.js"></script>
<script>
'use strict';
window.onload = function() {
const WORD_LIST = ALL_WORDS.match(/.{1,5}/g);
const ROW = 6, COL = 5;
const STATES = ["unknown", "absent", "present", "correct"];
function rotateState(button) {
const oldState = button.state ?? "unknown";
const newState = STATES[(STATES.indexOf(oldState) + 1) % STATES.length];
setButtonState(button, newState);
}
function setButtonState(button, state) {
const COLORS = {
"unknown": "#212121",
"absent": "#3a3a3c",
"present": "#b59f3b",
"correct": "#538d4e",
};
const id = button.id; // ex: "button01"
const row = id[id.length - 2];
const col = id[id.length - 1];
button.state = state;
button.title = state;
button.style.background = COLORS[state];
const letterEl = document.getElementById(`letter${row}${col}`);
letterEl.style.background = COLORS[state];
updateCandidateList();
}
function updateCandidateList() {
let candidateList = [];
for (const word of WORD_LIST) {
let isCandidateWord = true;
for (let row = 0; row < ROW; ++row) {
const absentLetters = [];
const presentLetters = [];
let remainingLetters = word;
for (let col = 0; col < COL; ++col) {
const letter = document.getElementById(`letter${row}${col}`).value;
if (letter.length === 0) {
continue;
}
const state = document.getElementById(`button${row}${col}`).state ?? "unknown";
if (state === "absent") {
absentLetters.push(letter);
} else if (state === "present") {
presentLetters.push(letter);
} else if (state === "correct") {
if (word[col] !== letter) {
isCandidateWord = false;
break;
}
remainingLetters = remainingLetters.replace(letter, "");
}
} // col loop
for (const absentLetter of absentLetters) {
if (remainingLetters.indexOf(absentLetter) >= 0) {
isCandidateWord = false;
break;
}
}
for (const presentLetter of presentLetters) {
if (remainingLetters.indexOf(presentLetter) < 0) {
isCandidateWord = false;
break;
}
}
if (!isCandidateWord) {
break;
}
} // row loop
if (isCandidateWord) {
candidateList.push(word);
}
}
let title = `${candidateList.length} candidate word`;
title += candidateList.length === 1 ? "!!" : "s";
document.getElementById("candidateWordsTitle").innerHTML = title;
document.getElementById("candidateWords").value = candidateList.join(" ");
}
(function initBoard() {
// Generate table
let html = "";
for (let row = 0; row < ROW; ++row) {
html += "<tr>";
for (let col = 0; col < COL; ++col) {
html += `<td class="tile"><input class="letter" id="letter${row}${col}" type="text" maxlength="1"/></td>`;
}
html += "</tr><tr>";
for (let col = 0; col < COL; ++col) {
html += `<td><input class="button" id="button${row}${col}" type="button"/></td>`
}
html += "</tr>";
}
document.getElementById("board").innerHTML = html;
// Register handlers
for (let row = 0; row < ROW; ++row) {
for (let col = 0; col < COL; ++col) {
const letterEl = document.getElementById(`letter${row}${col}`);
letterEl.addEventListener("click", e => {
e.target.setSelectionRange(0, e.target.value.length);
});
letterEl.addEventListener("input", e => {
if (e.target.value.length > 0) {
e.target.value = e.target.value.toUpperCase();
// Focus jumps to the next letter tile if it is empty
if (col < COL - 1) {
const nextLetterEl = document.getElementById(`letter${row}${col + 1}`);
if (nextLetterEl.value.length < 1) {
nextLetterEl.focus();
}
}
}
updateCandidateList();
});
const button = document.getElementById(`button${row}${col}`);
button.addEventListener("click", e => {
rotateState(e.target);
});
setButtonState(button, "unknown");
}
}
updateCandidateList();
document.getElementById("letter00").focus();
})();
}
</script>
</head>
<body>
<label>WORDLE CRACKER</label>
<div style="display: flex; justify-content: center;">
<div style="margin: 10px 20px;">
<label>Guesses</label>
<table id="board"></table>
</div>
<div style="margin: 10px 20px;">
<label id="candidateWordsTitle"></label>
<textarea id="candidateWords" rows="27" cols="33" readonly></textarea>
</div>
</div>
<a href="https://github.com/randalhsu/WordleCracker" style="text-decoration: none;"><label style="cursor: pointer;">By Randal</label></a>
</body>
</html>